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

simple_tags working only with multiple-choice

Open fghibellini opened this issue 11 years ago • 2 comments

Theres a simple_tags option that sets the selected objects id to the model insted of the object itself. This feature doesn't seem to count with the single select variant of select2. In such case the select2 data() function returns a simple object instead of an array.

Original code:

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

/* 
Convert from Angular view-model to Select2 view-model.
*/
var convertToSelect2Model = function(angular_data) { 
    var model = [];
    if (!angular_data) {
        return model;
    }

    if (opts.simple_tags) {
        model = [];
        angular.forEach(
        angular_data,
        function(value, index) {
          model.push({'id': value, 'text': value});
        });
    } else {
        model = angular_data;
    }
    return model;
};

Possible fix:

/*
Convert from Select2 view-model to Angular view-model.
*/
with the data passed to the callback by initSelection()
var convertToAngularModel = function(select2_data) {
  var model;
  if (opts.simple_tags) {
      if ( elm.data('select2') instanceof window.Select2.class.multi ) { // alternative angular.isArray(select2_data)
        /* is multiple */
        model = [];
        angular.forEach(select2_data, function(value, index) {
          model.push(value.id);
        });
      }
      else {
         /* is single */
         model = select2_data && select2_data.id;
      }
  } else {
    model = select2_data;
  }
  return model;
};

/*
Convert from Angular view-model to Select2 view-model.
*/
var convertToSelect2Model = function(angular_data) {
  var model = [];
  if (!angular_data) {
    return model;
  }

  if (opts.simple_tags) {
      if ( elm.data('select2') instanceof window.Select2.class.multi ) { // slower alternative angular.isArray(select2_data)
        model = [];
        angular.forEach(
          angular_data,
          function(value, index) {
            model.push({'id': value, 'text': value});
          });
      } else {
          model = {'id': angular_data, 'text': angular_data};
      }
  } else {
    model = angular_data;
  }
  return model;
};

I don't know all the features of the select2 jquery plugin so i can't guarantee that this fix will work at all times, but it fixes it for the single option cases.

fghibellini avatar Feb 21 '14 15:02 fghibellini

+1 to getting this fixed.

asaworld avatar Apr 18 '14 16:04 asaworld

We're seeing this happen where we get both a String value set on the model and then an array value set on the model for each change of ui-select2.

Splaktar avatar Nov 12 '14 15:11 Splaktar