@@ -98,6 +98,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
9898 } ,
9999 ] ;
100100
101+ private _selectedItems : Array < string | null > = [ ] ;
102+ private _selectedProducts : Array < ProductDtoModel > = [ ] ;
103+
101104 constructor ( ) {
102105 super ( ) ;
103106
@@ -215,7 +218,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
215218 }
216219
217220 async #loadSelectionItems( ) {
218- this . _selection = this . data ! . selectedItemIdList ;
221+ this . _selection = this . _selectedItems . length > 0
222+ ? this . _selectedItems
223+ : this . data ! . selectedItemIdList ;
219224 this . _addUpToItems = ( this . data ?. config ?. maxItems ? this . data ?. config ?. maxItems : 0 ) - ( this . data ?. config ?. minItems ? this . data ?. config ?. minItems : 0 ) ;
220225 }
221226
@@ -227,27 +232,64 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
227232 this . #onEventRun( event ) ;
228233 }
229234
230- #onEventRun( event : UmbTableSelectedEvent | UmbTableDeselectedEvent ) {
235+ #onEventRun( event : UmbTableSelectedEvent | UmbTableDeselectedEvent ) {
236+
231237 event . stopPropagation ( ) ;
232238 const table = event . target as UmbTableElement ;
233239 const selection = table . selection ;
234240 const items = table . items ;
235- this . #collectionContext?. selection . setSelection ( selection ) ;
241+
242+ this . saveSelectedItems ( items , selection ) ;
243+
244+ this . #collectionContext?. selection . setSelection ( selection ) ;
236245
237246 this . #getSelectedProduct( selection , items ) ;
238247 this . _numberOfSelection = selection . length ;
239248 }
240249
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+
241264 #getSelectedProduct( selectedRows : Array < string > , allRows : Array < UmbTableItem > ) {
242265 let lst : Array < UmbTableItem [ ] > = [ ] ;
243266 selectedRows . forEach ( selectedRow => {
244267 const selectedProduct = allRows . filter ( r => r . id == selectedRow ) ;
245- lst . push ( selectedProduct ) ;
268+ if ( selectedProduct && selectedProduct . length > 0 ) {
269+ lst . push ( selectedProduct ) ;
270+ }
246271 } ) ;
247272
248273 let lstData = lst . map ( l => l [ 0 ] . data ) ;
249274 let lstId = lst . map ( l => l [ 0 ] . id ) ;
250275 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+ } ) ;
251293 }
252294
253295 #mapToDto( lstData : UmbTableItemData [ ] [ ] , lstId : string [ ] ) {
@@ -280,7 +322,7 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
280322 this . _rejectModal ( ) ;
281323 }
282324
283- this . value = { productList : this . _modalSelectedProducts } ;
325+ this . value = { productList : this . _selectedProducts } ;
284326 this . _submitModal ( ) ;
285327 }
286328 }
0 commit comments