JQgrid 与 jquery UI 自动完成验证
我所拥有的是 jqgrid,它在列中使用 jQueryUI 自动完成功能。一切都很完美,除了一件小事,如果用户选择写“fsdfsdfsfasf”并按回车键,我的程序当然会惊慌失措并告诉我这样的项目不存在。
我想要的是进行某种验证,因此如果用户输入自动完成列表中不存在的某些数据,程序应自动将自动完成文本设置为 ""
就像这里一样: http://view.jquery.com/trunk/plugins/autocomplete/demo/ 在“月”字段。
由于我使用的是 jQueryUI 自动完成而不是 jquery 自动完成,我无法使用 mustmatch
选项。
{
name: 'Brand',
index: 'Brand',
align: 'left',
width: 50,
sortable: true,
editable: true,
edittype: 'text',
editoptions: {
dataInit:
function (elem) {
$(elem).autocomplete({
delay: 0,
minLength: 0,
source: '@Url.Action("GetBrands")',
minChars: 0,
max: 12,
autoFill: true,
mustMatch: true,
matchContains: false,
scrollHeight: 220,
formatItem: function(data, i, total) {
return data[0];
},
select: function (event, ui) {
if (ui.item.value == "Opret ny Brand...") {
$(function () {
var dialogDiv = $('#dialog');
var viewUrl = '@Url.Action("CreateBrand")';
$.get(viewUrl, function (data) {
dialogDiv.html(data);
var $form = $("#updateCarForm");
$form.unbind();
$form.data("validator", null);
//Check document for changes
$.validator.unobtrusive.parse(document);
//Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
//open dialog
dialogDiv.dialog('open');
});
});
}
}
})
.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a><span style='display:inline-block;width:60px;'><b>" +
item.value + "</b></span></a>")
.appendTo(ul);
};
}
}
},
What I have is jqgrid which uses jQueryUI autocomplete in a column. Everything works perfect except 1 little thing, if user choose to write "fsdfsdfsfasf" and press enter my program will of course freak out and tell me that such item does not exist.
What I want is to make some kind of validation, so if user enter some data which does not exist in autocomplete list program should automatically put autocomplete text to be ""
just like here: http://view.jquery.com/trunk/plugins/autocomplete/demo/ in the "month" field.
Since I am using jQueryUI autocomplete and not jquery autocomplete i cant use the mustmatch
option.
{
name: 'Brand',
index: 'Brand',
align: 'left',
width: 50,
sortable: true,
editable: true,
edittype: 'text',
editoptions: {
dataInit:
function (elem) {
$(elem).autocomplete({
delay: 0,
minLength: 0,
source: '@Url.Action("GetBrands")',
minChars: 0,
max: 12,
autoFill: true,
mustMatch: true,
matchContains: false,
scrollHeight: 220,
formatItem: function(data, i, total) {
return data[0];
},
select: function (event, ui) {
if (ui.item.value == "Opret ny Brand...") {
$(function () {
var dialogDiv = $('#dialog');
var viewUrl = '@Url.Action("CreateBrand")';
$.get(viewUrl, function (data) {
dialogDiv.html(data);
var $form = $("#updateCarForm");
$form.unbind();
$form.data("validator", null);
//Check document for changes
$.validator.unobtrusive.parse(document);
//Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
//open dialog
dialogDiv.dialog('open');
});
});
}
}
})
.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a><span style='display:inline-block;width:60px;'><b>" +
item.value + "</b></span></a>")
.appendTo(ul);
};
}
}
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过提供一个函数作为自动完成的
source
参数的参数并自行执行 AJAX 请求来实现此功能。类似于:(未经测试。您可能需要调整 AJAX 调用)
如果需要,我可以提供远程示例,但概念与使用本地数据源的示例相同:http://jsfiddle.net/andrewwhitaker/Dgfgr/
You can implement this functionality by providing a function as an argument to the
source
parameter of autocomplete and doing the AJAX request yourself. Something like:(Untested. You may need to tweak the AJAX call)
I can provide a remote example if necessary, but the concept is the same with an example using a local data source: http://jsfiddle.net/andrewwhitaker/Dgfgr/