0

As the title says, has anyone tried to get the row number when editing a column as a draft value (Quantity in the image reference below). So for example, if I add "2" to the Quantity column in Row 1, I can fetch the row number "1".

Anyone has done anything similar? Any advice, code snippets, pointers are helpful.

enter image description here

3

You don't get the row number, but you do get the key-field value. This is sufficient to find the matching row.

var rowIndex = this.data.findIndex(row => row.id === event.detail.draftValues[0].id);

Remember arrays are zero-based, so if for some reason you need the row number as it appears in the table, add 1.

In case you just want the actual row that's in memory, use find instead:

var row = this.data.find(row => row.id === event.detail.draftValues[0].id);
4
  • Thanks for sharing. I used both methods but unfortunately they didn't work. What I did was to use an onchange={findRowIndex} in the data table's HTML, which fired the following logic. But nothing is captured in the console.log. findRowIndex(event){ var row = this.data.find(row => row.id === event.detail.draftValue[0].id); console.log(row); } Am I doing this wrong? What will you suggest as the trigger to fire the event to capture the row number / actual row? – Fred Aug 7 '20 at 2:57
  • @Fred I had a typo, should be draftValues. I did test this in a Playground beforehand, it worked there, should be just fine with the correction. – sfdcfox Aug 7 '20 at 6:07
  • @sfdcfox, In my case there is no "id" is available in my data (or any column with unique values for that matter), what are the possible ways for me then? Data manipulation (adding some unique ID to each row) is the only option, or is there any better solution? – user43598 Mar 31 at 8:41
  • 1
    @user43598 You need a unique value for a datatable to work (the key field), so you could use that as the unique identifier. That's the intention of how you use a data table. – sfdcfox Mar 31 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.