Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"track by" doesn't work #31

Open
caseyjhol opened this issue Jan 23, 2015 · 4 comments
Open

"track by" doesn't work #31

caseyjhol opened this issue Jan 23, 2015 · 4 comments

Comments

@caseyjhol
Copy link

See http://codepen.io/caseyjhol/pen/dPRXBN

Example:

scope.colors = [{ id: 10, name: 'Red' }, { id: 20, name: 'Green' }, { id: 30, name: 'Blue' }];
<select selectpicker ng-model="selected" ng-options="c.name for c in colors track by c.id"></select>

When an option is selected, the value of the select is set to the object, rather than the track by property. So, selecting "Green" from the list sets the value to {id: 20, name: 'Green' }, instead of 20.

I created a workaround by implementing a trackBy attribute and altering the refresh function.

      function refresh(newVal) {
        $timeout(function () {
          if (attrs.ngOptions && /track by/.test(attrs.ngOptions)) {
            if (attrs.trackBy && newVal) {
              element.val(newVal[attrs.trackBy]);
            } else {
              element.val(newVal);
            }
          }
          element.selectpicker('refresh');
        });
      }
<select selectpicker ng-model="selected" ng-options="c.name for c in colors track by c.id" track-by="id"></select>

This isn't ideal and doesn't work when tracking by $index, but it works for me for now.

@latata
Copy link

latata commented Feb 14, 2015

To fix it you can comment out this line:
if (attrs.ngOptions && /track by/.test(attrs.ngOptions)) element.val(newVal);
http://codepen.io/anon/pen/EaQQRj
I am cursious why this line is in the code?

latata pushed a commit to latata/angular-bootstrap-select that referenced this issue Feb 14, 2015
latata pushed a commit to latata/angular-bootstrap-select that referenced this issue Feb 14, 2015
@latata
Copy link

latata commented Feb 14, 2015

Check my commits. Now it works with both:
c.name for c in newColors track by c.id
c.id as c.name for c in newColors track by c.id
Removing the line caused second example not working.

caseyjhol referenced this issue in snapappointments/ng-bootstrap-select Jul 30, 2015
@caseyjhol
Copy link
Author

Using track by and as at the same time is not supported by Angular. See https://docs.angularjs.org/api/ng/directive/ngOptions#-select-as-and-track-by-. These issues have been resolved in my fork - https://github.com/caseyjhol/ng-bootstrap-select.

@mariusstaicu
Copy link

nicela

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

No branches or pull requests

3 participants