Kelvin Kelvin - 7 months ago 11
PHP Question

Selected specific option in dropdown based on data retrieved from database(mysql) with ng-option

I need help with automatic selecting the option in dropdown list based on data retrieved from database. It is for editing purpose (when you editing, the data will be retrieved automatically so that you can edit). Currently, I'm able to retrieve data from database and put it in dropdown list. Btw, I'm using ng-option and the model I'm using is MVC.

<div class="col-sm-4 col-md-4">
<div class="form-group" data-ng-init="companies = <?php echo htmlspecialchars(json_encode($this->companyList)); ?>">
<label for="companyname">Company Name</label>
<select class="form-control" data-ng-options="c.id as c.company_name for c in companies track by c.id" id="companyname" name="companyname" data-ng-model="filter.company">
</select>
</div>
</div>


Initially, the dropdown list will be like this

Initially, the dropdown list will be like this

The dropdown I want

The dropdown I want

Your kind assistance is much appreciated please. Thank You.

Answer

Try doing this

<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
  <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>

Now whichever value you wish to show selected that must be your ng-model for <select> tag.