为什么内联编辑添加新行?

问题描述:

在内联模式中,我添加一个新行,编辑新值,然后单击“保存”按钮到服务器和本地。但是,当我继续单击'编辑'按钮来编辑新行而不是编辑行时,它会添加一个新行。例如,首先添加一个新行:{ aa,bb,cc},然后通过单击'编辑'按钮并对其进行编辑将其更改为{ aaaa,bb,cc},但jqGrid将oper=add发送到服务器,这会导致添加新行。我不明白为什么?为什么内联编辑添加新行?

代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>ddddd</title> 
<link 
href="/education2/jqGrid4.4/themes/redmond/jquery-ui-1.8.2.custom.css" 
rel="Stylesheet" /> 
<link href="/education2/jqGrid4.4/themes/ui.jqgrid.css" rel="Stylesheet" /> 
<script src="/education2/jqGrid4.4/js/jquery-1.7.2.js" 
type="text/javascript"></script> 
<script type="text/javascript" 
src="/education2/jqGrid4.4/js/jquery-ui-1.8.2.custom.min.js"></script> 
<script type="text/javascript" 
src="/education2/jqGrid4.4/js/i18n/grid.locale-cn.js"></script> 

<script src="/education2/jqGrid4.4/js/jquery.jqGrid.src.js" 
type="text/javascript"></script> 

<script type="text/javascript"> 
jQuery(document).ready(function() { 
    jQuery("#gridTable").jqGrid({ 
     url : '/education2/json/searchStudent', 
     mtype : "POST", 
     datatype : "json", 
     colNames : [ 'studentId','studentNo', 'name', 'class'], 
     colModel : [ { 
      name : 'studentId', 
      index : 'studentId', 
      width : 55, 
      hidden : true, 
      hidedlg : true 
      key : true, 
      editable : true 
     }, { 
      name : 'studentNo', 
      index : 'studentNo', 
      width : 150, 
      editable : true   
     }, { 
      name : 'studentName', 
      index : 'studentName', 
      width : 150, 
      align : "right", 
      editable : true 
     }, { 
      name : 'className', 
      index : 'className', 
      width : 150, 
      align : "right", 
      editable : true, 
      edittype : 'select', 
      editoptions : { 
       dataUrl : "/education2/json/classNameStudent" 
      } 
     }], 
     jsonReader : { 
      root : "gridModel", 
      records : "record", 
      repeatitems : false 
     }, 
     prmNames : { 
      search : "search", 
      id : "id" 
     }, 
     rowNum : 10, 
     rowList : [ 10, 20, 30 ], 
     height : 400, 
     //   multiselect : true, 
     //   multiboxonly : true, 
     pager : jQuery('#gridPager'), 
     sortname : 'studentId', 
     viewrecords : true, 
     altRows : true, 
     sortorder : "desc", 
     editurl : "/education2/json/editStudent", 
     caption : "student" 
    }); 

    jQuery("#gridTable").jqGrid('navGrid', "#gridPager", { 
     edit : false, 
     add : false, 
     del : true 
    }, {}, {}, {}, { 
     caption : "find", 
     Find : "find", 
     closeAfterSearch : true 
    }); 

    jQuery("#gridTable").jqGrid('inlineNav', "#gridPager", { 

     editParams : { 
      successfunc : succesfunc1, 
      restoreAfterError : false 
     } 
    }); 
}); 

var succesfunc1 = function(response) { 
    var result = eval('(' + response.responseText + ')'); 
    if (result.success == true) { 
     alert("success!"); 
     return true; 
    } else { 
     alert(result.message); 
     return false; 
    } 
}; 


}; 
</script> 
</head> 
<body> 

<table id="gridTable"></table> 
<div id="gridPager"></div> 
<br /> 

</body> 
</html> 

UPDATE

我用$('#'+$.jgrid.jpID(rowid).attr('id', result1.new_id)刷新新行的行ID(想法来自@oleg问题“如何更新后列中内嵌的jqGrid添加“),并在服务器上使用oper=new && id !="new_row"来决定是编辑还是添加新行。它运行良好,但我必须添加一个aftersavefunc方法。

上面的问题我不太了解。我所做的只是绕过这个问题。

我的问题是,我:添加一个新行然后改变它,jqgrid不会改变它,jqgrid发送一个"oper=add"到服务器,这意味着服务器将添加一个新的行。

的另一个问题是:

jQuery("#gridTable").jqGrid('inlineNav', "#gridPager", { 

    addParams : { 
     addRowParams : { 
      keys : true, 
      url : '/education2/json/editStudent1?inlinePoer=add' 
     } 
    }, 
    editParams : { 
     url : '/education2/json/editStudent?inlinePoer=edit', 
    } 
}); 

不像我期望的那样,每当我点击添加,编辑或内嵌导航器中的保存按钮,只有网址:'/education2/json/editStudent?inlinePoer=edit'被激发。看来url:'/education2/json/editStudent1?inlinePoer=add'不能被解雇,为什么?有人能帮助我吗?

我用萤火虫和IE。

+0

您是否在编辑行键(StudentId)的列? – 2012-08-07 12:59:32

+0

是的,我编辑并将数据保存到服务器 – llpllpllp 2012-08-07 17:55:47

+0

因此,如果您编辑您的密钥,您正在创建一个新的记录。如果您不在网格列上声明密钥,它将使用自动生成的rownumbers作为可以解决您的问题的密钥。 – 2012-08-07 17:57:00

我想你错过了什么是给编辑:真正的在下面一行

jQuery("#gridTable").jqGrid('inlineNav', "#gridPager", { 

     editParams : { 
      successfunc : succesfunc1, 
      restoreAfterError : false 
     } 
    }); 

检查此链接

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#inlinenav,并期待在这里的参数。我不确定它只是试图给予编辑为真,如果它工作正常,很好......否则当我完成我的办公室工作时,我会查看你的代码。

+0

感谢您的帮助,但添加编辑:true是无用的。 我发现添加的行的ID是“new_row”,但我认为它将是一个int值int studentId, 来自服务器的数据是{“id”:“28”,“message”:“success !“,”new_id“:28,”success“:true},也许这个返回值不是jqgrid的标准返回值?有人能帮助我吗?谢谢 ! – llpllpllp 2012-08-07 16:12:29

+0

以及我可以向你保证,这jqgrid行和学生ID行号将永远不会是一样的。 – 2012-08-07 16:14:50

+0

和jqgrid网站只给我们应该发送到服务器,但我无法找出什么数据应该从服务器返回。我在哪里可以找到关于什么数据应该从服务器返回关于jqgrid的文档?谢谢 ! – llpllpllp 2012-08-07 16:17:59