0

The salesforce.com documentation for Lightning Web Component 'Datatable' (https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation) gives sample code for dynamic row-level Actions which includes a call to 'setTimeout()' from function 'getRowActions():

getRowActions(row, doneCallback) {
    const actions = [];
        if (row['isActive']) {
            actions.push({
                'label': 'Deactivate',
                'iconName': 'utility:block_visitor',
                'name': 'deactivate'
            });
        } else {
            actions.push({
                'label': 'Activate',
                'iconName': 'utility:adduser',
                'name': 'activate'
            });
        }
        // simulate a trip to the server
        setTimeout(() => {
            doneCallback(actions);
        }), 200);
}

However, I find that my equivalent 'getRowActions()' function seems to work fine when simply executing 'doneCallback(actions)' rather than using the JavaScript timeout.

Is there some problem expected when not using the timeout? If so, how would I re-produce that problem? And what is the significance of the comment "simulate a trip to the server"?

1 Answer 1

1

There is no expected problem using the callback synchronously. The point of the code example isn't to discourage synchronous use of the callback, but rather to demonstrate that we can, if necessary, perform some asynchronous work, such as calling fetch or an @AuraEnabled method, then use that data to render the final list of options. The comment simply explains that it is going to wait 200ms, as if we were calling code on the server, and wanted to set the row actions upon completion of the callout.

3
  • ok, so I'll therefore keep the code which does the synchronous callback. Thanks. What we did find was that with the setTimeout() used to execute the callback, even though there was no actual request to the server, the Action menu SOMETIMES failed to appear. This is the reason I was investigating the code in the first place. Do you know what would have caused that? Some kind of timing issue, I'm guessing.
    – John Lewis
    May 25, 2023 at 7:46
  • possibly I can now answer my own question: what seems to happen is that when the button is clicked, a small rectangle appears. When the action menu items are displayed as expected they seem to be inserted into this rectangle. However, if the mouse pointer is moved outside the rectangle before the doneCallback() function is executed the rectangle disappears and no menu items are shown. Would that be the explanation?
    – John Lewis
    May 25, 2023 at 8:17
  • 1
    @JohnLewis I'd have to build something to confirm, but that does sound like something that could happen.
    – sfdcfox
    May 25, 2023 at 8:24

You must log in to answer this question.

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