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

simple tagging mode not working with single value selection

Open amcdnl opened this issue 10 years ago • 2 comments

When I have options on a select2 like:

  $scope.delimiterOpts = {
        minimumResultsForSearch: 1,
        data: [
            { id: "comma", text: "Comma" }, 
            { id: "tab", text: "Tab" }, 
            { id: "pipe", text: "Pipe" }, 
        ],
        // used to allow the user to add options on the fly
        createSearchChoice: function(term, data) { 
            if (!$(data).filter(function() { 
                return !this.text.localeCompare(term); 
            }).length) {
                return { id:term, text:term };
            } 
        },
        multiple: false,
        simple_tags: true
    };

the convertToAngularModel inserts the data incorrectly during the conversion...

   if (opts.simple_tags) {
            model = [];
            angular.forEach(select2_data, function(value, index) {
              model.push(value.id);
            });
      } else {
        model = select2_data;
      }

in this case it always creates a array instead of a single value. I think a good option would be to check for multiple and do

     if (opts.simple_tags) {
        if(opts.multiple){
            model = [];
            angular.forEach(select2_data, function(value, index) {
                // value.id is wrong here ...
              model.push(value.id);
            });
        } else if(select2_data) {
            model = select2_data.id;
        }
      } else {
        model = select2_data;
      }

Thoughts?

FYI- this builds on #234

amcdnl avatar Jun 25 '14 14:06 amcdnl

You should make a pull request for this. I was blocked by the same thing and replaced convertToAngularModel with your implementation and it works exactly as I would expect it to.

/*
   Convert from Select2 view-model to Angular view-model.
 */
var convertToAngularModel = function(select2_data) {
    var model;
    if (opts.simple_tags) {
        if (opts.multiple) {
            model = [];
            angular.forEach(select2_data, function (value, index) {
                model.push(value.id);
            });
        } else if(select2_data) {
            model = select2_data.id;
        }
    } else {
        model = select2_data;
    }
    return model;
};

dancrew32 avatar Aug 11 '14 21:08 dancrew32

Nice, ya I was hoping to get feedback. Usually I do these type of things and they need a tad bit of tweaking but on it!

amcdnl avatar Aug 12 '14 11:08 amcdnl