15

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

Thanks in advance.

Diosney
  • 10,520
  • 15
  • 66
  • 111

1 Answers1

21

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

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.

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;
      }
  });

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

enter image description here

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:

enter image description here

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • This worked like a charm! Thanks, Oleg! I wish to have enough reputation to vote up your answer :( – Diosney Aug 09 '11 at 14:22
  • @diosney: You are welcome! If you don't forgot you can vote the old helpful answers later after you will have 15 points of reputations. Your first question was good, if you will continue so you will soon have much more reputation points as 15. I have enough reputations. The most important to vote up good questions or answers to place there higher in the searching results. So the answer could help other people too. – Oleg Aug 09 '11 at 14:37
  • @Tareq: You are welcome! I am glad to read that the answer help you. – Oleg Oct 03 '11 at 11:17
  • @Oleg: What are you ? Saved my day all over again. – Gopinagh.R Mar 28 '14 at 10:47