diff --git a/app/code/Magento/Ui/view/base/web/js/form/components/insert-listing.js b/app/code/Magento/Ui/view/base/web/js/form/components/insert-listing.js index 52857c94fcf1a..f967f6fe89522 100644 --- a/app/code/Magento/Ui/view/base/web/js/form/components/insert-listing.js +++ b/app/code/Magento/Ui/view/base/web/js/form/components/insert-listing.js @@ -276,7 +276,14 @@ define([ _.extend(selectionsData, this.params || {}, selections.params); if (selections[itemsType] && selections[itemsType].length) { - selectionsData.filters = {}; + /** + * In exclude mode the request is constrained by 'nin' on the excluded ids only, + * so the applied grid filters must be preserved - otherwise the whole + * unfiltered collection (minus excluded ids) would be requested. + */ + if (!selections.excludeMode) { + selectionsData.filters = {}; + } selectionsData['filters_modifier'] = {}; selectionsData['filters_modifier'][this.indexField] = { 'condition_type': filterType, diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/components/insert-listing.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/components/insert-listing.test.js new file mode 100644 index 0000000000000..142bf12896cb9 --- /dev/null +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/components/insert-listing.test.js @@ -0,0 +1,106 @@ +/** + * Copyright 2026 Adobe + * All Rights Reserved. + */ +/* eslint-disable max-nested-callbacks */ +define([ + 'Magento_Ui/js/form/components/insert-listing' +], function (Constr) { + 'use strict'; + + describe('Magento_Ui/js/form/components/insert-listing', function () { + describe('"updateFromServerData" method', function () { + var context, + requestedData; + + beforeEach(function () { + requestedData = null; + + context = { + indexField: 'entity_id', + params: {}, + requestConfig: { + method: 'POST' + }, + + /** + * Captures the payload the component would send to the server. + * + * @param {Object} data + * @returns {Object} + */ + requestData: function (data) { + requestedData = data; + + return { + /** Stub */ + done: function () { + return this; + }, + + /** Stub */ + fail: function () { + return this; + } + }; + }, + + /** Stub */ + setExternalValue: function () {}, + + /** Stub */ + loading: function () {}, + + /** Stub */ + onError: function () {} + }; + }); + + it('keeps applied grid filters when rows are excluded from the selection', function () { + var selections = { + excluded: ['1', '2'], + selected: [], + excludeMode: true, + params: { + filters: { + name: 'Bag' + }, + namespace: 'product_listing' + } + }; + + Constr.prototype.updateFromServerData.call(context, selections, 'excluded'); + + expect(requestedData.filters).toEqual({ + name: 'Bag' + }); + expect(requestedData['filters_modifier']['entity_id']).toEqual({ + 'condition_type': 'nin', + value: ['1', '2'] + }); + }); + + it('resets applied grid filters when rows are explicitly selected', function () { + var selections = { + excluded: [], + selected: ['1', '2'], + excludeMode: false, + params: { + filters: { + name: 'Bag' + }, + namespace: 'product_listing' + } + }; + + Constr.prototype.updateFromServerData.call(context, selections, 'selected'); + + expect(requestedData.filters).toEqual({}); + expect(requestedData['filters_modifier']['entity_id']).toEqual({ + 'condition_type': 'in', + value: ['1', '2'] + }); + }); + }); + }); +});