我实际上正在为我正在开发的WebApp做一个过滤器,问题是我无法设法为searchQuery设置一个有效的条件。
我实际上得到了那些东西:
myApp.filter('searchFilter', function() {
return function(arrayToFilter, searchQuery, searchValue) {
var baseArray = arrayToFilter;
var usersSearchAfterQuery = [];
var _searchQueryEmpty = {
searchValue: searchValue,
program: '',
location: '',
skillLevel: {
shouldBeBeginner: false,
shouldBeIntermediate: false,
shouldBeExpert: false
}
};
/**
* true if the user has skillLevel at beginner
* @param user
* @returns {boolean}
*/
function isBeginner(user) {
return (user.skillLevel.toLowerCase() === 'beginner');
}
/**
* true if the user has skillLevel at intermediate
* @param user
* @returns {boolean}
*/
function isIntermediate(user) {
return (user.skillLevel.toLowerCase() === 'intermediate');
}
/**
* true if the user has skillLevel at expert
* @param user
* @returns {boolean}
*/
function isExpert(user) {
return (user.skillLevel.toLowerCase() === 'expert');
}
/**
* true if object1 and object2 has same program
* @param object1
* @param object2
* @returns {boolean}
*/
function hasSameProgram(object1, object2) {
return ((object1.program === object2.program));
}
/**
* true if object1 and object2 has same location
* @param object1
* @param object2
* @returns {boolean}
*/
function hasSameLocation(object1, object2) {
return ((object1.location === object2.location));
}
/**
* returns true if query1 === query2
* @param query1
* @param query2
* @returns {boolean}
*/
function queriesAreEquals(query1, query2) {
return (hasSameProgram(query1, query2)) && (hasSameLocation(query1, query2)) && (query1.skillLevel.shouldBeBeginner === query2.skillLevel.shouldBeBeginner) && (query1.skillLevel.shouldBeIntermediate === query2.skillLevel.shouldBeIntermediate) && (query1.skillLevel.shouldBeExpert === query2.skillLevel.shouldBeExpert);
}
/**
* if the filter is not empty or hasn't been reset
*/
if (!queriesAreEquals(searchQuery, _searchQueryEmpty)) {
console.log('Query is not empty');
/**
* second filter : if _user has the same values as in the filter
*/
angular.forEach(arrayToFilter, function(_user) {
if ((
searchQuery.skillLevel.shouldBeBeginner && isBeginner(_user))
|| (searchQuery.skillLevel.shouldBeIntermediate && isIntermediate(_user))
|| (searchQuery.skillLevel.shouldBeExpert && isExpert(_user)
)
&& (
hasSameLocation(_user, searchQuery)
&& hasSameProgram(_user, searchQuery)
)
) {
usersSearchAfterQuery.push(_user);
}
});
}
else if (searchValue !== null && typeof searchValue !== 'undefined' && searchValue.replace(/ /g, '') !== '') {
if (typeof arrayToFilter !== 'undefined' && (searchValue.replace(/ /g, '') !== '' || searchValue.length !== 0)) {
/**
* first filter : if _user has the same skill as in searchValue
*/
angular.forEach(arrayToFilter, function (_user) {
if (_user.skill.toLowerCase().indexOf(searchValue.toLowerCase()) > -1) {
usersSearchAfterQuery.push(_user);
}
});
}
} else {
usersSearchAfterQuery = baseArray;
}
return usersSearchAfterQuery;
};
});
因此,我必须将收到的searchQuery作为过滤器的参数进行比较,然后过滤arrayToFilter,之后视图应该只显示与查询匹配的用户。 searchValue是在搜索输入中输入的文本。
我的问题是:当我开始过滤查询时,我什么也没有显示。
我不知道自己做错了什么,或者我是否遇到了病情(我不希望这样,因为愚蠢的情况使我失去了4个小时)。
你们有些魔法药水吗?
非常感谢!
干杯。