简体   繁体   English

Angular和JS过滤非标准复选框行为

[英]Angular and JS Filter upon non-standard checkboxes behavior

I would like create filters upon table data. 我想在表数据上创建过滤器。

Filter should be generic (one to many filters group) and is mixed with search field. 过滤器应该是通用的(一对多过滤器组)并与搜索字段混合。 I have data, basic logic and search working, but can not find the right way to construct function which will return filtered records. 我有数据,基本逻辑和搜索工作,但找不到正确的方法来构造将返回过滤记录的函数。 All the filters have ALL as option, it's checked during onLoad , but rest checkboxes remains unchecked (customer specification), when you check one of the filter option the ALL is unchecked. 所有过滤器都有ALL作为选项,在onLoad期间进行检查,但是当您选中其中一个过滤器选项时,取消选中ALL ,其余复选框仍未选中(客户规格)。

HTML and JS code: HTML和JS代码:

 $scope.checkedAll = [true, true]; $scope.data.items = [ {name: "Provider", value: 'provider_id', content: [ {name: 'Azure VM', value: 'azure_vm', selected: false}, {name: 'CloudForms', value: 'cloudforms', selected: false} // more providers of them... ]}, {name: "State", value: 'state_id', content: [ {name: 'Terminated', value: 'terminated', selected: false}, {name: 'On', value: 'on', selected: false}, {name: 'Off', value: 'off', selected: false} //more states ]} //more filters [] ]; $scope.data.list = [ {provider_id: "provider1", display_name: "name1", state_id: "on"}, {provider_id: "provider2", display_name: "name2", state_id: "on"}, {provider_id: "provider2", display_name: "name3", state_id: "off"}, {provider_id: "provider2", display_name: "name4", state_id: "terminated"} //much more records ]; $scope.optionClickedAll = function(index) { var checked = $scope.checkedAll[index]; if (!checked) { return; } angular.forEach($scope.data.items[index].content, function (element) { element.selected = !checked; }); } $scope.optionClicked = function(index, element) { var checked = element.selected; if (checked) { $scope.checkedAll[index] = false; } } $scope.filterSelected = function(list) { var result = []; /* var filterIds = []; var itemIds = []; for (var i = 0; i < $scope.data.items.length; ++i) { var item = $scope.data.items[i]; for (var j = 0; j < item.content.length; ++j) { var value = item.content[j]; if ($scope.checkedAll[i] || value.selected) { if (filterIds.indexOf(item.value) == -1) filterIds.push(item.value); if (itemIds.indexOf(value.value) == -1) itemIds.push(value.value); } } } */ for (var k = 0; k < list.length; ++k) { // ... return result; } } 
 <div class="col-md-2 "> <div class="filters"> <div class="search-box"> <label class="filter-label">FILTERS</label> <div class="input-field"> <input type="text" ng-model="searchInstances"> <label class="">${Search}</label> </div> </div> <div ng-repeat="item in data.items"> <label>{{item.name|uppercase}}</label> <div class="checkbox-group"> <input type="checkbox" ng-click="optionClickedAll($index)" ng-model="checkedAll[$index]" id="{{item.value}}"> <label for="{{item.value}}">All</label> <div ng-init="parentIndex = $index"> <div ng-repeat="element in item.content"> <input class="filled-in filter-all" type="checkbox" ng-click="optionClicked(parentIndex, element)" ng-model="element.selected" id="{{element.value}}"> <label for="{{element.value}}">{{element.name}}</label> </div> </div> </div> </div> </div> <div class="col-md-10 "> <table class="table"> <thead> <tr> <th>Provider</th> <th>State</th> <th>Name</th> </tr> </thead> <tbody> <tr ng-repeat="item in filterSelected(data.list) | filter:searchInstances"> <td>{{item.provider_id}}</td> <td>{{item.state_id}}</td> <td>{{item.display_namy}}</td> </tr> </table> </div> 

It's possible to change structure of $scope.data.items . 可以更改$scope.data.items结构。 Appreciate any suggestions. 感谢任何建议。

Angular supports custom filter which is probably what you want. Angular支持自定义过滤器,这可能是你想要的。 Take a look at the AngularJS documentation Creating custom filters . 查看AngularJS文档创建自定义过滤器 You will also find an example there. 你也会在那里找到一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM