使用Ajax显示与QUOT

使用Ajax显示与QUOT

本文介绍了使用Ajax显示与QUOT jqGrid的自定义编辑规则功能;自定义函数返回数组&QUOT!;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的jqGrid的,最新的版本,当我应用的编辑规则,是一个自定义函数和执行Ajax它总是返回自定义函数应该总是返回一个数组。我认为这是一个时间问题,所以我设置了ajax为false,但仍然有问题。任何人在那里有一个自定义函数来执行一个AJAX调用它正常工作。鸭preciate任何帮助。谢谢。

 的jQuery(softwareReportingGrid.gridId).jqGrid({
    editurl:clientArray,
    数据类型:JSON,
    colNames:汽车],
    colModel:
        {指数:汽车,名:汽车,edittype:文,编辑:真实,
            editrules:{自定义:真实,custom_func:validateCar,要求:真}}
    ....
 

我有以下的javascript函数被称为

  validateCar:函数(值,colname需要){
    jQuery.ajax({
        异步:假的,
        网址:validateCarUrl,
        数据:{carName:值},
        数据类型:JSON,
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:功能(数据){
            如果(数据){
                返回[真,'']
            } 其他 {
                返回[假的,值+'不是一个有效的车'];
            }
        },
        错误:函数(){警报(错误试图验证车+值); }
    });
}
 

解决方案

validateCar()不返回任何东西,因为AJAX是异步的。而且即使是,你正在返回从指定为成功处理程序,而不是从外部 validateCar()功能。

当从 $。AJAX响应到达时,该方法早就回来了。你要么必须使用同步AJAX(一种灰心):

  validateCar:函数(值,colname需要){
    VAR的结果= NULL;
    jQuery.ajax({
        异步:假的,//这是关键
        网址:validateCarUrl,
        数据:{carName:值},
        数据类型:JSON,
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:功能(数据){
            如果(数据){
                结果= [真,'']
            } 其他 {
                结果=假,值+'不是一个有效的车'];
            }
        },
        错误:函数(){警报(错误试图验证车+值); }
    });
    返回结果;
}
 

或重新设计你的函数。

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); }
    });
}
解决方案

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.

这篇关于使用Ajax显示与QUOT jqGrid的自定义编辑规则功能;自定义函数返回数组&QUOT!;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:27