1

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() {
    super();
    this.template.addEventListener('keyup', this.shortcuts.bind(this));
}

// KEYBOARD SHORTCUTS
shortcuts(event){
    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?

0

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:

event.currentTarget.dataset.id
event.currentTarget.dataset.rowidx
  • 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. – PatMcClellan__c Apr 10 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 Apr 10 at 17:43
  • 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 Apr 10 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... – PatMcClellan__c Apr 10 at 22:43

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.