Interlinked Dropdowns with options from MySQL database table
On selection of option of first dropdown list ( category ) , the options from second drop down list ( sub-category ) will change to match the selected option. ( The second drop down list is dependant on first drop down list )
<div ng-app="my_app" ng-controller="my_ctrl" >
<select ng-model="category" ng-options="x.category for x in details" ng-change="onChange()">
</select>
<select ng-model="subcategory" ng-options="x.subcategory for x in subcat | filter:check_id">
</select>
<br><br><br><br>
<span>Selected category = {{category}}</span> ,
<span>Selected cat_id = {{category.cat_id}}</span>
<br>
<span>Selected subcategory = {{subcategory}}</span> ,
<span>Selected subcat_id = {{subcategory.subcat_id}}</span>
</div>
<script>
var app = angular.module('my_app', []);
app.controller('my_ctrl', function($scope, $http) {
$http.get("dd-data.php") // data source for options
.then(function (response) {$scope.details = response.data.category_data;});
$http.get("dd-data2.php") // data source for options
.then(function (response2) {$scope.subcat = response2.data.subcategory_data;});
////
$scope.id=null;
$scope.onChange = function()
{
$scope.id=$scope.category.cat_id;
}
////
$scope.check_id = function(subcat){
if($scope.id >0){
return (subcat.cat_id == $scope.id);
}
return true // display all options if nothing is selected
};
///////
});
</script>
Using filters
On selection of options from first list, onChange event is triggered and filter is applied for selection of options for second drop down list.
DEMO: Filtering Options using functions →