根据另一个下拉列表的选择值,过滤并显示下拉列表的选项
Filter and display the options of a dropdown, based on the selected value of another dropdown
我在angularJS中使用ng-options创建了两个下拉列表。下拉选项由两个不同的数组type_names
和names
填充。两个数组都包含对象作为元素。下面是HTML代码
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedTypeName" ng-options="item.id as item.text for item in type_names">
</select>
<select ng-model="selectedName" ng-options="item.id as item.text for item in names|filter:{type:'others'}">
</select>
</div>
<p>This example shows how to fill a dropdown list using the ng-options directive.</p>
下面是AngularJS脚本
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.type_names = [
{id: '0', type: 'me', text: 'Master of Computer Engineering'},
{id: '1', type: 'msc', text: 'Master of Computer Science'},
{id: '2', type: 'others', text: 'Others'}
];
$scope.names = [
{id: '', type: 'choose', text: 'Choose a Program'},
{id: '0', type: 'msc', text: 'Masters - Information Systems Management'},
{id: '1', type: 'msc', text: 'Masters - Software Engineering'},
{id: '2', type: 'msc', text: 'Masters - Computer Security'},
{id: '3', type: 'others', text: 'Bachelors of Computer Science'},
{id: '4', type: 'others', text: 'Exchange Program'},
{id: '5', type: 'others', text: 'Study Abroad'},
{id: '6', type: 'others', text: 'Scientific Summer School'},
{id: '7', type: 'others', text: 'French Summer School'},
{id: '8', type: 'me', text: 'ME - Global IT Management'},
{id: '9', type: 'me', text: 'ME - Software Development and Multimedia'},
{id: '10', type: 'me', text: 'ME - Systems, Networks and Security'}
];
});
正如你所看到的,我在第二个下拉菜单中使用了filter:{type:'others'}
,所以它只会显示type: others
的选项。现在我的目标是填充类型值(例如。根据第一个下拉框中选择的选项,自动设置filter:{type:'others'}
中的me
、others
或msc
)。为了可伸缩性和健壮性,我不想在HTML(分支)代码中使用if/else条件。
注意:我正在尝试使用AngularJS与Symfony2(PHP框架)
任何想法?
方案1
可以使用第一个下拉列表中的类型作为第二个下拉列表中的过滤器:
http://plnkr.co/edit/Bxr8I3tahgzRluI4TIaW?p =
预览<select ng-model="selectedTypeName"
ng-options="item.type as item.text for item in type_names">
</select>
<select ng-model="selectedName"
ng-options="item.id as item.text for item in names|filter:{type:selectedTypeName}">
</select>
发生了什么:在第一个下拉列表中使用type:text
pair而不是id:text
pair。因此,第一个下拉列表的值将是type
而不是id
;
解决方案2
如果你想让id
作为第一个select
的值,而不是type
,那么你可以使用ngChange
:
http://plnkr.co/edit/IjcbVzSSqan8FJ98yRqA?p =
预览<select ng-model="selectedTypeId"
ng-change="selectedType=(type_names|filter:{id:selectedTypeId})[0].type"
ng-options="item.id as item.text for item in type_names">
</select>
<select ng-model="selectedName"
ng-options="item.id as item.text for item in names|filter:{type:selectedType}">
</select>
发生了什么:每当在第一个下拉菜单中选择不同的选项时,它将selectedType
更新为所选选项的类型。它用于过滤第二个下拉列表中的数据。
你可以默认下拉值(就像你做的那样:others
),通过在控制器中设置模型值:
$scope.selectedTypeId = '2';
$scope.selectedType = 'others';
简单的解决方案。
演示:小提琴
这可以很容易地完成使用过滤器。简单的技巧部分是filter: {id: '!' + fruit2.id}
<select ng-model="fruit1.id" ng-options="x.id as x.value for x in fruits | filter: {id: '!' + fruit2.id}">
</select>
<select ng-model="fruit2.id" ng-options="x.id as x.value for x in fruits | filter: {id: '!' + fruit1.id}">
</select>
控制器内部。
$scope.fruits = [
{"id":"1","value":"Apple"},
{"id":"2","value":"Banana"},
{"id":"3","value":"Cherry"},
{"id":"4","value":"Fig"},
{"id":"5","value":"Grapes"}
];
$scope.fruit1.id = "1";
- 如何使用jQuery选择下拉列表的值
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- Javascript按钮下拉列表
- jQuery表单添加不适用于下拉列表
- 使用下拉列表过滤 ng 重复,而不复制下拉选项
- 有没有办法在打开下拉列表之前过滤typeahead.js的选择
- AngularJS-我应该如何处理多个动态下拉列表来过滤列表
- 如何将过滤器添加到我的表中,以允许选择多个复选框,以及从下拉列表中进行过滤
- 使用对象数组填充,并使用jQuery过滤级联下拉列表
- 使用jquery过滤下拉列表
- 将数组结果过滤到下拉列表中
- 如何通过免费文本和多个下拉列表过滤ng-repeat
- JavaScript:根据其他下拉列表的值过滤下拉列表
- 如何使用下拉列表过滤剑道UI MVC网格
- 从下拉列表中获取过滤后的数据,并显示在另一个下拉列表中
- 所有者下拉列表未过滤故事纸板
- 过滤剑道下拉列表以删除选项
- 过滤安全Qs下拉列表
- 根据另一个下拉列表的选择值,过滤并显示下拉列表的选项
- Ng-options在另一个下拉列表中按值过滤一个数据