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