@@ -143,7 +143,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
143143 }
144144
145145 updateDraftValues ( updateItem , fieldName ) {
146- let draftValueChanged = false ;
146+ let hasNewDraftValueRecord = false ;
147147 let copyDraftValuesCustomTypes = [ ...this . draftValuesCustomDatatypes ] ;
148148 //store changed value to do operations
149149 //on save. This will enable inline editing &
@@ -152,28 +152,34 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
152152 if ( item . Id === updateItem . Id ) {
153153 item [ fieldName ] = updateItem [ fieldName ] ;
154154
155- draftValueChanged = true ;
155+ hasNewDraftValueRecord = true ;
156156 }
157157 } ) ;
158158
159- if ( draftValueChanged ) {
159+
160+ if ( hasNewDraftValueRecord ) {
160161 this . draftValuesCustomDatatypes = [ ...copyDraftValuesCustomTypes ] ;
162+ this . draftValuesCustomDatatypes = this . mergeDraftValues ( [ ...this . draftValues , ...this . draftValuesCustomDatatypes ] ) ;
161163
162164 } else {
163165 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- } , [ ] ) ;
175166 }
176167
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+ } , [ ] ) ;
177183 }
178184
179185 fetchRecords ( ) {
@@ -191,18 +197,7 @@ export default class LwcDatatable extends NavigationMixin(LightningElement) {
191197 }
192198
193199 handleSave ( event ) {
194- const mergedValues = [ ...event . detail . draftValues , ...this . draftValuesCustomDatatypes ] . reduce ( ( merged , current ) => {
195- let found = merged . find ( val => val . Id === current . Id ) ;
196- if ( found ) {
197- // merge the current object with the existing object
198- Object . assign ( found , current ) ;
199- } else {
200- // add the current object to the merged object
201- merged . push ( current ) ;
202- }
203- return merged ;
204- } , [ ] ) ;
205-
200+ const mergedValues = this . mergeDraftValues ( [ ...event . detail . draftValues , ...this . draftValuesCustomDatatypes ] ) ;
206201
207202 const recordInputs = mergedValues . slice ( ) . map ( ( draft ) => {
208203 const fields = Object . assign ( { } , draft ) ;
0 commit comments