Unable to capture selected rows details in LWC datatable, here is code On selection passing selectedData is showing duplicate data in alert, I need to populate selected rows details(selectedData) in new table after capturing.
If I select 3 records,R1,R2,R3, it is duplicating like R1R1R2R1R2R3, How to remove this diplicate.
It is producing array of jsons like [{R1}{R1}{R2}{R1}{R2}{R3}]
HTML code:
<lightning-datatable
key-field="id"data={data}
columns={columns}
onrowselection= {selectedRowHandler}>
</lightning-datatable>
JS code:
import { LightningElement,api,track,wire } from 'lwc';
import getProds from '@salesforce/apex/MM_ProdsInfo.getProducts';
export default class GetProdsData extends LightningElement {
@track data = [];
selectedData = []; // will pass selected records into new table view
@track error;
@wire(getProds)
wiredProducts({
error,
data
}) {
if (data) {
this.data = data;
} else if (error) {
this.error = error;
}
}
selectedRowHandler(event){
const selectedRows = event.detail.selectedRows;
for ( let i = 0; i < selectedRows.length; i++ ){
if ( !this.selectedData.includes(selectedRows[i].id) )
this.selectedData =[...this.selectedData, selectedRows[i]];
}
alert('@@:::NEW DATA:::'+JSON.stringify(this.selectedData));
}
{
in your html foronrowselection= selectedRowHandler}
i think it's typo error.selectedData
twice? did you check console.log(selectedRows[i].id) ?