存在于另一个数组中的过滤器选项
Filter options which exists in another array
我有两个数组,国家和选定国家。
用户可以将国家/地区从国家/地区添加到所选国家/地区。
现在,我只想显示用户尚未选择的国家/地区(所选国家/地区中的国家/地区) - 但是我不知道如何实现这一点。
这是 HTML:
<div ng-controller="MyCtrl">
<select
ng-model="selectedCountry"
ng-options="country.name for country in countries"></select>
<button ng-click="addCountry(selectedCountry)">Add country</button>
<table>
<tr ng-repeat="country in selectedCountries">
<td>{{ country.name }}</td>
</tr>
</table>
</div>
而当前的Javascript:
function MyCtrl($scope) {
$scope.countries = [
{ id: 1, name: 'Denmark' },
{ id: 2, name: 'Sweden' },
{ id: 3, name: 'Norway' }
];
$scope.selectedCountries = [];
$scope.addCountry = function(country) {
$scope.selectedCountries.push(country);
}
}
如何筛选显示的国家/地区?
这是一个工作 JSFiddle
为它创建一个过滤器。您的网页:
ng-options="country.name for country in countries |notin:selectedCountries"
而您的过滤器:
app.filter('notin',function(){
return function(items,matchset) {
return items.filter(function(item){
return matchset.indexOf(item) < 0;
});
};
});
它会自动使它们保持最新状态。在这里更新了小提琴:http://jsfiddle.net/jxwbwjdq/1/
控制器:
$scope.notinSelectedCountries=function(item)
{
if ($scope.selectedCountries.indexOf(item) < 0)
return true
}
视图:
ng-options="country.name for country in countries | filter :notinSelectedCountries"
它不会改变数组,只需使用过滤器
在你的 js 中更改它:
$scope.addCountry = function(country) {
$scope.selectedCountries.push(country);
$scope.notselectedcountries = [];
$scope.countries.map(function(item) {
if($scope.selectedCountries.indexOf(item) < 0)
$scope.notselectedcountries.push(item);
})
}
这在你的 HTML 中:
<table ng-if="notselectedcountries.length > 0" border="1">
如果要
保持countries
完整,可以将选择绑定到函数,而不是从countries
中过滤掉selectedCountries
的列表:
在控制器中添加
$scope.unselectedCountries = function() {
return $scope.countries.filter(function(c) {
return $scope.selectedCountries.indexOf(c) == -1;
});
};
然后
<select
ng-init="selectedCountry = countries[0]"
ng-model="selectedCountry"
ng-options="country.name for country in unselectedCountries()">
</select>
更新的小提琴
为什么不在选择国家/地区时将其从$scope.countries
中删除:
$scope.addCountry = function(country) {
$scope.selectedCountries.push(country);
$scope.countries.splice($scope.countries.indexOf(country), 1);
}
使用筛选器的另一种有效方法是将国家/地区对象标记为选定状态,并筛选出未选择的国家/地区对象:
$scope.addCountry = function (country) {
$scope.selectedCountries.push(country);
country.selected = true;
}
<select ng-model="selectedCountry"
ng-options="country.name for country in countries|filter:{selected:'!true'}">
</select>
http://codepen.io/anon/pen/YPqWNo
相关文章:
- 如何定义用作AngularJS过滤器的选择的“选定”选项
- jQuery:将按钮过滤器转换为选择菜单选项
- 将过滤器添加到Angular中的选择框选项列表中
- 使用ng选项选择中的ng选项进行过滤器下拉列表
- 选择和 ng 选项过滤器
- JavaScript 数据表过滤器选项不起作用
- 在选择框中选择一个具有过滤器Angularjs的选项
- AngularJS ngOptions 搜索过滤器 - 删除重复的选择选项
- 存在于另一个数组中的过滤器选项
- 值大于(小于)变量的 jQuery 过滤器选项
- 表排序器从选择下拉菜单和自定义时间过滤器中选择多个选项
- jQuery过滤器通过文本输入选择选项
- Html在jsp中的jquery即时过滤器后单击时选择不更改选项
- 使用angular根据选定的过滤器选项填充表
- React JS中的ng-repeat过滤器选项
- 网格.MVC日期过滤器"选项不起作用
- 带有用户过滤器选项的静态表使用Javascript / CSS自定义视图
- ExtJS 网格过滤器:如何从外部 JSON 加载“列表”过滤器选项
- 在我的页面加载后自动选择一个过滤器选项
- 过滤器按钮上的同位素变化过滤器选项