jqGrid: Disable form fields when editing
Asked Answered
P

5

17

I'm currently developing a web application designed for the administration of vending machines and such. I decided to use jQuery, jQuery UI and jqGrid for this project, so I can easily provide a great and highly customizable user interface.
Unfortunately, the jqGrid documentation is pretty outdated and doesn't cover all the features of this great plug-in (cause I do really like it, even though the documentation is rather poor).

Anyway, enough background information, I suppose. Let's get to the point:
I use the navbar which is built-in to jqGrid to Add, Edit and Delete items from the grid.
I've got this working like a charm, except for one thing: some fields may only be enabled (or visible) when adding a new item and not when in editing-mode (they should be hidden and/or disabled).

Example:
The company I'm working for sells vending towers and there are several types (different sizes and stuff) of these towers. When a new tower is added to a location and entered into the system, the type must be set. But the tower doesn't magically change over time, so this field may not be edited later on.

Does anyone know if this behavior can be accomplished by changing some initialization parameters?
Perhaps it's an undocumented edit-option (editoptions) or form-option (formoptions)?
Or maybe you've got a simple solution for this?

I'd love to hear your suggestion / solutions!
Thanks =)

Puerperal answered 4/8, 2010 at 11:26 Comment(0)
G
36

You can implement your requirements in different ways. For example, inside of beforeShowForm event you can hide or show the

jQuery("#list").jqGrid({
    colModel: [
        { name: 'Name', width: 200, editable: true },
   //...

}).jqGrid('navGrid','#pager', { edit: true, add: true, del: false},
          { // edit option
              beforeShowForm: function(form) { $('#tr_Name', form).hide(); }
          },
          { // add option
              beforeShowForm: function(form) { $('#tr_Name', form).show(); }
          });

where the id "tr_Name" is constructed from "tr_" prefix and "Name" - the name property of the column from the colModel.

UPDATED: In the answer and in another one are shown one more way how the properties can be changed dynamically immediately before the editing will be initialized.

UPDATED 2: Free jqGrid allows to define editable as callback function or as "disabled", "hidden" or "readonly". See the wiki article. It allows to implement the same requirements more easy.

Gene answered 4/8, 2010 at 13:28 Comment(8)
Thanks for the info. Additionally if you want to hide the column in the grid but show in add or edit form then use hidden: true in colModel and in beforeShowForm use show() method.Crenulate
Thanks for the solution. I have a additional small question: the hidden field in edit modal form, is set required in add modal form, so at edit the client side validation tell me: Field is required.Faker
@Cargo: Sorry, but I can't understand your question. Could you ask the question in other words? Do you need to change the value of required property of the editrules? Inside of beforeShowForm you should be able to do this.Gene
Hi Oleg, sorry for the unclear question. I have 2 fields: username and password. I wish in add new row form, both fields to be required and in edit form the password to not appear in form(to be hidden). The adding works fine but at edit the form requires to complete the password (the hidden field). Thanks a lot.Faker
@Gene In function(form) { the parameter form represents what?Spears
@CJRamki: It's jQuery wrapper of the DOM element of the form which is inside of the editing dialog. The problem which could exist in jqGrid is the following: if you would create more as one grid on the same page and two grids will have columns with the same name name: "Name" then one could have id duplicates. If you opens editing dialog in first grid and close it the form having ids tr_Name and Name will be hidden, but still exist. If you try to edit row in the second grid then ` $('#tr_Name')` will find field in the first grid and the code $('#tr_Name').hide(); will not work.Gene
@Gene thanks... Could you please take a look at this issue...https://mcmap.net/q/430451/-jqgrid-hide-column-on-view-depends-another-column-value/2567813. I referred most of your answers. But I could not get the correct solution for my issue.Spears
@CJRamki: I posted my answer on your question with the demo. The main changes are in the prefix of the row: "tr_" should be replaced to "trv_".Gene
A
8

To make the field editable or not, this is what I wound up coding after searching for an answer for a while (to disable editing on in-row edit, but allow it on 'Add') and not finding the answer I needed:

colModel :[ 
    {name:'id', index:'id', editable:false, ...

    }).navGrid("#pager",{edit:false,add:true,del:false,search:false,refresh:true},
        {}, // edit
        {   
            beforeInitData: function(formid) {
                $("#list").jqGrid('setColProp','id',{editable:true});
            },
            afterShowForm: function (formid) {
                $("#list").jqGrid('setColProp','id',{editable:false});
            },
Anacreontic answered 12/12, 2011 at 15:25 Comment(0)
P
0

Here is an example:

http://www.ok-soft-gmbh.com/jqGrid/CustomFormEdit.htm

                  beforeShowForm: function(form) {
                     $('#tr_Name', form).hide();
                  }
Polysaccharide answered 23/10, 2012 at 13:35 Comment(1)
Hi josemaria, can you tell me how can I add this aditional information label? Where Have i to put this code? Thanks.Charissecharita
P
0

Visible but not editable:

{ // edit option
    beforeShowForm: function(form) {
        $('#col_name', form).attr("disabled", true);
    }
}
Prosser answered 3/4, 2016 at 4:40 Comment(0)
M
0

This will work with the free jqgrid, plain and simple:

This particular example will allow edit only in the "add" form:

editable: function (options) {
                            // Allow edit only for "add" not for "edit"
                            if (options.mode === "addForm")
                            {
                                return true;
                            }
                            else if (options.mode === "editForm")
                            {
                                return false;
                            }
                            else
                            {
                                return false;
                            }
Mousebird answered 7/3, 2017 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.