For SELECT filter ng-options, use track by instead of selectAs #3400
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.