[英]jQuery UI Ajax Autocomplete case sensitive ASP.net MVC
在这里,我正在为搜索框使用Jquery-ui AutoComplete函数,但如果我搜索小写字母,则大写字母没有出现在建议列表中。如何添加大写和小写字母的敏感内容,以搜索此自动完成的Ajax ASP.net MVC
并且,如果有可能添加匹配文本的粗体搜索建议列表?
查看页面
<input id="app-search">
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet" />
<script>
(function() {
$("#app-search").autocomplete({
minLength: 1, //start letter search
selectFirst: true,
autoFocus: true,
source: function(request, response) {
$.ajax({
url: '@Url.Action("GetSearchType")',
type: "POST",
dataType: "json",
data: {
SearchType: @Model.SearchType,
Prefix: request.term
},
success: function(data) {
if (!data.length) {
var result = [{
label: 'No record(s) found',
value: response.term
}];
response(result);
} else {
response($.map(data.slice(0, 10), function(item) {
return {
label: item.OrganizationName,
value: item.OrganizationName
};
}))
}
}
})
},
});
});
</script>
在MVC Asp.net中的此控制器
[HttpPost]
public JsonResult GetSearchType(string Prefix)
{
List<OrganizationModel> OrganizationList = new List<OrganizationModel>()
{
new OrganizationModel {OrganizationName = "Apple" },
new OrganizationModel { OrganizationName = "name" },
new OrganizationModel { OrganizationName = "New" },
};
var CourseList = (from C in OrganizationList
where C.OrganizationName.StartsWith(Prefix)
select new { C.OrganizationName });
return Json(CourseList, JsonRequestBehavior.AllowGet);
}
您需要使用StartsWith
重载 ,并使用StringComparison类型进行指定,以便比较时不区分大小写:
C.OrganizationName.StartsWith(Prefix, StringComparison.InvariantCultureIgnoreCase)
在功能内部 添加匹配的文本加粗的自动完成建议列表可能 在此代码内
$.ui.autocomplete.prototype._renderItem = function (ul, item) {
item.label = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.