我使用选择的(chosen website)使选择列表更加用户友好,搜索功能只搜索一个确切的单词。
我的意思是如果列表包含
和用户类型
搜索功能
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
如何编辑此内容以允许用户搜索多个单词?
这是完整的JS函数:
AbstractChosen.prototype.winnow_results = function() {
var escapedSearchText, option, regex, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
this.no_results_clear();
results = 0;
searchText = this.get_search_text();
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
zregex = new RegExp(escapedSearchText, 'i');
regex = this.get_search_regex(escapedSearchText);
_ref = this.results_data;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i];
option.search_match = false;
results_group = null;
if (this.include_option_in_results(option)) {
if (option.group) {
option.group_match = false;
option.active_options = 0;
}
if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
results_group = this.results_data[option.group_array_index];
if (results_group.active_options === 0 && results_group.search_match) {
results += 1;
}
results_group.active_options += 1;
}
if (!(option.group && !this.group_search)) {
option.search_text = option.group ? option.label : option.text;
option.search_match = this.search_string_match(option.search_text, regex);
if (option.search_match && !option.group) {
results += 1;
}
if (option.search_match) {
if (searchText.length) {
startpos = option.search_text.search(zregex);
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
}
if (results_group != null) {
results_group.group_match = true;
}
} else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
option.search_match = true;
}
}
}
}
this.result_clear_highlight();
if (results < 1 && searchText.length) {
this.update_results_content("");
return this.no_results(searchText);
} else {
this.update_results_content(this.results_option_build());
return this.winnow_results_set_highlight();
}
};
答案 0 :(得分:1)
$(&#34; .chzn-select&#34;)。selected({&#34; search_contains&#34;:true});是搜索的解决方案,即使用户输入的内容不在文本的开头
答案 1 :(得分:0)
从纯正的regexp方法(我不知道选择),你可以尝试类似的东西:
var splitSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").split(' ');
escapedSearchText = ('^((' + splitSearchText.join('|') + ')\\s?){' + splitSearchText.length + '}$');
这是一个测试,以显示它是如何工作的(不确定全局脚本的工作原理是否相同):
var searchText = 'abc edf';
var splitSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").split(' ');
var escapedSearchText = ('^((' + splitSearchText.join('|') + ')\\s?){' + splitSearchText.length + '}$');
var zregex = new RegExp(escapedSearchText, 'i');
alert(zregex.test('abc'));
alert(zregex.test('abc bcd'));
alert(zregex.test('abc edf'));
alert(zregex.test('edf abc'));
答案 2 :(得分:-1)
您可以使用String.indexOf过滤数组:
var data = [
"Hello",
"Hello dude",
"Hello dudes",
"Hi dudes"
];
function stringContains(str, value) {
return str.indexOf(value) > -1;
}
function arrayFilter(arr, value) {
var result = [];
for (var i = 0; i < arr.length; i+=1) {
var subject = arr[i];
if (stringContains(subject, value)) {
result.push(subject);
}
}
return result;
}
console.log(arrayFilter(data, "Hello")); // ["Hello", "Hello dude", "Hello dudes"]
console.log(arrayFilter(data, "Hello dude")); // ["Hello", "Hello dude"]
如果您不关心prehistoric browsers或使用polyfills,您只需使用:
function arrayFilter(arr, value) {
return arr.filter(function (subject) {
return subject.indexOf(value) > -1;
}
}