1

I'm trying to get the list of selected rows with onrowselection event.

                        <lightning-datatable
                                key-field="index"
                                data={currentPageData}
                                columns={columns} 
                                onsort={updateColumnSorting}
                                sorted-by={sortedBy}
                                sorted-direction={sortDirection}
                                is-loading={isLoading}
                                onrowselection={onRowSelection}
                                selected-rows={selectedRows}
                                >

enter image description here onRowSelection()

  onRowSelection(event){
      let currentRows = event.detail.selectedRows;

      console.log('event '+JSON.stringify(event));
 }

I'm keeping track of selected index ids in selectedRows list.

But when you unselect a record onrowselection, seems event does not provide unselected record id,etc. information.

e.g. In above code, Console.log() When selected/checked

event {"isTrusted":false,"composed":false,"target":{},"srcElement":{},"path":[{},{},{},{},{},{},{}],"detail":{"selectedRows":[{"accountNo":"accountNo 1","amount":2.3,"clientName":"TESTclient","communityCode":"ComCode","downloadUrl":"https://www.google.com.au/","dueDate":"2020-08-28","index":2,"invoiceNo":"invoiceNo - 1","issueDate":"2020-08-23","selected":false,"status":"Credit"}]},"NONE":0,"CAPTURING_PHASE":1,"AT_TARGET":2,"BUBBLING_PHASE":3,"type":"rowselection","currentTarget":{},"eventPhase":2,"bubbles":false,"cancelable":false,"defaultPrevented":false,"timeStamp":99900.97999991849,"returnValue":true,"cancelBubble":false}

Console.log() When unselected/unchecked

event {"isTrusted":false,"composed":false,"target":{},"srcElement":{},"path":[{},{},{},{},{},{},{}],"detail":{"selectedRows":[]},"NONE":0,"CAPTURING_PHASE":1,"AT_TARGET":2,"BUBBLING_PHASE":3,"type":"rowselection","currentTarget":{},"eventPhase":2,"bubbles":false,"cancelable":false,"defaultPrevented":false,"timeStamp":198875.6299999077,"returnValue":true,"cancelBubble":false}

Any help to workaround getting the unselected record information?

Appreciate any help, suggestions. Cheers!

3
  • Don't know if you tried this but as I can see you are maintaining a list of selectedRecords first then in your code upon Unselect you can capture the event (on row action get the recordId) and from your list you can remove the recordId.
    – Mitesh
    Commented Aug 25, 2020 at 5:44
  • Hi @Mitesh Yes I could do that, but unfortunately when you click Salesforce provided checkbox column seems it doesn't fire onrowaction event. I tried to have a own select/checkbox column. But when you do it's showing Save/Cancel buttons for Datatable, and other unwanted behaviors which I don't want. Commented Aug 25, 2020 at 5:54
  • Ohh in that case does it fire any event at all? Can we console log the emmited event on click to uncheck a selected row... I am already out of options :-(
    – Mitesh
    Commented Aug 25, 2020 at 6:00

2 Answers 2

4

You can use the array filter method to get the previously unselected rows.

Let's say you have an attribute called currentSelectedRows which will be defaulted to an empty array.

So when the selected row event fires you can get the difference of newly selected rows and old selected rows like this.

currentSelectedRows = [];

handleRowsSelected(event) {
    let currentRows = event.detail.selectedRows;
    if (this.currentSelectedRows.length > 0) {
        let selectedIds = currentRows.map(row => row.id);
        let unselectedRows = this.currentSelectedRows.filter(row => !selectedIds.includes(row.id));
        console.log(unselectedRows);
    }
    this.currentSelectedRows = currentRows;
}
1
  • 1
    Thanks for pointing this - Didn't realize it's sending all objects including previously selected ones too, too easy :) Commented Aug 27, 2020 at 1:25
1

//We can use below code to get unselected rows:- // Deselect function

let unSelectedValues = [];
if(event.detail.config.selection =='rowDeselect'){
 unSelectedValues.push(event.detail.config.value);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .