0

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

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

在下面的网格中,我试图在“编辑”模式下将“登录名”设置为只读,而不是在“添加”模式下。

$('#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>?");
        }
     }
 );
4

1 回答 1

0

好的,经过数小时的测试和重新测试,并尝试了各种方法,我偶然发现了解决方案!

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

1)将对“SetColProp”的调用从beforeShowForm移到beforeInitData函数中

2)将 beforeInitData 放在 beforeShowForm 之前 - 我不认为这实际上有任何区别,但它有效,所以我把它留在那里!

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

我认为第 1 点和第 3 点有所不同,但我不能;不告诉你为什么!

继承自 The Question的工作 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>?");
    }
 }

);

于 2012-12-12T17:17:04.773 回答