@@ -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+
1926export 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 }
0 commit comments