For SELECT filter ng-options, use track by instead of selectAs #3408
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 this is a problem in ui-grid, please see this plnkr. For the plnkr, I've modified the Company field to be an object instead of a string and use the object to display multiple object properties in that column. A SELECT filter is in place and it works but say I wanted an initial filter 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 an identical reference to the option value. This problem is also encountered when doing a restoreState since again, ng-options will not detect the restored value to be the same as an existing option. 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, which involves using "track by" instead of "selectAs" and then adjusting for the changes in the code. 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.
Due to this change, the term that is set in ng-model now will be the full selectOption object with value and label properties instead of just the .value. This affects the comparison when the filters are run. Therefore, code was added to A) extract the .value property before doing the filter comparison and B) set up a default condition function for SELECT filters instead of using the default startswithRE regex. B helps when the selectOption .value is an object and not a string.