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

查看:41
本文介绍了在 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,""];
}

推荐答案

搜索的使用方式至少有三种:工具栏搜索自定义搜索单一字段搜索高级搜索,它们共享相同的代码.所以目前有三种不同的 close things 实现.

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 不会使用.单字段搜索高级搜索 在搜索之前不要调用任何事件处理程序.在所有搜索情况下,将搜索过滤器和 jqGrid 参数 search 设置为 true,然后使用类似

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 事件处理程序并将其链接.对应的代码如下:

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天全站免登陆