Skip to content

Commit 9eaab37

Browse files
Merge pull request #18 from Sarveshgithub/dev
Dev
2 parents 3b3d24f + 5712c3e commit 9eaab37

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

force-app/main/default/lwc/lwcRelatedList/lwcRelatedList.html

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
<template>
22
<!-- Main content -->
33
<lightning-card variant="Narrow" title={titleFormatted} icon-name={iconName}>
4-
<!--default button create -->
5-
<template if:false={actionButtonsListNotEmpty}>
6-
<lightning-button label="New" title="Non-primary action" onclick={newRecord} class="slds-m-left_x-small"
7-
slot="actions"></lightning-button>
8-
</template>
9-
10-
<!-- custom buttons for list -->
4+
<!-- custom action buttons for list, by default new is displayed -->
115
<template if:true={actionButtonsListNotEmpty}>
126
<template for:each={actionButtonsList} for:item="actionButton" for:index="index">
13-
<lightning-button if:true={actionButton.callApex}
14-
key={actionButton.label}
7+
<lightning-button key={actionButton.label}
158
label={actionButton.label}
169
title={actionButton.label}
1710
variant={actionButton.variant}
18-
onclick={callApexFromButton}
19-
data-name={actionButton.label}
11+
onclick={callButtonAction}
12+
data-name={actionButton.uniqueName}
2013
class="slds-m-left_x-small"
2114
slot="actions"
2215
>
2316
</lightning-button>
24-
25-
<lightning-button if:false={actionButton.callApex}
26-
data-index={index}
27-
key={actionButton.label}
28-
label={actionButton.label}
29-
title={actionButton.label}
30-
variant={actionButton.variant}
31-
onclick={fireEventFromButton}
32-
class="slds-m-left_x-small"
33-
slot="actions"
34-
>
35-
</lightning-button>
36-
3717
</template>
3818
</template>
3919

@@ -80,4 +60,4 @@
8060
onclick={lastPage} class="slds-m-left_xx-small slds-m-right_xx-small"></lightning-button>
8161
</div>
8262
</lightning-card>
83-
</template>
63+
</template>

force-app/main/default/lwc/lwcRelatedList/lwcRelatedList.js

