提交JQGrid中使用filterGrid生成的Search表单之前进行验证 [英] Validation before submitting the Search form generated using filterGrid in JQGrid

查看:69
本文介绍了提交JQGrid中使用filterGrid生成的Search表单之前进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JqGrid中的filterGrid选项生成的搜索表单.我想添加一个JavaScript逻辑,该逻辑在我提交搜索"表单之前被调用.我添加了一个方法,该方法由filterGrid的beforeSubmit属性调用.它在提交之前进入方法,但是无论返回的值如何,总是提交表单.如果JavaScript返回false,我希望表单不提交.

I have a search form I generated using the filterGrid option in JqGrid. I want to add a JavaScript logic which is invoked before I submit the Search form. I have added a method which is invoked by the beforeSubmit property for the filterGrid. It goes into the method before submitting, but always submits the form regardless of the value returned. I would like the form to not submit if the javascript returns false.

以前你们有没有实现过类似的东西.还是有其他更好的方法来实现这一目标.在这方面的任何帮助将不胜感激.

Have any of you guys implemented anything like this before. Or is there any othe rbetter way to implement this. Any help on this will be really appreciated.

代码:

$("#search").filterGrid("#resultsGrid",
                        {gridModel:true,gridNames:true,enableSearch:true,
                         formtype:"vertical",buttonclass:"submitButton",
                         enableClear:true,beforeSearch:validateDate});

function validateDate(dateDiff) {
    if(daysDiff < 0){
        return [false,"Message"];
    }
    } // ??? (commented by Oleg)
    return [true,""];
}

推荐答案

至少可以使用三种不同的搜索方式:您使用的自定义搜索单字段搜索高级搜索,它们共享相同的代码.因此,目前有三种不同的关闭方式实现.

There are at least three different ways how searching can be used: Toolbar Searching, Custom Searching which you use and Single field searching or Advanced Searching which share the same code. So one have currently three different implementations of close things.

只有工具栏搜索具有beforeSearch事件处理程序可以返回false停止搜索.如果自定义搜索事件处理程序返回的值beforeSearch 不会使用. 单字段搜索

Only Toolbar Searching has beforeSearch event handler which can return false to stop searching. In case of Custom Searching the value returned by the event handler beforeSearch will not used. Single field searching or Advanced Searching don't call any event handler before searching. In all cases for the searching will set searching filter and the jqGrid parameter search to true and then force grid reloading with the code like

$("#gridId").trigger("reloadGrid",[{page:1}]);

为了能够进行任何验证并停止重新加载网格,我发现没有简单的方法.所以我建议只关注.

To be able to make any validations and stop reloading of the grid I see no simple way. So I suggest only following.

您可以覆盖标准的reloadGrid事件处理程序并对其进行链接.相应的代码con如下所示:

You can overwrite the standard reloadGrid event handler and chain it. The corresponding code con look like following:

var grid = $("#gridId");
var events = grid.data("events"); // read all events bound to 
var originalReloadGrid; // here we will save the original event handle
var skipRefresh = false; // this can be changed by owe validation function
// Verify that one reloadGrid event hanler is set. It is typical sitation
if (events && events.reloadGrid && events.reloadGrid.length === 1) {
    originalReloadGrid = events.reloadGrid[0].handler; // save old
    grid.unbind('reloadGrid');
    var newEvents = grid.data("events");
    grid.bind('reloadGrid', function(e,opts) {
        if (!skipRefresh && grid[0].p.search) {
            originalReloadGrid(e,opts);
        }
    });
}

可能稍后我将创建一个演示,在一个示例中进行演示,并在此处放置该演示的链接.此外,我将尝试建议对jqGrid进行代码更改,以便在所有不同的实现方式中,都可以通过beforeSearch事件句柄返回false来停止搜索.

Probably I will create later a demo which demonstrate this on an example and place the link to the demo here. Moreover I will try to suggest code changes to jqGrid so, that in all different implementations of searching will be possible to stop serching by returning false by beforeSearch event handle.

已更新:好!我为您准备了演示.在演示中,我不使用任何服务器组件,因此它不会真正进行搜索,但是如果刷新网格并转到第1页,则可以看到结果.

UPDATED: OK! I prepared a demo for you. In the demo I use no server components, so it will not really do searching, but you can see the results if the grid will be refreshed and goes to the page 1.

要测试演示,您可以执行以下操作:

To test the demo you can do following:

  1. 在客户端"输入字段中输入不以"test"开头的文本,然后单击搜索"按钮.您会收到一条模拟验证对话框的警报.
  2. 在客户端"输入字段中输入以'test'开头的文本,例如test1,然后单击搜索"按钮.现在,由于验证可以,因此grig将刷新.
  1. type in the "Client" input field a text not starting with 'test' and click "search" button. You receive an alert which simulate the validation dialog.
  2. type in the "Client" input field a text starting with 'test' like test1 and click "search" button. Now the grig will refreshed because the validation will be OK.

这篇关于提交JQGrid中使用filterGrid生成的Search表单之前进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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