iam*_*box 7 select filter angularjs
我真的很惊讶没有任何文档或关于AngularJS的这种更高级的过滤机制的问题 - 它对于任何UI都是必不可少的.
我需要过滤掉包含当前项目的选择选项.问题是当我们遍历进入过滤器的项目时,如何拼出每个选项,并为要过滤的列表中的每个选择返回正确的结果.
例如,如果我们有三个地区 - 加拿大,英国,美国 - 选择输入应如下所示:
Canada
[select]
[option0] United Kingdom
[option1] United States
United Kingdom
[select]
[option0] Canada
[option1] United States
United States
[select]
[option0] Canada
[option1] United Kingdom
Run Code Online (Sandbox Code Playgroud)
.. 等等...
HTML:
<div ng-repeat="region in data.regions">
<h2> {{region.name}} </h2>
<input ui-select2="version2" type="hidden" name="keywordsLocal-{{$index}}" class="region-keywords input-xlarge" data-ng-model="data.regions[$index].keywords" required-multiple />
<select ui-select2 id="copy-{{$index}}" ng-show="region.length > 1" class="input-xlarge" ng-click="_copy($event, $index)" data-ng-model="data.regions[$index].keywords">
<option value="">Use the same keywords as:</option>
<option ng-repeat="region in data.regions | myFilter" value="{{region.keywords}}">{{region.name}}</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
app.filter('myFilter', function() {
return function(items) {
var group = [];
for (var i = 0; i < items.length; i++) {
group.push(items.name);
}
for (var i = 0; i < group.length; i++) {
group[i].splice(i, 1);
}
return group;
};
});
Run Code Online (Sandbox Code Playgroud)
选择不包括当前选择的内容存在缺陷。我将在解决方案中包含该错误,以便您可以看到它。
ng-options是您所需要的。这是使用2个单独选择的示例。
<select ng-model='selectedCountry' ng-options='country.name for country in countries'></select>
<select ng-model='selectedCountry' ng-options='country.name for country in filteredCountries()'></select>
Run Code Online (Sandbox Code Playgroud)
这是$scope碎片。请注意,filteredCountries只是返回的国家(减去所选国家)。
$scope.countries = [{name:'United States'},{name:'United Kingdom'},{name:'Canada'}];
$scope.filteredCountries = function(){
var filteredCountries = [];
angular.forEach($scope.countries, function(val, key){
if(val !== $scope.selectedCountry){
this.push(val);
}
},filteredCountries);
return filteredCountries;
};
Run Code Online (Sandbox Code Playgroud)
您将注意到的错误是,选择第二个错误后,它不会立即显示您选择的值,因为它会立即从选项中过滤掉。那不是最佳的用户体验。
工作示例:http : //plnkr.co/edit/cRBdkE3akCeBXYqfyHFO
更新: 由于您不能在AngularUI Select2指令中使用ng-options,因此这里是一个带有select 2示例的plunkr:http ://plnkr.co/edit/Hmt1OXYvtHOAZPzW7YK3?p=preview
同样,代替上面列出的filteredCountries函数,a watch会更有效。仅在检测到监视值更改时才运行代码。
$scope.$watch('version1model',function(){
$scope.filteredItems = [];
angular.forEach($scope.items, function(val, key){
if(val.id != $scope.version1model){
this.push(val);
}
},$scope.filteredItems);
});
Run Code Online (Sandbox Code Playgroud)
我想出了一个解决方案。它是一个基本过滤器,但实际上我将 ng-repeat 中索引的当前值传递给过滤器:
筛选:
app.filter('keywordFilter', function() {
return function(items, name) {
var arrayToReturn = [];
for (var i = 0; i < items.length; i++) {
if (items[i].name != name.name) {
arrayToReturn.push(items[i]);
}
}
return arrayToReturn;
};
});
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
<select ui-select2 id="copy-{{$index}}" class="input-xlarge" ng-change="_copy($index)" ng-options="region.name for region in data.regions | keywordFilter: {name: region.name}" data-ng-model="selectedKeywords">
<option value="">Use same keywords as:</option>
</select>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18522 次 |
| 最近记录: |