Skip to content

Commit 60f7ed7

Browse files
committed
add configurable buttons #11
1 parent 99321d7 commit 60f7ed7

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
<template>
22
<!-- Main content -->
33
<lightning-card variant="Narrow" title={titleFormatted} icon-name={iconName}>
4-
<lightning-button label="New" title="Non-primary action" onclick={newRecord} class="slds-m-left_x-small"
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"
57
slot="actions"></lightning-button>
8+
</template>
9+
10+
<!-- custom buttons for list -->
11+
<template if:true={actionButtonsListNotEmpty}>
12+
<template for:each={actionButtonsList} for:item="actionButton" for:index="index">
13+
<lightning-button if:true={actionButton.callApex}
14+
key={actionButton.label}
15+
label={actionButton.label}
16+
title={actionButton.label}
17+
variant={actionButton.variant}
18+
onclick={callApexFromButton}
19+
class="slds-m-left_x-small"
20+
slot="actions"
21+
>
22+
</lightning-button>
23+
24+
<lightning-button if:false={actionButton.callApex}
25+
data-index={index}
26+
key={actionButton.label}
27+
label={actionButton.label}
28+
title={actionButton.label}
29+
variant={actionButton.variant}
30+
onclick={fireEventFromButton}
31+
class="slds-m-left_x-small"
32+
slot="actions"
33+
>
34+
</lightning-button>
35+
36+
</template>
37+
</template>
38+
639
<!-- Error block -->
740
<template if:true={error}>
841
<div class="slds-notify slds-notify_alert slds-theme_alert-texture slds-theme_error" role="alert">
@@ -12,9 +45,26 @@
1245
</template>
1346
<!-- Data table -->
1447
<template if:true={data}>
15-
<lightning-datatable key-field="id" data={data} columns={columns} onrowaction={handleRowAction}
48+
<!-- DATATALBE WITHOUT CHECKBOXES -->
49+
<lightning-datatable
50+
if:false={showCheckboxes}
51+
key-field="id"
52+
data={data}
53+
columns={columns}
54+
onrowaction={handleRowAction}
1655
hide-checkbox-column>
1756
</lightning-datatable>
57+
58+
<!-- DATATABLE WITH CHECKBOXES -->
59+
<lightning-datatable
60+
if:true={showCheckboxes}
61+
key-field="id"
62+
data={data}
63+
columns={columns}
64+
onrowaction={handleRowSelection}>
65+
>
66+
</lightning-datatable>
67+
1868
</template>
1969
<!-- Pagination -->
2070
<div slot="footer" style="text-align: right;">

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ export default class LightningDatatable extends NavigationMixin(
2929
@api whereClause;
3030
@api limit = 10;
3131
@api isCounterDisplayed;
32+
@api actionButtonsList; //buttons for the list
33+
@api showCheckboxes;
3234
// Private Property
3335
@track data;
3436
@track soql;
3537
@track offSet = 0;
3638
@track totalRows = 0;
3739
@track error;
40+
@track selectedRows;
3841

3942
// Do init funtion
4043
connectedCallback() {
@@ -263,4 +266,31 @@ export default class LightningDatatable extends NavigationMixin(
263266
get titleFormatted() {
264267
return (this.isCounterDisplayed) ? this.title + ` (${this.totalRows})` : this.title;
265268
}
269+
270+
callApexFromButton(event) {
271+
//add logic to call apex method based on button label (event.target.label)
272+
}
273+
274+
fireEventFromButton(event) {
275+
event.preventDefault();
276+
console.log('fireEventFromButton');
277+
278+
const buttonEvent = new CustomEvent(
279+
'action'+event.target.dataset.index,
280+
{ }
281+
);
282+
283+
// Dispatches the event.
284+
this.dispatchEvent(buttonEvent);
285+
}
286+
287+
handleRowSelection(event){
288+
this.selectedRows = event.detail.selectedRows;
289+
console.log('this.selectedRows ', JSON.stringify(this.selectedRows) );
290+
}
291+
292+
get actionButtonsListNotEmpty() {
293+
console.log('this.actionButtonsList ', this.actionButtonsList);
294+
return this.actionButtonsList && this.actionButtonsList.length > 0;
295+
}
266296
}

0 commit comments

Comments
 (0)