You need to sign in to do that
Don't have an account?
erikvm
Event handler on lightning:datatable button?
I've got a lightning:datatable that looks like this:
Here's my issue: I want the button to handle the handleRowAction. I don't want a dropdown menu - how can I make my button handle the action? What's the point of having a button on the lightning:datatable if I can't do anything with it? As of now it seems that only "action" can handle stuff like "select this row" etc.
<aura:attribute name="resources" type="Object"/> <aura:attribute name="mycolumns" type="List"/> <aura:handler name="init" value="{! this }" action="{! c.doInit }"/> <lightning:datatable data="{! v.resources }" columns="{! v.mycolumns }" keyField="Id" onrowaction="{! c.handleRowAction }" hideCheckboxColumn="true" />
with a doInit that looks like this:
doInit: function (component, event, helper) { var actions = [ { label: 'Select', name: 'selectRecord' }, ]; component.set('v.mycolumns', [ {type: 'action', typeAttributes: { rowActions: actions, }}, {type: "button", typeAttributes: { iconName: 'utility:add', label: '', name: 'selectRecord', title: 'selectRecord', disabled: false, value: 'test', }}, {label: 'Resource', fieldName: 'Name', type: 'text'}, {label: 'Comment', fieldName: 'Comment__c', type: 'text'}, ]); },
handleRowAction: function (component, event, helper) { var action = event.getParam('action'); var row = event.getParam('row'); switch (action.name) { case 'selectRecord': console.log(row.Id); console.log('Showing Details: ' + JSON.stringify(row)); break;
Here's my issue: I want the button to handle the handleRowAction. I don't want a dropdown menu - how can I make my button handle the action? What's the point of having a button on the lightning:datatable if I can't do anything with it? As of now it seems that only "action" can handle stuff like "select this row" etc.
You can achieve that behaviour in the following way
I have added variant type attribute to the button you are adding and in your handleRowAction method for the case of type 'selectRecord' call the helper method 'addRow' as show below, I am setting the variant of the selected row's button to something else so that I can show that the button is clicked. You can create 'fieldName' for any of the typeAttributes and achieve the same.
All Answers
You can achieve that behaviour in the following way
I have added variant type attribute to the button you are adding and in your handleRowAction method for the case of type 'selectRecord' call the helper method 'addRow' as show below, I am setting the variant of the selected row's button to something else so that I can show that the button is clicked. You can create 'fieldName' for any of the typeAttributes and achieve the same.
This works for me , can you console and check if the data you are getting is bound one to the datatable? and if you could console the console.log(row.Id) and check it will be good.
I had the similar issue because I had in my code (Id as id and its case sensitive). If you still have any issues, if you could post the code it will be helpful.
Thanks
the above check might have become undefined === undefined and its making all the rows as variant 'Brand'
Do you know if it's possible to set a default value for the iconName? Ideally I would like to have it go from "iconName: xxxx" AND "variant: xxxx"
to "iconName: yyyy" and "variant: "yyyy". However, if no button is clicked (just when the component first renders, all buttons just appear as a thin line, as nothing is clicked.
I tried something like
and thenHowever, since nothing is yet clicked, the buttons just appear as "-----". Everything works fine when one button is clicked, but not before that.
I am also implementing Button in Lightning table, I am succeeded to populate the label values on button. Just the text is starting from center. I need to start the text from left side.
here is my code:
doInit: function(component, event, helper) {
component.set('v.mycolumns', [
{label: 'Report Name',
type: 'button',
typeAttributes: {
title: 'Preview',
variant: 'brand',
alternativeText: 'View',
disabled: false,
label: { fieldName: 'Name' },
name: { fieldName: 'Name' },
}},
]);
helper.getPBListHandler(component,event);
},