Angularjs 自定义角度搜索筛选器按匹配的字数排序
我想在angular JS中创建一个预测文本自定义过滤器,它根据搜索查询过滤出一个字符串数组,并根据与搜索查询中的单词匹配的单词数对它们进行排序 即使搜索查询中的单词顺序与它们在记录数组中出现的顺序不匹配,过滤器也应该返回相同的和所有的记录。(例如,如果我搜索“桌布”作为搜索查询,它也应该能够与“装饰桌布”匹配(请注意,角度过滤器不这样做))Angularjs 自定义角度搜索筛选器按匹配的字数排序,angularjs,search,angularjs-ng-repeat,angularjs-filter,Angularjs,Search,Angularjs Ng Repeat,Angularjs Filter,我想在angular JS中创建一个预测文本自定义过滤器,它根据搜索查询过滤出一个字符串数组,并根据与搜索查询中的单词匹配的单词数对它们进行排序 即使搜索查询中的单词顺序与它们在记录数组中出现的顺序不匹配,过滤器也应该返回相同的和所有的记录。(例如,如果我搜索“桌布”作为搜索查询,它也应该能够与“装饰桌布”匹配(请注意,角度过滤器不这样做)) 此外,应该只允许搜索查询中单词的前缀匹配(例如,如果我有一个包含单词“phone”的记录,如果我在搜索查询中不应该显示的某些地方键入“one”。我自己编写
此外,应该只允许搜索查询中单词的前缀匹配(例如,如果我有一个包含单词“phone”的记录,如果我在搜索查询中不应该显示的某些地方键入“one”。我自己编写了一个自定义过滤器,它可以执行以下操作
- {{helpFAQ.question}
- […]
AngularJS自定义过滤器
vm.helpFAQs = [{"question":"How can i hide an site?",
"priority":1},
{"question":"How can i stop an ad?",
"priority":7},
{"question":"How do i change my email address?",
"priority":9},
{"question":"How do i pay electricity bill?",
"priority":3},
{"question":"How do i see the number of times I logged in?",
"priority":6},
{"question":"How do i set up a new connection?",
"priority":3},
{"question":"Where can i see disclosures for my account?",
"priority":1},
{"question":"Where can i see my privacy setting?",
"priority":4},
{"question":"Why can't i see my profile?",
"priority":2}
];
vm.serchBoxKeyed = function(searchTerm){
vm.regex = new RegExp('\\b' + vm.escapeRegExp(searchTerm), 'i');
}
vm.filterBySearch = function(topic) {
topic.matchCount = 0;
var searchTextSplit = vm.searchQuery.toLowerCase().split(' ');
for(var y = 0; y < searchTextSplit.length; y++){
vm.serchBoxKeyed(searchTextSplit[y]);
var prefixMatch =vm.regex.test(topic.question);
if(topic.question.toLowerCase().indexOf(searchTextSplit[y]) !== -1 && prefixMatch){
topic.matchCount = topic.matchCount + 1;
}
}
if(topic.matchCount>0)
return true;
else
return false;
};
vm.escapeRegExp = function(searchTerm){
return searchTerm.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
vm.helpFAQs=[{“问题”:“如何隐藏网站?”,
“优先权”:1},
{“问题”:“我怎样才能阻止广告?”,
“优先权”:7},
{“问题”:“我如何更改我的电子邮件地址?”,
“优先权”:9},
{“问题”:“我如何支付电费?”,
“优先权”:3},
{“问题”:“如何查看我的登录次数?”,
“优先权”:6},
{“问题”:“如何建立新连接?”,
“优先权”:3},
{“问题”:“我在哪里可以看到我的账户披露?”,
“优先权”:1},
{“问题”:“我在哪里可以看到我的隐私设置?”,
“优先权”:4},
{“问题”:“为什么我看不到我的个人资料?”,
“优先权”:2}
];
vm.serchBoxKeyed=函数(searchTerm){
vm.regex=newregexp('\\b'+vm.escapeRegExp(searchTerm),'i');
}
vm.filterBySearch=函数(主题){
topic.matchCount=0;
var searchTextSplit=vm.searchQuery.toLowerCase().split(“”);
对于(变量y=0;y0)
返回true;
其他的
返回false;
};
vm.escapeRegExp=函数(searchTerm){
返回searchTerm.replace(/([.*+?^=!:${}()\[\]\/\])/g,“\\$1”);
}
你能告诉我们你试过什么吗?我发布了答案。
vm.helpFAQs = [{"question":"How can i hide an site?",
"priority":1},
{"question":"How can i stop an ad?",
"priority":7},
{"question":"How do i change my email address?",
"priority":9},
{"question":"How do i pay electricity bill?",
"priority":3},
{"question":"How do i see the number of times I logged in?",
"priority":6},
{"question":"How do i set up a new connection?",
"priority":3},
{"question":"Where can i see disclosures for my account?",
"priority":1},
{"question":"Where can i see my privacy setting?",
"priority":4},
{"question":"Why can't i see my profile?",
"priority":2}
];
vm.serchBoxKeyed = function(searchTerm){
vm.regex = new RegExp('\\b' + vm.escapeRegExp(searchTerm), 'i');
}
vm.filterBySearch = function(topic) {
topic.matchCount = 0;
var searchTextSplit = vm.searchQuery.toLowerCase().split(' ');
for(var y = 0; y < searchTextSplit.length; y++){
vm.serchBoxKeyed(searchTextSplit[y]);
var prefixMatch =vm.regex.test(topic.question);
if(topic.question.toLowerCase().indexOf(searchTextSplit[y]) !== -1 && prefixMatch){
topic.matchCount = topic.matchCount + 1;
}
}
if(topic.matchCount>0)
return true;
else
return false;
};
vm.escapeRegExp = function(searchTerm){
return searchTerm.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}