angular-select2 icon indicating copy to clipboard operation
angular-select2 copied to clipboard

Losing selection in multiple with custom query

Open piernik opened this issue 10 years ago • 0 comments

If using multiple with custom query it's llosing selection. in getSelection You call custom query function to fetch options. But often those option are defferent because of ajax. and earlier selected options are gone. That's why You have to store previous selection Change:

function getSelection(callback) {
    if (isMultiple) {
        getOptions(function (options) {
            var selection = [];
            for (var i = 0; i < options.length; i++) {
                var option = options[i];
                var viewValue = controller.$viewValue || [];
                if (viewValue.indexOf(option.id) > -1) {
                    selection.push(option);
                }
            }
            callback(selection);
        });
    } else {
        getOptions(function () {
            callback(optionItems[controller.$viewValue] || {obj: {}});
        });
    }
}

To:

var previousSelection = [];

function getSelection(callback) {
    if (isMultiple) {
        getOptions(function (options) {
            var selection = [];
            options = options.concat(previousSelection || []);
            for (var i = 0; i < options.length; i++) {
                var option = options[i];
                var viewValue = controller.$viewValue || [];
                if (viewValue.indexOf(option.id) > -1) {
                    selection.push(option);
                }
            }
            previousSelection = angular.copy(selection);
            callback(selection);
        });
    } else {
        getOptions(function () {
            callback(optionItems[controller.$viewValue] || {obj: {}});
        });
    }
}

piernik avatar Oct 16 '15 09:10 piernik