【问题标题】:mvc3 jqgrid readonly fields on edit/addform编辑/添加表单上的 mvc3 jqgrid 只读字段
【发布时间】:2012-12-11 17:34:26
【问题描述】:

我有一个使用表单编辑的 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>?");
        }
     }
 );

【问题讨论】:

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


    【解决方案1】:

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

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

    1)beforeShowForm 中对“SetColProp”的调用移至 beforeInitData 函数中

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

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

    );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      相关资源
      最近更新 更多