Skip to content

Commit 5712c3e

Browse files
Merge pull request #17 from mickaelgudin/dev
Dev : make button configuration easy and unique(not based on event, or method)
2 parents ba03833 + e2e3361 commit 5712c3e

File tree

4 files changed

+99
-79
lines changed

4 files changed

+99
-79
lines changed

README.md

+37-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Salesforce Lightning Data table (LWC Version)
44

5-
![Image description](https://github.com/Sarveshgithub/sfdc-lwc-lightning-datatable/blob/master/lwc-datatable.PNG?raw=true)
5+
![datatable](https://user-images.githubusercontent.com/39730173/158892595-3e7c91a3-9259-4e13-914b-191504ca8a05.PNG)
66

77
## About
88

@@ -18,7 +18,7 @@ The data table has following features.
1818
- Pagination as First,Previous,Next,Last buttons.
1919
- New record creation action
2020
- Row action, like : show detail, edit record, delete record
21-
- configurable buttons (for developers, see [Buttons configuration](#buttons-configuration-for-developers-accessible-in-the-component-and-from-parent-component) )
21+
- configurable buttons (for developers, see [Buttons configuration](#buttons-configuration) )
2222

2323
## Steps to Customization through Design Attribute
2424

@@ -36,6 +36,7 @@ Design Attribute
3636
Enter Related field API Name | :x: | String | Enter related field api name | Example AccountId for contact when component is on account layout.
3737
Enter WHERE clause | :x: | String | provide aditional filters | Example `LastName like '%s' AND Account.Name like '%t'`
3838
| Show the number of record | :x: | Boolean | append the number of records in the title | checked(true) OR not checked(false) |
39+
| Buttons to display | :x: | String | buttons that we want to display | new,delete-selected |
3940

4041
## Columns JSON Example
4142
``` yaml
@@ -61,53 +62,59 @@ Enter WHERE clause | :x: | String | provide aditional filters | Example `LastNam
6162
"type": "text"
6263
}]
6364
```
64-
## Buttons configuration (for developers, accessible in the component and from parent component)
65+
## Buttons configuration
66+
67+
### Buttons definition(javascript controller) :
68+
```
69+
const buttonsOfList = {
70+
'new' : { label : "New", variant: "neutral", needSelectedRows: false },
71+
'delete-everything' : { label : "delete all", variant: "destructive", needSelectedRows: false },
72+
'delete-selected' : { label : "delete selected", variant : "brand", needSelectedRows: true }
73+
};
74+
```
75+
### Default displayed buttons
76+
The "New" button is displayed by default, modify the method setDefaultListButtons to change default buttons.
6577

66-
### Call apex or javascript method
67-
For a button that is going to call callApexFromButton, the properties must be :
68-
- callApexFromButton: true
6978

70-
### Fire event to parent component
71-
For a button that is going to fire an event, the properties must be :
72-
- callApex: false
73-
- needSelectedRows : can be true if you need to send selected rows to parent component
79+
### Button logic definition (fire an event, call a method in the javascript controller)
80+
You can implement your own logic for your new buttons based on button key (new, delete-selected...).
7481

75-
### Example
76-
Javascript array (actionButtonsList property) :
7782
```
78-
[
79-
{ label : "delete all", variant: "destructive", needSelectedRows: true, callApex: true },
80-
{ label : "action button", variant : "brand", needSelectedRows: false, callApex: false },
81-
{ label : "another action button", variant : "brand", needSelectedRows: true, callApex: false }
82-
];
83+
callButtonAction(event) {
84+
//call desired javacript method or apex call, or throw an event based on the button key(new, delete-selected...)
85+
//if button has needSelectedRows set to true, have selected rows using this.selectedRows
86+
const buttonLabel = event.target.dataset.name;
87+
88+
if(buttonLabel === 'new') {
89+
this.newRecord();
90+
} else if(buttonLabel === 'delete-selected') {
91+
new customEvent('deleteselected', {detail : this.rows});
92+
}
93+
console.log('callButtonAction, button clicked has label : '+buttonLabel);
94+
}
8395
```
84-
Parent component (**you don't need parent component, you can define default buttons using actionButtonsList**) :
96+
97+
**From Parent component (for developers) :**
8598
```
8699
//in template
87100
<c-lwc-related-list object-name="Contact"
88101
columns={columns}
102+
title="contacts-from-parent-component"
89103
related-field-api="AccountId"
90104
is-counter-displayed=true
91-
action-buttons-list={actions}
92-
show-checkboxes=true
93-
onaction2={helloWorld}
94-
onaction3={helloWorld}
105+
action-buttons-to-display='new,delete-selected'
106+
ondeleteselected={helloWorld} >
95107
108+
</c-lwc-related-list>
109+
```
110+
```
96111
//in js controller
97112
helloWorld(event) {
98113
console.log('hello world');
99114
console.log('event rows ', event.detail);
100115
}
101116
```
102117

103-
- The first button "delete all" is not going to send event to parent it will call the javascript method callApexFromButton you must
104-
implement the desired javascript/apex call based on the button label.
105-
- The button "action button" fires the event action2
106-
- The button "another action button" fires the event action3
107-
108-
The results should be :
109-
![Capture buttons](https://user-images.githubusercontent.com/39730173/158386203-bca7099f-0070-48d2-8ec9-6936a68dd754.PNG)
110-
111118
## Deploy
112119
Click Button to deploy source in Developer/Sandbox
113120

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)