Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Select2 tags initializing correctly with Angular UI when option groups are used?

I have been trying to get my angular ui select2 directive to initialize and have been unable to get it to work with option groups.

The Code:

function testCtrl1($scope)
{
    $scope.selectedOptions = ['1'];
    $scope.categories = [
            {label: 'cat1', options: [{desc: 'one', value: 1}]}, 
            {label: 'cat2', options: [{desc: 'two', value: 2}]}
    ];
}

The HTML:

<select multiple ui-select2 ng-model="selectedOptions" style="width: 300px">
    <optgroup ng-repeat="category in categories" label="{{category.label}}">
      <option ng-repeat="option in category.options" value="{{option.value}}">{{option.desc}} - {{option.value}}</option>
    </optgroup>
</select>

The Fiddle: I created the following jsfiddle.

While doing so I notice that it would initialize correctly if I included a second select2 directive that didn't include the option groups (weird). I notice some other odd behavior when including the second select2 but I am not too concerned about it since my goal is just to get testCtrl1 working.

like image 875
testing123 Avatar asked Apr 20 '13 18:04

testing123


1 Answers

Download latest angular-ui select2 and update line 24:

repeatOption = tElm.find( 'optgroup[ng-repeat], optgroup[data-ng-repeat], option[ng-repeat], option[data-ng-repeat]' );

Now its supports option groups.

like image 157
Kushan Avatar answered Sep 28 '22 04:09

Kushan