I have a lightning-datatable, and I'd like to use keyboard shortcuts for my actions -- but to do that I'll need to be able to determine the row that is currently in focus. I can't find documentation on that.

Any ideas?

I don't have any code written for it yet. Basically, I would add a listener to pick up the keycode, then if it's one of the active keys, I'd pull the row data from the row that is currently in focus. Something like this?

 constructor() {
    this.template.addEventListener('keyup', this.shortcuts.bind(this));

    const keycode = event.keyCode;
    if(keycode === 79){
        this.selectedRowData = [datatableName.focus.row]
        this.confirmOptOut = true;       

Note the section in brackets -- that's what I'm searching for. Is there something like component.find('dataTableName').focus.row?

  • can you add code snippte? Commented Apr 8, 2019 at 16:59

1 Answer 1


If you are looking to get a data attribute value on the row (or any element that triggers an event) -- perhaps this can help:

I have a table row in my html as:

<template for:each={cases} for:item="c" for:index="idx">
    <tr key={c.Id} data-id={c.Id} data-rowidx={idx} onclick={handleSelectRow}>

then, in the handler, I can get the "rowidx" or the "id" value using:

  • thanks. I'll need to grab the row id everytime there's a change in focus on the lightning-datatable -- but I don't see an onfocus event for datatable. In my example, the event is the keyup, so there is no target available. Commented Apr 10, 2019 at 0:32
  • Table elements (table/tr/td, etc) don't get focus - I don't believe they can be the target of keyboard focus. Perhaps you are looking to capture keypress on the "document" - and you'd have to track "focus" (such as highlighting a row?) by tracking the id/order/etc in code. However, in my experience, manipulating the DOM (using the "template" in lightning web components) has been troublesome.
    – Paul N
    Commented Apr 10, 2019 at 17:43
  • 1
    PS: I ended up creating my own table -- using SLDS formatting -- instead of using lightning-datatable, as I couldn't populate the cell contents the way I wanted using datatable.
    – Paul N
    Commented Apr 10, 2019 at 17:46
  • Thanks for your thoughts. I'm heavily invested in the datatable at this point, using just about all its built-in options. Guess I'll just have to do with that feature... Commented Apr 10, 2019 at 22:43

You must log in to answer this question.

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