Skip to content

For SELECT filter ng-options, use track by instead of selectAs #3400

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

Closed
wants to merge 2 commits into from

Conversation

ravishivt
Copy link
Contributor

This fixes the dropdown view when initializing the dropdown value’s "term" or when setting the "term" value programmatically such as during table restore state.

For an example of how it affects ui-grid, please see this plnkr. For the plnkr, I've modified the Company field to be an object instead of a string and display multiple properties for that object in the column cells. I want an initial filter selection to be set so I set the term to be "{name: 'Oulu Incorporated', country: 'US'}". Although the table is properly filtered, the filter dropdown is blank. This is because Angular's ng-options determines that the term is not the identical reference to the option value. This problem is also encountered when doing a restoreState. Technically, we could fix this by adding a track by expression but AngularJS does not fully support "track by" and "selectAs" as per angular/angular.js#6564. Angular core team does not appear to be planning to fix this.

A workaround is described in that ticket, plnkr. That workaround was used for this second plnkr and contains the fix in this pull request. Note that the initial value shows up in the dropdown, the table is properly filtered on that initial value, and that other dropdown options work as well.

Please note that this is a breaking fix for existing users of dropdown SELECT filters if they are using cell values that are objects and not strings. The initializing term must be the entire selectOption object instead of just the value portion. Also, when comparing the searchTerm to the cellValue in the condition function, the searchTerm.value must be used instead of just searchTerm.

This fixes the dropdown view when initializing the dropdown value’s
"term" or when setting the "term" value programmatically such as during
table restore state.
Since searchOption now uses "track by" instead of "selectAs", the
entire searchOption object will be set as the term.  This would require
a custom condition function for every SELECT filter since you would
need to extract the term.value before comparing to the cell value.
This code extracts the .value before it does the comparison.  It also
uses angular.equals() to compare the SELECT filters by default instead
of trying to use a starts with regex.  This solves the problem of using
objects instead of strings for searchOption values.
@ravishivt
Copy link
Contributor Author

Closing this since a better implementation for searchOption filter comparison is coming.

@ravishivt ravishivt closed this May 1, 2015
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

Successfully merging this pull request may close these issues.

1 participant