jqgrid 区分 dataInit 添加和编辑表单 [英] jqgrid differentiate in dataInit add and edit forms

查看:21
本文介绍了jqgrid 区分 dataInit 添加和编辑表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分 dataInit 事件是添加新数据还是编辑数据?

How I can differentiate in the dataInit event wether am I adding new data or editing it?

提前致谢.

推荐答案

这是个好问题!为此我 +1.

It's a good question! +1 from me for it.

没有直接的方法可以检测 dataInit 内部是从 Add 还是 Edit 表单调用的.如果您使用多种编辑模式(如 表单编辑内联编辑)和多种搜索模式(高级搜索 对话框和 搜索工具栏)没有直接的方法来检测控件的使用位置.

There are no direct way to detect inside of dataInit whether it is called from Add or Edit form. In the same way if you use multiple editing mode (like form editing and inline editing) and multiple searching modes (advance searching dialog and the searching toolbar) there are no direct way to detect in which place the control are used.

作为解决方法,您可以使用以下方法.您可以定义一个变量,您可以在事件内部的不同值中设置该变量,该事件将在 before 将调用 dataInit 之前在添加和编辑表单中调用.告诉信任你没有那么大的选择.这只是 beforeInitData 事件.所以你可以这样做

As the workaround you can use the following. You can define a variable which you can set in the different value inside of an event which will be called in both Add and Edit form before the dataInit will be called. To tell the trust you have not so large choice. It's the beforeInitData event only. So you can do like following

var myGrid = $("#list"),
    inEdit;

$("#list").jqGrid({
    // all parameters of the jqGrid definition
});
myGrid.jqGrid('navGrid', '#pager',
  { del: false, search: false },
  { // Edit
      recreateForm: true,
      beforeInitData: function () {
          inEdit = true;
      }
  },
  { // Add
      recreateForm: true,
      beforeInitData: function () {
          inEdit = false;
      }
  });

我还使用了 recreateForm:true 属性确保每次打开 Add 或 Edit 网格时都会创建新的表单并调用 dataInit.

I used recreateForm:true property additionally to be sure that on every opening of the Add or Edit grid the form created new and the dataInit be called.

演示中,添加表单看起来像

In the demo the Add form looks like

这里的dataInit为Notes"只写了文本in Add"在相应的控件中.Inv. No"是保存 id 的字段.添加/编辑表单的相应 <input> 控件被禁用.因此用户无法进行任何更改.在添加"对话框的情况下,方法 $.jgrid.randId() 将用于生成新的唯一 id 值.

Here the dataInit for the "Notes" write just the text "in Add" in the corresponding control. The "Inv. No" is the field which hold the id. The corresponding <input> control of the Add/Edit form is disabled. So the user can't make any changes. In case of "Add" dialog the method $.jgrid.randId() will be used to generate new unique id value.

对应的编辑表格如下图所示:

The corresponding Edit form are like on the picture below:

这篇关于jqgrid 区分 dataInit 添加和编辑表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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