Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

simple_tags working only with multiple-choice #159

Open
fghibellini opened this issue Feb 21, 2014 · 2 comments
Open

simple_tags working only with multiple-choice #159

fghibellini opened this issue Feb 21, 2014 · 2 comments

Comments

@fghibellini
Copy link

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.

@asaworld
Copy link

+1 to getting this fixed.

@Splaktar
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants