How to use getSelectedRows in Lightning Datatable to get the selected rows

10:46 AM


Use the 
this.template.querySelector('lightning-datatable').getSelectedRows()
to retrieve the selected rows.




<
template>

<lightning-button onclick={processSelected} label="GetSelection">
</lightning-button>
    <lightning-datatable
if:true={showProducts}
key-field="id"
data={products}
columns={columns}>
</lightning-datatable>
</template>



import {LightningElement, wire} from 'lwc';

const columns = [
{ label: 'Product Name', fieldName: 'Name', type: 'text' },
{ label: 'IsActive', fieldName: 'IsActive' ,type: 'boolean'},
{ label: 'Product Code', fieldName: 'ProductCode', type: 'text' },
{ label: 'Product Family', fieldName: 'Family' , type: 'text'},
];

export default class CategoryProductAssociation extends LightningElement {
products = [];
columns = columns;
searchKey;
showProducts = false;
categoryid;


processSelected(){
console.log("getSelectedRows => ",
        this
.template.querySelector('lightning-datatable').getSelectedRows());
}

}

0 comments

Stats