如何进行“预处理"?在jqGrid中显示之前的ajax响应 [英] How to "pre-process" an ajax response before displaying in jqGrid

查看:147
本文介绍了如何进行“预处理"?在jqGrid中显示之前的ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid来显示数据库查询的结果.执行查询,然后将结果格式化为XML以供jqGrid使用的php脚本也进行了一些错误检查. (例如,它可能会验证日期,以确保它们的格式正确,并且开始日期在结束日期之前..)这些类型的错误均格式化为XML,但格式为格式与成功查询不同.

I'm using jqGrid to display the results of a database query. The php script which performs the query and then formats the results as XML for consumption by jqGrid also does some error-checking. (For example, it might validate dates to be sure that they're in the correct format and that the start date is before the end date.) These kinds of errors are formatted into XML, but in a different format than a successful query.

我想要做的是拦截ajax调用的结果,并根据结果是否包含这些自定义错误之一对它进行不同的处理.如果没有错误存在,那么我想将结果加载到jqGrid中.如果出现错误,我只会在没有网格的页面上显示(因为网格设置为不同的列数).

What I want to do is to intercept the result of the ajax call and process it differently depending on whether the result contains one of these custom errors. If no error is present, then I would want to load the result in jqGrid. If there is an error, I would just display on the page without the grid (since the grid is set up for a different number of columns).

我正在寻找的是正确的进行方法(不一定是实际代码). (我的问题不是如何解析XML响应,而是如何拦截它,以便能够解析它.)我希望使用jqGrid事件(例如gridComplete或loadComplete),但这些事件似乎在之后网格已经加载.

What I'm looking for is the correct approach of how to proceed (not necessarily the actual code). (My issue is not how to parse the XML response, but how to intercept it so I am able to parse it.) I had hoped to use the jqGrid events like gridComplete or loadComplete, but these seem to fire after the grid is already loaded.

推荐答案

这是我的最终解决方案.

Here's my final solution.

我的服务器脚本以xml格式返回自定义错误:

My server script returns custom errors in xml format:

 <row>
      <code>problem</code>
      <description>End date is before start date.</description>
 </row>

所以我使用了@Oleg建议的ajaxGridOptions技术来检查错误标记,如果找到,则显示错误消息:

so I used the ajaxGridOptions technique suggested by @Oleg to check for an error tag, and if found, display the error message:

 ajaxGridOptions:   
        {dataFilter:    
            function(data,dataType){    // preprocess the data
                if ( $(data).find("code").text() == 'problem' ) {   // check for an error in the result
                    $("#list").jqGrid('GridUnload');
                    $("#errormsg").text( $(data).find("description").text() );
                }else{
                    return data;
                }
            }
        }

这篇关于如何进行“预处理"?在jqGrid中显示之前的ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