admin管理员组文章数量:1393069
I'm using jqGrid, latest version, and when I apply a edit rule that is a custom function and perform ajax it always returns "Custom function should always return a array". I think it's a timing issue so I set the ajax to false but still have the problem. Anyone out there have a custom function that performs a ajax call which works correctly. Appreciate any help. Thank you.
jQuery(softwareReportingGrid.gridId).jqGrid({
editurl: 'clientArray',
datatype: 'json',
colNames: ["Car"],
colModel: [
{"index":"Car","name":"Car","edittype":"text","editable":true,
"editrules":{"custom":true,"custom_func":validateCar,"required":true}}
....
I have the following javascript function which is called
validateCar: function (value, colname) {
jQuery.ajax({
async: false,
url: validateCarUrl,
data: { carName: value },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data) {
return [true, '']
} else {
return [false, value + ' is not a valid car'];
}
},
error: function () { alert('Error trying to validate car ' + value); }
});
}
I'm using jqGrid, latest version, and when I apply a edit rule that is a custom function and perform ajax it always returns "Custom function should always return a array". I think it's a timing issue so I set the ajax to false but still have the problem. Anyone out there have a custom function that performs a ajax call which works correctly. Appreciate any help. Thank you.
jQuery(softwareReportingGrid.gridId).jqGrid({
editurl: 'clientArray',
datatype: 'json',
colNames: ["Car"],
colModel: [
{"index":"Car","name":"Car","edittype":"text","editable":true,
"editrules":{"custom":true,"custom_func":validateCar,"required":true}}
....
I have the following javascript function which is called
validateCar: function (value, colname) {
jQuery.ajax({
async: false,
url: validateCarUrl,
data: { carName: value },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data) {
return [true, '']
} else {
return [false, value + ' is not a valid car'];
}
},
error: function () { alert('Error trying to validate car ' + value); }
});
}
Share
Improve this question
edited Feb 28, 2012 at 22:21
Oleg
222k35 gold badges413 silver badges812 bronze badges
asked Feb 28, 2012 at 21:21
Two SeedsTwo Seeds
531 silver badge5 bronze badges
1 Answer
Reset to default 5Your validateCar()
does not return anything because AJAX is asynchronous. And even if it was, you are returning something from a function assigned as a success
handler, not from the outer validateCar()
function.
When the response from your $.ajax
arrives, the method returned long ago. You either have to use synchronous AJAX (kind of discouraged):
validateCar: function (value, colname) {
var result = null;
jQuery.ajax({
async: false, //this is crucial
url: validateCarUrl,
data: { carName: value },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data) {
result = [true, '']
} else {
result = [false, value + ' is not a valid car'];
}
},
error: function () { alert('Error trying to validate car ' + value); }
});
return result;
}
or redesign your function.
本文标签:
版权声明:本文标题:javascript - jqGrid custom edit rule function using Ajax displays "Custom Function should return array!" - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772975a2624453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论