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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772975a4592873.html
评论列表(0条)