function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dev87Dev87 

Lightning Component- Button Name

Hello everyone,
I'm working with lightning component.
I have button and I want to display list of records on click button.
When I click on the button, I get the name of the button and I make a select in a table according to the name of the button
Sunil MadanaSunil Madana
Hi Dev87,
You could use the code below to show all contacts based on the click of the button and show them on datatable.

Component:
<aura:component controller="ContactController">
    <aura:attribute name="mydata" type="Object"/>
    <aura:attribute name="mycolumns" type="List"/>
    <lightning:button variant="brand" label="Show All Contacts" onclick="{! c.init }" />
    <aura:if isTrue="{! v.mycolumns.length > 0 }">
        <lightning:datatable data="{! v.mydata }" 
                             columns="{! v.mycolumns }" 
                             keyField="Id"
                             hideCheckboxColumn="true"/>
    </aura:if>
</aura:component>
Controller:
({
    init: function (cmp, event, helper) {
        cmp.set('v.mycolumns', [
            {label: 'Contact Name', fieldName: 'Name', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', type: 'phone'},
            {label: 'Email', fieldName: 'Email', type: 'email'}
        ]);
        helper.getData(cmp);
    }
})
Helper:
({
    getData : function(cmp) {
        var action = cmp.get('c.getContacts');
        action.setCallback(this, $A.getCallback(function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set('v.mydata', response.getReturnValue());
            } else if (state === "ERROR") {
                var errors = response.getError();
                console.error(errors);
            }
        }));
        $A.enqueueAction(action);
    }
})
Hope the above solutions helps.