@@ -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