+56-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const actions = [
1616
{ label: "Edit", name: "edit" },
1717
{ label: "Delete", name: "delete" }
1818
];
19+
20+
const buttonsOfList = {
21+
'new' : { label : "New", variant: "neutral", needSelectedRows: false },
22+
'delete-everything' : { label : "delete all", variant: "destructive", needSelectedRows: false },
23+
'delete-selected' : { label : "delete selected", variant : "brand", needSelectedRows: true }
24+
};
25+
1926
export default class LightningDatatable extends NavigationMixin(
2027
LightningElement
2128
) {
@@ -29,6 +36,7 @@ export default class LightningDatatable extends NavigationMixin(
2936
@api whereClause;
3037
@api limit = 10;
3138
@api isCounterDisplayed;
39+
@api actionButtonsToDisplay;
3240
@api actionButtonsList; //buttons for the list
3341
@api showCheckboxes;
3442
// Private Property
@@ -44,6 +52,40 @@ export default class LightningDatatable extends NavigationMixin(
4452
if (this.columns != null && this.columns != undefined) {
4553
cols = JSON.parse(this.columns);
4654
}
55+
56+
//defining custom list buttons based on actionButtonsToDisplay(string seperated design property)
57+
if(this.actionButtonsToDisplay && this.actionButtonsToDisplay != undefined ) {
58+
let arrayOfButtonsKeys = this.actionButtonsToDisplay.replace(/\s/g, '').split(',');
59+
let actionsButtons = [];
60+
61+
if(arrayOfButtonsKeys && arrayOfButtonsKeys.length > 0) {
62+
arrayOfButtonsKeys.forEach(buttonKey => {
63+
//checking if button key is empty or button not defined
64+
if(buttonKey && buttonsOfList[buttonKey]) {
65+
let button = buttonsOfList[buttonKey];
66+
button.uniqueName = buttonKey;
67+
68+
actionsButtons.push(button );
69+
70+
//if one button needs selected rows then we show checkboxes
71+
if(buttonsOfList[buttonKey].needSelectedRows ) {
72+
this.showCheckboxes = true;
73+
}
74+
}
75+
});
76+
77+
if(actionsButtons.length > 0) {
78+
this.actionButtonsList = actionsButtons;
79+
} else {
80+
this.setDefaultListButtons();
81+
}
82+
} else {
83+
this.setDefaultListButtons();
84+
}
85+
} else {
86+
this.setDefaultListButtons();
87+
}
88+
4789
cols.push({
4890
type: "action",
4991
typeAttributes: { rowActions: actions }
@@ -56,6 +98,11 @@ export default class LightningDatatable extends NavigationMixin(
5698
this.fetchRecords();
5799
}
58100

101+
setDefaultListButtons() {
102+
this.actionButtonsList = [];
103+
this.actionButtonsList.push(buttonsOfList['new']);
104+
}
105+
59106
fetchRecords() {
60107
getRecords({ soql: this.soql, SObjectName: this.objectName, iconName: this.iconName })
61108
.then((data) => {
@@ -267,35 +314,20 @@ export default class LightningDatatable extends NavigationMixin(
267314
return (this.isCounterDisplayed) ? this.title + ` (${this.totalRows})` : this.title;
268315
}
269316

270-
callApexFromButton(event) {
271-
//call desired apex method based on buttonLabel value
317+
callButtonAction(event) {
318+
//call desired javacript method or apex call, or throw an event based on the button key(new, delete-selected...)
272319
//if button has needSelectedRows set to true, have selected rows using this.selectedRows
273320
const buttonLabel = event.target.dataset.name;
274-
console.log('callApexFromButton, button clicked has label : '+buttonLabel);
275-
}
276321

277-
fireEventFromButton(event) {
278-
event.preventDefault();
279-
console.log('fireEventFromButton');
280-
const buttonPos = Number(event.target.dataset.index) + 1;
281-
const button = this.actionButtonsList[buttonPos-1];
282-
let buttonEvent = null;
283-
284-
console.log('action'+buttonPos);
285-
286-
if(button.needSelectedRows && button.needSelectedRows === true) {
287-
buttonEvent = new CustomEvent(
288-
'action'+buttonPos,
289-
{ detail : JSON.stringify(this.selectedRows) }
290-
);
291-
} else {
292-
buttonEvent = new CustomEvent('action'+buttonPos);
322+
if(buttonLabel === 'new') {
323+
this.newRecord();
324+
} else if(buttonLabel === 'delete-selected') {
325+
const eventDeleteSelected = new CustomEvent('deleteselected', {detail : JSON.stringify(this.selectedRows) });
326+
this.dispatchEvent(eventDeleteSelected);
293327
}
294-
295-
// Dispatches the event.
296-
this.dispatchEvent(buttonEvent);
328+
console.log('callButtonAction, button clicked has label : '+buttonLabel);
297329
}
298-
330+
299331
handleRowSelection(event){
300332
this.selectedRows = JSON.parse(JSON.stringify(event.detail.selectedRows) );
301333
}

force-app/main/default/lwc/lwcRelatedList/lwcRelatedList.js-meta.xml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<property name="relatedFieldAPI" type="String" default="" label="Enter Related field API Name" />
1818
<property name="whereClause" type="String" default="" label="Enter WHERE clause" />
1919
<property name="isCounterDisplayed" type="Boolean" label="Show the number of record" />
20+
<property name="actionButtonsToDisplay" type="String" label="Buttons to display" />
2021
</targetConfig>
2122
</targetConfigs>
2223
</LightningComponentBundle>

0 commit comments

Comments
 (0)