function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc developer 1056sfdc developer 1056 

how to get the Key-Field id or row index in javascript LWC

Hi Team,
I'm building the lightning data table where I'm using "id" as a key-field
<lightning-datatable 
                key-field="id" 
                data={accountList} 
                columns={columns}  
                onrowselection={handleRowSelected}
                selected-rows={selection}
                oncellchange={handleCellChange}>
            </lightning-datatable>
I want to retrive the row id in my javascript. How to fetch that ? I have tried many solution like:
let selectedRecords = this.template.querySelector("lightningdatatable").getSelectedRows();
        if (selectedRecords) {
            for (const key in selectedRecords) {
                console.log('checking',selectedRecords[key].event.currentTarget.dataset.id);
               console.log('checking',selectedRecords[key]['id']);
               console.log('checking',selectedRecords[key].event.target.dataset.rowId);
               console.log('checking',selectedRecords[key].dataset.id);
               console.log('checking',selectedRecords[key]['id']);
}
Please let me know if anybody knows the solutions.
Thank you in advance
CharuDuttCharuDutt
Hii SFDC Developer
Try Below Code
<lightning-datatable                  
key-field="id"                  
data={accountList}                  
columns={columns}                   
onrowselection={handleRowSelected}                 
selected-rows={selection} 
oncellchange={handleCellChange}> 
</lightning-datatable>

handleRowSelected(event) {
        const selectedRows = event.detail.selectedRows;
        // Display that fieldName of the selected rows
        for (let i = 0; i < selectedRows.length; i++){
            alert("You selected: " + selectedRows[i].Id);
        }
    }
Please Mark It As Best Answer If It Helps
Thank You!

 
sfdc developer 1056sfdc developer 1056
Hi Charu,
Thanks for reply. But it is not working and giving error saying ReferenceError: key is not defined in lWC. 
Also i dnt want Id(record Id), I want the row id/ Key field value.
Rama ChunduRama Chundu
Did you find a solution for this?
Suraj Tripathi 47Suraj Tripathi 47
Hii SFDC Developer,

Try this code :
//DataTable 
<lightning-datatable 
                key-field="id" 
                data={accountList} 
                columns={columns}  
                onrowselection={handleFunction}
                selected-rows={selection}
                oncellchange={handleCellChange}>
</lightning-datatable>

//JavaScript:

handleFunction(event) {
        var selectedRowsArray = event.detail.selected-Rows;
        // Display that fieldName of the selected rows
        for (let i = 0; i < selectedRowsArray.length; i++){
            alert("You selected: " + selectedRows[i].Id);
        }
    }
---------------
If you find your Solution then mark this as the best answer to close this question. 

Thank you!
Regards,
Suraj Tripathi
Vani N1Vani N1

Inorder to fetch the record id of the current selected row in lightning data table we can use the below workaround.

HTML

<lightning-datatable class="slds-p-left_x-small" hide-checkbox-column={hidecheckboxcolumn}
                    key-field="id" data={activeGridData} columns={columns} onrowaction={handleRowAction}
                    onheaderaction={handleHeaderAction} onrowselection={getSelectedName}>
                </lightning-datatable>

Javascript

getSelectedName(event){
          //event.detail.selectedrows will fetch all the selected records. Since we need the latest clicked record we need to fetch the last selected row
           let lastidx = event.detail.selectedrows.length - 1;
           let selectedrow = event.detail.selectedrows[lastidx];
}

SUSHANT ROKADESUSHANT ROKADE
Vani N1- How to get the deselected recordId from the lighting datatable?
Hemant Ghaturkar 25Hemant Ghaturkar 25
how to display records from one table to onther table with the help of rowselection? i am facing issue to display please give me solution