Objective: Arranging a default <option>
to always be displayed at the top of a <select>
dropdown upon opening.
I am making progress towards my objective. We currently have a dropdown that dynamically populates based on selected elements, with the 'Text' option correctly appearing as default. I have implemented the orderBy:
feature to sort the list alphabetically by their label
, which was our initial requirement. However, we also want the 'Text' option to consistently appear at the top when the dropdown is opened.
This is the HTML code for the dropdown:
<select ng-options="t.id as t.label for t in currentOptions | orderBy:'label'"
ng-model="rule.field"
ng-change="updateOptions(rule, $index, true)"
ng-init="updateOptions(rule, $index)" title="{{rule.label}}"
class="form-control" style="width:100%" required></select>
The structure of the currentOptions array is similar to this:
currentOptions{"Text", "Banana", "Apple", "Notebook", "Coffee", "Zebra"}
Initially, the browser displays 'Text' in the clickable field since it occupies the [0] position in the array. When the dropdown is opened, the visible list appears as follows: "Apple" "Banana" "Coffee" "Notebook" "Text" "Zebra"
Our desired outcome is for the visible list to display as: "Text" "Apple" "Banana" etc.