@@ -16,6 +16,13 @@ const actions = [
16
16
{ label : "Edit" , name : "edit" } ,
17
17
{ label : "Delete" , name : "delete" }
18
18
] ;
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
+
19
26
export default class LightningDatatable extends NavigationMixin (
20
27
LightningElement
21
28
) {
@@ -29,6 +36,7 @@ export default class LightningDatatable extends NavigationMixin(
29
36
@api whereClause ;
30
37
@api limit = 10 ;
31
38
@api isCounterDisplayed ;
39
+ @api actionButtonsToDisplay ;
32
40
@api actionButtonsList ; //buttons for the list
33
41
@api showCheckboxes ;
34
42
// Private Property
@@ -44,6 +52,40 @@ export default class LightningDatatable extends NavigationMixin(
44
52
if ( this . columns != null && this . columns != undefined ) {
45
53
cols = JSON . parse ( this . columns ) ;
46
54
}
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
+
47
89
cols . push ( {
48
90
type : "action" ,
49
91
typeAttributes : { rowActions : actions }
@@ -56,6 +98,11 @@ export default class LightningDatatable extends NavigationMixin(
56
98
this . fetchRecords ( ) ;
57
99
}
58
100
101
+ setDefaultListButtons ( ) {
102
+ this . actionButtonsList = [ ] ;
103
+ this . actionButtonsList . push ( buttonsOfList [ 'new' ] ) ;
104
+ }
105
+
59
106
fetchRecords ( ) {
60
107
getRecords ( { soql : this . soql , SObjectName : this . objectName , iconName : this . iconName } )
61
108
. then ( ( data ) => {
@@ -267,35 +314,20 @@ export default class LightningDatatable extends NavigationMixin(
267
314
return ( this . isCounterDisplayed ) ? this . title + ` (${ this . totalRows } )` : this . title ;
268
315
}
269
316
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...)
272
319
//if button has needSelectedRows set to true, have selected rows using this.selectedRows
273
320
const buttonLabel = event . target . dataset . name ;
274
- console . log ( 'callApexFromButton, button clicked has label : ' + buttonLabel ) ;
275
- }
276
321
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 ) ;
293
327
}
294
-
295
- // Dispatches the event.
296
- this . dispatchEvent ( buttonEvent ) ;
328
+ console . log ( 'callButtonAction, button clicked has label : ' + buttonLabel ) ;
297
329
}
298
-
330
+
299
331
handleRowSelection ( event ) {
300
332
this . selectedRows = JSON . parse ( JSON . stringify ( event . detail . selectedRows ) ) ;
301
333
}
0 commit comments