@@ -98,6 +98,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
98
98
} ,
99
99
] ;
100
100
101
+ private _selectedItems : Array < string | null > = [ ] ;
102
+ private _selectedProducts : Array < ProductDtoModel > = [ ] ;
103
+
101
104
constructor ( ) {
102
105
super ( ) ;
103
106
@@ -215,7 +218,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
215
218
}
216
219
217
220
async #loadSelectionItems( ) {
218
- this . _selection = this . data ! . selectedItemIdList ;
221
+ this . _selection = this . _selectedItems . length > 0
222
+ ? this . _selectedItems
223
+ : this . data ! . selectedItemIdList ;
219
224
this . _addUpToItems = ( this . data ?. config ?. maxItems ? this . data ?. config ?. maxItems : 0 ) - ( this . data ?. config ?. minItems ? this . data ?. config ?. minItems : 0 ) ;
220
225
}
221
226
@@ -227,27 +232,64 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
227
232
this . #onEventRun( event ) ;
228
233
}
229
234
230
- #onEventRun( event : UmbTableSelectedEvent | UmbTableDeselectedEvent ) {
235
+ #onEventRun( event : UmbTableSelectedEvent | UmbTableDeselectedEvent ) {
236
+
231
237
event . stopPropagation ( ) ;
232
238
const table = event . target as UmbTableElement ;
233
239
const selection = table . selection ;
234
240
const items = table . items ;
235
- this . #collectionContext?. selection . setSelection ( selection ) ;
241
+
242
+ this . saveSelectedItems ( items , selection ) ;
243
+
244
+ this . #collectionContext?. selection . setSelection ( selection ) ;
236
245
237
246
this . #getSelectedProduct( selection , items ) ;
238
247
this . _numberOfSelection = selection . length ;
239
248
}
240
249
250
+ private saveSelectedItems ( items : UmbTableItem [ ] , selection : string [ ] ) {
251
+ // remove current table view items from the selected array to cover the deselect action.
252
+ this . _selectedItems = this . _selectedItems . filter ( obj => {
253
+ if ( ! items . some ( item => item . id == obj ) ) {
254
+ return obj ;
255
+ }
256
+ } ) ;
257
+ selection . forEach ( obj => {
258
+ if ( this . _selectedItems . indexOf ( obj ) == - 1 ) {
259
+ this . _selectedItems . push ( obj ) ;
260
+ }
261
+ } ) ;
262
+ }
263
+
241
264
#getSelectedProduct( selectedRows : Array < string > , allRows : Array < UmbTableItem > ) {
242
265
let lst : Array < UmbTableItem [ ] > = [ ] ;
243
266
selectedRows . forEach ( selectedRow => {
244
267
const selectedProduct = allRows . filter ( r => r . id == selectedRow ) ;
245
- lst . push ( selectedProduct ) ;
268
+ if ( selectedProduct && selectedProduct . length > 0 ) {
269
+ lst . push ( selectedProduct ) ;
270
+ }
246
271
} ) ;
247
272
248
273
let lstData = lst . map ( l => l [ 0 ] . data ) ;
249
274
let lstId = lst . map ( l => l [ 0 ] . id ) ;
250
275
this . _modalSelectedProducts = this . #mapToDto( lstData , lstId ) ;
276
+
277
+ this . saveSelectedProducts ( allRows ) ;
278
+ }
279
+
280
+ private saveSelectedProducts ( allRows : Array < UmbTableItem > ) {
281
+ // clear items of current table view
282
+ this . _selectedProducts = this . _selectedProducts . filter ( obj => {
283
+ if ( ! allRows . some ( row => row . id == obj . id . toString ( ) ) ) {
284
+ return obj ;
285
+ }
286
+ } ) ;
287
+
288
+ this . _modalSelectedProducts . forEach ( obj => {
289
+ if ( ! this . _selectedProducts . some ( product => product . id == obj . id ) ) {
290
+ this . _selectedProducts . push ( obj ) ;
291
+ }
292
+ } ) ;
251
293
}
252
294
253
295
#mapToDto( lstData : UmbTableItemData [ ] [ ] , lstId : string [ ] ) {
@@ -280,7 +322,7 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
280
322
this . _rejectModal ( ) ;
281
323
}
282
324
283
- this . value = { productList : this . _modalSelectedProducts } ;
325
+ this . value = { productList : this . _selectedProducts } ;
284
326
this . _submitModal ( ) ;
285
327
}
286
328
}
0 commit comments