• sony xperia
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
here i am getting the data from the json response that's why i used  the "htmlres" by using this i am displaying the data in the  data table but not able to sort and i tryied using javascript from w3s but  i am  getting an error...

Html table:

<template>
<template if:true={htmlres}>
<table class="tablecls" id="myTable" >
<tr>
<th><lightning-button onclick={sortTable} label="dateIssued" onsort={handleSortdata}
sorted-by={sortedBy} sorted-direction={sortedDirection} ></lightning-button>
</th>
<th>
<div title="payee">payee</div>
</th>
<th>
<div title="amount">amount</div>
</th>
<th>
<div title="status">status</div>
</th>
<th>
<div title="chequeEFT">chequeEFT</div>
</th>
<th>
<div title="description">description</div>
</th>
</tr>
<template for:each={htmlres} for:item="hres1">
<tr key={hres1}>
<td data-label="dateIssued">
<div>{hres1.dateIssued}</div>
</td>
<td data-label="payee">
<div>{hres1.payee}</div>
</td>
<td data-label="payee">
<div>{hres1.amount}</div>
</td>
<td data-label="payee">
<div>{hres1.status}</div>
</td>
<td data-label="payee">
<div>{hres1.chequeEFT}</div>
</td>
<td data-label="payee">
<div>{hres1.description}</div>
</td>
</tr>
</template>
</table>
</template>
</template>
--------------------------------------
javascript code:
import { LightningElement,api,track } from 'lwc';
export default class Htmlpayment extends LightningElement {
@api htmlres=[];
@track sortedDirection = 'asc';
@track sortedBy;
sortTable(){
// eslint-disable-next-line no-alert
alert('button is clicked');
// eslint-disable-next-line no-alert
alert('button is clicked'+this.htmlres);
let table,rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = this.template.querySelector('myTable');
// eslint-disable-next-line no-undef
window.console.log('myTable--->'+myTable);
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.tr;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD");
y = rows[i + 1].getElementsByTagName("TD");
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir === "asc") {
if (x.toLowerCase() > y.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir === "desc") {
if (x.toLowerCase() < y.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
// Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount === 0 && dir === "asc") {
dir = "desc";
switching = true;
}
}
}
}
}
-----------------------------------------------------------------
css code:
.tablecls {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
.tablecls td{
background-color: #9fd0df25;
padding: 25px;
}
.tablecls th {
border: 1px solid #f3f3f3dc;
padding: 20px;
}
.tablecls tr:nth-child(odd) {
background-color: rgba(44, 205, 226, 0.452);
}
.tablecls tr:hover {
background-color: rgba(28, 227, 241, 0.534);
}
.tablecls th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: rgba(20, 75, 228, 0.925);
color: rgb(255, 255, 255);
}


Thanks in advance....
here i am getting the data from the json response that's why i used  the "htmlres" by using this i am displaying the data in the  data table but not able to sort and i tryied using javascript from w3s but  i am  getting an error...

Html table:

<template>
<template if:true={htmlres}>
<table class="tablecls" id="myTable" >
<tr>
<th><lightning-button onclick={sortTable} label="dateIssued" onsort={handleSortdata}
sorted-by={sortedBy} sorted-direction={sortedDirection} ></lightning-button>
</th>
<th>
<div title="payee">payee</div>
</th>
<th>
<div title="amount">amount</div>
</th>
<th>
<div title="status">status</div>
</th>
<th>
<div title="chequeEFT">chequeEFT</div>
</th>
<th>
<div title="description">description</div>
</th>
</tr>
<template for:each={htmlres} for:item="hres1">
<tr key={hres1}>
<td data-label="dateIssued">
<div>{hres1.dateIssued}</div>
</td>
<td data-label="payee">
<div>{hres1.payee}</div>
</td>
<td data-label="payee">
<div>{hres1.amount}</div>
</td>
<td data-label="payee">
<div>{hres1.status}</div>
</td>
<td data-label="payee">
<div>{hres1.chequeEFT}</div>
</td>
<td data-label="payee">
<div>{hres1.description}</div>
</td>
</tr>
</template>
</table>
</template>
</template>
--------------------------------------
javascript code:
import { LightningElement,api,track } from 'lwc';
export default class Htmlpayment extends LightningElement {
@api htmlres=[];
@track sortedDirection = 'asc';
@track sortedBy;
sortTable(){
// eslint-disable-next-line no-alert
alert('button is clicked');
// eslint-disable-next-line no-alert
alert('button is clicked'+this.htmlres);
let table,rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = this.template.querySelector('myTable');
// eslint-disable-next-line no-undef
window.console.log('myTable--->'+myTable);
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.tr;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD");
y = rows[i + 1].getElementsByTagName("TD");
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir === "asc") {
if (x.toLowerCase() > y.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir === "desc") {
if (x.toLowerCase() < y.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
// Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount === 0 && dir === "asc") {
dir = "desc";
switching = true;
}
}
}
}
}
-----------------------------------------------------------------
css code:
.tablecls {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
.tablecls td{
background-color: #9fd0df25;
padding: 25px;
}
.tablecls th {
border: 1px solid #f3f3f3dc;
padding: 20px;
}
.tablecls tr:nth-child(odd) {
background-color: rgba(44, 205, 226, 0.452);
}
.tablecls tr:hover {
background-color: rgba(28, 227, 241, 0.534);
}
.tablecls th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: rgba(20, 75, 228, 0.925);
color: rgb(255, 255, 255);
}


Thanks in advance....
i am not able to debug my lightning web component javaScript code. am using console.log / window.alert() statements. but it's showing following error :
Unexpected console statement. eslint(no-console)
Unexpected console statement. eslint(no-alert)

any help will be appreciated