javascript - jqGrid custom edit rule function using Ajax displays "Custom Function should return array!" - Sta

I'm using jqGrid, latest version, and when I apply a edit rule that is a custom function and perfo

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
Add a ment  | 

1 Answer 1

Reset to default 5

Your 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信