Skip to content

Commit 0c1d6a5

Browse files
committed
#167 #192 fix merge drafts +allow empty lookup
1 parent 5966384 commit 0c1d6a5

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export default class DatatableLookup extends LightningElement {
110110

111111
//value of formatted url
112112
get lookupValue() {
113+
if(!this.value) return '';
114+
113115
return (this.record.data != null && this.record.data.fields[this.getFieldName()].value) ? '/' + this.value : '';
114116
}
115117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
onrowaction={handleRowAction}
6161
onrowselection={handleRowSelection}
6262
onsave={handleSave}
63-
draft-values={draftValuesCustomDatatypes}
63+
draft-values={draftValues}
6464
sorted-direction={sortDirection}
6565
onsort={handleSort}
6666
sorted-by={sortBy}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
6060
@track colsJson;
6161
@track searchTerm;
6262

63+
draftValues = []; // don't touch
6364
draftValuesCustomDatatypes = [];
6465
labels = {
6566
recordUpdatedSuccessMessage,
@@ -135,19 +136,19 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
135136
this.updateDraftValues(
136137
{
137138
Id: dataReceived.context,
138-
[dataReceived.fieldName]: dataReceived.value
139+
[dataReceived.fieldName]: (dataReceived.value) ? dataReceived.value : null
139140
},
140141
dataReceived.fieldName
141142
);
142143
}
143144

144145
updateDraftValues(updateItem, fieldName) {
145146
let draftValueChanged = false;
146-
let copyDraftValues = [...this.draftValuesCustomDatatypes];
147+
let copyDraftValuesCustomTypes = [...this.draftValuesCustomDatatypes];
147148
//store changed value to do operations
148149
//on save. This will enable inline editing &
149150
//show standard cancel & save button
150-
copyDraftValues.forEach((item) => {
151+
copyDraftValuesCustomTypes.forEach((item) => {
151152
if (item.Id === updateItem.Id) {
152153
item[fieldName] = updateItem[fieldName];
153154

@@ -156,10 +157,23 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
156157
});
157158

158159
if (draftValueChanged) {
159-
this.draftValuesCustomDatatypes = [...copyDraftValues];
160+
this.draftValuesCustomDatatypes = [...copyDraftValuesCustomTypes];
161+
160162
} else {
161-
this.draftValuesCustomDatatypes = [...copyDraftValues, updateItem];
163+
this.draftValuesCustomDatatypes = [...copyDraftValuesCustomTypes, updateItem];
164+
this.draftValuesCustomDatatypes = [...this.draftValues, ...this.draftValuesCustomDatatypes].reduce((merged, current) => {
165+
let found = merged.find(val => val.Id === current.Id);
166+
if(found) {
167+
// merge the current object with the existing object
168+
Object.assign(found, current);
169+
} else {
170+
// add the current object to the merged object
171+
merged.push(current);
172+
}
173+
return merged;
174+
}, []);
162175
}
176+
163177
}
164178

165179
fetchRecords() {
@@ -206,6 +220,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
206220
'success'
207221
);
208222
this.draftValuesCustomDatatypes = [];
223+
this.draftValues = [];
209224
this.fetchRecords();
210225
})
211226
.catch((error) => {

0 commit comments

Comments
 (0)