Skip to content

Commit e55db5a

Browse files
Merge pull request #194 from mickaelgudin/dev
#167 fix draft merge
2 parents 5966384 + 4847b8a commit e55db5a

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
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: 30 additions & 20 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,31 +136,50 @@ 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) {
145-
let draftValueChanged = false;
146-
let copyDraftValues = [...this.draftValuesCustomDatatypes];
146+
let hasNewDraftValueRecord = false;
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

154-
draftValueChanged = true;
155+
hasNewDraftValueRecord = true;
155156
}
156157
});
157158

158-
if (draftValueChanged) {
159-
this.draftValuesCustomDatatypes = [...copyDraftValues];
159+
160+
if (hasNewDraftValueRecord) {
161+
this.draftValuesCustomDatatypes = [...copyDraftValuesCustomTypes];
162+
this.draftValuesCustomDatatypes = this.mergeDraftValues([...this.draftValues, ...this.draftValuesCustomDatatypes]);
163+
160164
} else {
161-
this.draftValuesCustomDatatypes = [...copyDraftValues, updateItem];
165+
this.draftValuesCustomDatatypes = [...copyDraftValuesCustomTypes, updateItem];
162166
}
167+
168+
this.draftValues = this.mergeDraftValues([...this.template.querySelector('c-extended-datatable').draftValues, ...this.draftValuesCustomDatatypes] );
169+
}
170+
171+
mergeDraftValues(arr) {
172+
return arr.reduce((merged, current) => {
173+
let found = merged.find(val => val.Id === current.Id);
174+
if(found) {
175+
// merge the current object with the existing object
176+
Object.assign(found, current);
177+
} else {
178+
// add the current object to the merged object
179+
merged.push(current);
180+
}
181+
return merged;
182+
}, []);
163183
}
164184

165185
fetchRecords() {
@@ -177,18 +197,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
177197
}
178198

179199
handleSave(event) {
180-
const mergedValues = [...event.detail.draftValues, ...this.draftValuesCustomDatatypes].reduce((merged, current) => {
181-
let found = merged.find(val => val.Id === current.Id);
182-
if(found) {
183-
// merge the current object with the existing object
184-
Object.assign(found, current);
185-
} else {
186-
// add the current object to the merged object
187-
merged.push(current);
188-
}
189-
return merged;
190-
}, []);
191-
200+
const mergedValues = this.mergeDraftValues([...event.detail.draftValues, ...this.draftValuesCustomDatatypes] );
192201

193202
const recordInputs = mergedValues.slice().map((draft) => {
194203
const fields = Object.assign({}, draft);
@@ -206,6 +215,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
206215
'success'
207216
);
208217
this.draftValuesCustomDatatypes = [];
218+
this.draftValues = [];
209219
this.fetchRecords();
210220
})
211221
.catch((error) => {

0 commit comments

Comments
 (0)