编辑/添加表格上的mvc3 jqgrid只读字段

时间:2012-12-11 17:34:26

标签: asp.net-mvc-3 jqgrid readonly

我有一个使用表单编辑的jqGrid。我希望某些字段在“编辑”模式下是Readonly,而不是在“添加”模式下。我已经尝试了一些我在其他地方找到的建议来解决这个问题(参见下面的例子),但仍然无法让它发挥作用!

如果有人可以提供帮助,我将非常感激!

在下面的网格中,我试图在“编辑”模式下将“LogonName”设置为ReadOnly,而不是“添加”模式。

$('#jpgCustomers').jqGrid({
     url: '@Url.Action("Customers")',
     datatype: 'json',
     mtype: 'POST',
     colNames: ['Name', 'FullName', 'Description'],
     colModel: [
                { name: 'LogonName', index: 'LogonName', align: 'left', width:80, editable:true, search:true, stype:'text',editrules:{required:true}},
                { name: 'FullName', index: 'FullName', align: 'left',width: 200, editable:true, search:true, stype:'text',editrules:{required:true}},
                { name: 'Description', index: 'Description', align: 'left', width: 300, editable:true, search:true, stype:'text'}
               ]

    //..

$("#jpgCustomers").jqGrid('navGrid', '#jpgpCustomers', 
        { add: true, del: true, edit: true, search: false},
        //edit form
        { width: '500', 
          editCaption: 'Edit Customer',
          url: '@Url.Action("EditCustomer")', 
          reloadAfterSubmit: true, 
          closeAfterEdit: true,
          //always start from a new form
          recreateForm: true,
          beforeShowForm: function(form) {
                  //center the edit dialog on screen
                  var dlgDiv = $("#editmod" + jpgCustomers.id);
                  CenterDialog(dlgDiv);
                  $("#jpgCustomers").jqGrid('setColProp','LogonName',{editoptions: {readonly:'readonly'}});
              }
        },
        //Add form
        { width: '500', 
          addCaption: 'Add Customer',
          url: '@Url.Action("CreateCustomer")', 
          reloadAfterSubmit: true, 
          closeAfterEdit: true,
          beforeShowForm: function(form) {
                  var dlgDiv = $("#editmod" + jpgCustomers.id);
                  CenterDialog(dlgDiv);
                  $("#jpgCustomers").jqGrid('setColProp','LogonName',{editoptions: {readonly:false}});
           }
        },
        //Delete form
        { width: '250', 
          url: '@Url.Action("DeleteCustomer")',
          beforeShowForm: function(form) {
          //center the delete dialog on screen
          var dlgDiv = $("#delmod" + jpgCustomers.id);
          CenterDialog(dlgDiv);
          //change the Delete confirmation message
          var sel_id = $("#jpgCustomers").jqGrid('getGridParam','selrow')
          $("td.delmsg", form).html("Delete User <b>" + $("#jpgCustomers").jqGrid('getCell', sel_id,'LogonName') + "</b>?");
        }
     }
 );

1 个答案:

答案 0 :(得分:0)

好的,经过几个小时的测试和重新测试,并尝试了各种各样的事情,我偶然发现了解决方案!

我对上面的代码进行了一些更改,并不完全确定哪个是问题的最重要答案,但这就是我所做的:

1)已将 beforeShowForm 中的'SetColProp'调用移至 beforeInitData 函数

2)在BeforeShowForm之前放置beforeInitData - 我不认为这实际上有所不同,但是它有效,所以我将它留在那里!

3)在“添加表单”部分中包含 recreateForm:true (我已在编辑部分中有一个)

我认为第1点和第3点有所不同,但我无法告诉你原因!

继承人工作的navGrid部分改变了问题:

$("#jpgCustomers").jqGrid('navGrid', '#jpgpCustomers', 
    { add: true, del: true, edit: true, search: false},
    //edit form
    { width: '500', 
      editCaption: 'Edit Customer',
      url: '@Url.Action("EditCustomer")', 
      reloadAfterSubmit: true, 
      closeAfterEdit: true,
      //always start from a new form
      recreateForm: true,
      beforeInitData: function(form) {
          $("#jpgCustomers").jqGrid('setColProp','LogonName',{editoptions: {readonly:'readonly'}});
      },
      beforeShowForm: function(form) {
              //center the edit dialog on screen
              var dlgDiv = $("#editmod" + jpgCustomers.id);
              CenterDialog(dlgDiv);
      }
    },
    //Add form
    { width: '500', 
      addCaption: 'Add Customer',
      url: '@Url.Action("CreateCustomer")', 
      reloadAfterSubmit: true, 
      closeAfterEdit: true,
      recreateForm: true,
      beforeInitData: function(form) {
           $("#jpgCustomers").jqGrid('setColProp','LogonName',{editoptions: {readonly:false}});
      },
      beforeShowForm: function(form) {
           var dlgDiv = $("#editmod" + jpgCustomers.id);
           CenterDialog(dlgDiv);
      }
    },
    //Delete form
    { width: '250', 
      url: '@Url.Action("DeleteCustomer")',
      beforeShowForm: function(form) {
      //center the delete dialog on screen
      var dlgDiv = $("#delmod" + jpgCustomers.id);
      CenterDialog(dlgDiv);
      //change the Delete confirmation message
      var sel_id = $("#jpgCustomers").jqGrid('getGridParam','selrow')
      $("td.delmsg", form).html("Delete User <b>" + $("#jpgCustomers").jqGrid('getCell', sel_id,'LogonName') + "</b>?");
    }
 }

);