我有两个过滤器,它们根据数据中的队列键过滤数据。 这是我的代码:
var app = angular.module('app', []);
app.controller('mainController', function($scope) {
// Data object
$scope.servers = [{
name: 'ServerA',
queue: '111'
},
{
name: 'Server7',
queue: '111'
},
{
name: 'Server2',
queue: '456'
},
{
name: 'ServerB',
queue: '456'
},
];
// Filter defaults
$scope.Filter = new Object();
$scope.Filter.queue = {
'PAV': '111',
'UAT': '456'
};
});
// Global search filter
app.filter('searchFilter', function($filter) {
return function(items, searchfilter) {
var isSearchFilterEmpty = true;
angular.forEach(searchfilter, function(searchstring) {
if (searchstring != null && searchstring != "") {
isSearchFilterEmpty = false;
}
});
if (!isSearchFilterEmpty) {
var result = [];
angular.forEach(items, function(item) {
var isFound = false;
angular.forEach(item, function(term, key) {
if (term != null && !isFound) {
term = term.toString();
term = term.toLowerCase();
angular.forEach(searchfilter, function(searchstring) {
searchstring = searchstring.toLowerCase();
if (searchstring != "" && term.indexOf(searchstring) != -1 && !isFound) {
result.push(item);
isFound = true;
}
});
}
});
});
return result;
} else {
return items;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.3/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="mainController">
<label>show 111</label>
<input type="checkbox" ng-model="Filter.queue.PAV" ng-true-value='"111"' ng-false-value='"!111"' />
<label>show 456</label>
<input type="checkbox" ng-model="Filter.queue.UAT" ng-true-value='"456"' ng-false-value='"!456"' />
<hr />
<table width="100%" cellpadding="5">
<tr>
<th>Name</th>
<th>Queue</th>
</tr>
<tr ng-repeat="server in servers | searchFilter:Filter.queue">
<td>{{server.name}}</td>
<td>{{server.queue}}</td>
</tr>
</table>
</div>
</div>
过滤器工作正常。
但是如果我有这样的数据,队列位于数组内:
$scope.servers = [
{name:'ServerA', queuearr:[{'queue' :'111'}]},
{name:'Server7', queuearr:[{'queue' :'111'}]},
{name:'Server2', queuearr:[{'queue' :'456'}]},
{name:'ServerB', queuearr:[{'queue' :'456'}]},
];
注意:队列中可以有多个对象,例如 这个:
[{queue :'111'},{queue :'278'}]
如何更改当前代码,使控件进入queuerr数组内部并匹配队列并相应地返回结果?
答案 0 :(得分:1)
我认为您可以这样做。由于您的代码过于复杂且排列不正确,因此尚不清楚您到底要做什么。
app.filter('searchFilter',function($filter) {
return function(items, searchfilter) {
const terms = Object.values(searchfilter).map(
(val)=>val.toLowerCase(),
);
return items.filter((item) =>
item.queuearr.some((q) => terms.includes(q.queue.toLowerCase())),
);
};
}
答案 1 :(得分:1)
如果要使用队列的值过滤$ scope.servers,可以尝试此操作。希望这会有所帮助。
const servers = [
{name:'ServerA', queuearr:[{'queue' :'111'}]},
{name:'Server7', queuearr:[{'queue' :'111'}]},
{name:'Server2', queuearr:[{'queue' :'456'}]},
{name:'ServerB', queuearr:[{'queue' :'456'}]},
];
const itemsToCheck = { 'PAV':'111', 'UAT':'426' };
const filter = (arr, itemsToCheck) => arr.filter((item) => {
for (let v of Object.values(itemsToCheck)) {
const found = item.queuearr.find(({ queue }) => queue === v);
if (found) return true;
}
return false;
});
console.log(filter(servers, itemsToCheck));
答案 2 :(得分:1)
Angular.forEach
中有一些条件需要更改,请看一下解决方案。
ServerB出现在两个搜索中
var app = angular.module('app', []);
app.controller('mainController', function($scope) {
// Data object
$scope.servers = [{
name: 'ServerA',
queue: '111'
},
{
name: 'Server7',
queue: '111'
},
{
name: 'Server2',
queue: '456'
},
{
name: 'ServerB',
queue: '456',
queuearr: [{
queue: '456'
}, {
queue: '111'
}]
},
];
// Filter defaults
$scope.Filter = new Object();
$scope.Filter.queue = {
'PAV': '111',
'UAT': '456'
};
});
// Global search filter
app.filter('searchFilter', function($filter) {
return function(items, searchfilter) {
var isSearchFilterEmpty = true;
angular.forEach(searchfilter, function(searchstring) {
if (searchstring != null && searchstring != "") {
isSearchFilterEmpty = false;
}
});
if (!isSearchFilterEmpty) {
var result = [];
angular.forEach(items, function(item) {
var isFound = false;
angular.forEach(item, function(term, key) {
// change here to check for arrays
if (term || Array.isArray(term) && !isFound) {
// use JSON.stringify here
term = JSON.stringify(term);
term = term.toLowerCase();
angular.forEach(searchfilter, function(searchstring) {
searchstring = searchstring.toLowerCase();
if (searchstring != "" && term.indexOf(searchstring) != -1 && !isFound) {
result.push(item);
isFound = true;
}
});
}
});
});
return result;
} else {
return items;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.3/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="mainController">
<label>show 111</label>
<input type="checkbox" ng-model="Filter.queue.PAV" ng-true-value='"111"' ng-false-value='"!111"' />
<label>show 456</label>
<input type="checkbox" ng-model="Filter.queue.UAT" ng-true-value='"456"' ng-false-value='"!456"' />
<hr />
<table width="100%" cellpadding="5">
<tr>
<th>Name</th>
<th>Queue</th>
</tr>
<tr ng-repeat="server in servers | searchFilter:Filter.queue">
<td>{{server.name}}</td>
<td>{{server.queue}}</td>
</tr>
</table>
</div>
</div>
答案 3 :(得分:0)
我通过添加另一个forEach循环(当queuearr
在数组内时通过queue
来解决了这个问题:
var app =angular.module('app', []);
app.controller('mainController', function($scope) {
// Data object
$scope.servers = [
{name:'ServerA', queuearr:[{'queue' :'111'},{'queue' :'456'}]},
{name:'Server7', queuearr:[{'queue' :'111'}]},
{name:'Server2', queuearr:[{'queue' :'456'}]},
{name:'ServerB', queuearr:[{'queue' :'456'}]},
];
// Filter defaults
$scope.Filter = new Object();
$scope.Filter.queue = {'PAV':'111',
'UAT':'456'
};
});
// Global search filter
app.filter('searchFilter',function($filter) {
return function(items,searchfilter) {
//console.log(items);
var isSearchFilterEmpty = true;
angular.forEach(searchfilter, function(searchstring) {
if(searchstring !=null && searchstring !=""){
isSearchFilterEmpty= false;
}
});
if(!isSearchFilterEmpty){
var result = [];
angular.forEach(items, function(item) {
var isFound = false;
angular.forEach(item.queuearr, function(z) {
//console.log(item.queue);
angular.forEach(z, function(term,key) {
//console.log(z);
//console.log(item.queue);
if(term != null && !isFound){
term = term.toString();
term = term.toLowerCase();
angular.forEach(searchfilter, function(searchstring) {
//console.log(searchfilter);
searchstring = searchstring.toLowerCase();
if(searchstring !="" && term.indexOf(searchstring) !=-1 && !isFound){
result.push(item);
isFound = true;
}
});
}
});
});
});
return result;
}else{
return items;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.4/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="mainController">
<label>show 111</label>
<input type="checkbox" ng-model="Filter.queue.PAV" ng-true-value='"111"' ng-false-value='""' />
<label>show 456</label>
<input type="checkbox" ng-model="Filter.queue.UAT" ng-true-value='"456"' ng-false-value='""' />
<hr />
<table width="100%" cellpadding="5">
<tr>
<th>Name</th>
</tr>
<tr ng-repeat="server in servers | searchFilter:Filter.queue">
<td>{{server.name}}</td>
</tr>
</table>
</div>
</div>
现在一切正常
答案 4 :(得分:0)
我已根据您给定的数组修改了您的代码段。它可以根据您的需要正常工作。
var app = angular.module('app', []);
app.controller('mainController', function($scope) {
// Data object
/*
$scope.servers = [{
name: 'ServerA',
queue: '111'
},
{
name: 'Server7',
queue: '111'
},
{
name: 'Server2',
queue: '456'
},
{
name: 'ServerB',
queue: '456'
},
];
*/
$scope.servers = [
{name:'ServerA', queuearr:[{'queue' :'111'}]},
{name:'Server7', queuearr:[{'queue' :'111'}]},
{name:'Server2', queuearr:[{'queue' :'456'}]},
{name:'ServerB', queuearr:[{'queue' :'456'}]},
];
// Filter defaults
$scope.Filter = new Object();
$scope.Filter.queue = {
'PAV': '111',
'UAT': '456'
};
});
// Global search filter
app.filter('searchFilter', function($filter) {
return function(items, searchfilter) {
var isSearchFilterEmpty = true;
angular.forEach(searchfilter, function(searchstring) {
if (searchstring != null && searchstring != "") {
isSearchFilterEmpty = false;
}
});
if (!isSearchFilterEmpty) {
var result = [];
angular.forEach(items, function(item) {
var isFound = false;
angular.forEach(item.queuearr, function(term, key) {
if (term.queue != null && !isFound) {
term.queue = term.queue.toString();
term.queue = term.queue.toLowerCase();
angular.forEach(searchfilter, function(searchstring) {
searchstring = searchstring.toLowerCase();
if (searchstring != "" &&
term.queue.indexOf(searchstring) != -1 &&
!isFound) {
result.push(item);
isFound = true;
}
});
}
});
});
return result;
} else {
return items;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.3/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="mainController">
<label>show 111</label>
<input type="checkbox" ng-model="Filter.queue.PAV" ng-true-value='"111"' ng-false-value='"!111"' />
<label>show 456</label>
<input type="checkbox" ng-model="Filter.queue.UAT" ng-true-value='"456"' ng-false-value='"!456"' />
<hr />
<table width="100%" cellpadding="5">
<tr>
<th>Name</th>
<th>Queue</th>
</tr>
<tr ng-repeat="server in servers | searchFilter:Filter.queue">
<td>{{server.name}}</td>
<td>{{server.queuearr[0].queue}}</td>
</tr>
</table>
</div>
</div>