Does anyone here know how to clear the selected rows of lightning:datatable on a button click or post processing of checked records?
2 Answers
You need to get the index of the selected row and remove it from the data attribute, something like this:
getSelectedName: function (cmp, event) {
var selectedRows = event.getParam('selectedRows');
console.log('SelectedRows.. '+JSON.stringify(selectedRows));
var arr = cmp.get('v.mydata');
for (var i = 0; i < selectedRows.length; i++){
console.log('You selected: ' + selectedRows[i].opportunityName);
var indexOfStevie = arr.findIndex(j => j.id == selectedRows[i].id);
console.log('>>>Indexxx.. '+JSON.stringify(arr.splice(indexOfStevie,1)));
//cmp.set('v.mydata',arr.splice(indexOfStevie,1));
}
}
The following worked for me:
<aura:attribute name="defaultRows" type="List" default="[]" description="reset selected rows..." />
<lightning:datatable ... selectedRows="{!v.defaultRows}" ... />
So I am setting an empty list attribute for the selectedRows parameter of the dataTable that gets reset everytime it is refreshed. If this is not reset, then on refresh, the onRowsSelection is automatically fired again for the previous selection.
-
better to explain why you think this is the best solution and explain what your code does. Thanks– gllsMay 28, 2018 at 13:19