分配唯一ID Jgrid行

时间:2010-09-24 06:42:30

标签: javascript jquery jqgrid

我正在使用Jgrid.i想要当我每次将新行分配给该行时向表中添加新行。

我正在使用

添加行
var AddNewRow = function(id){
                      var datarow = {Consultant:"",Role:"",Task:"",SDate:"",EDate:"",
                                       Deliverables:"",Complete:""};

                      var lastsel2 = i+1;
                      //debugger;
                      var su=jQuery("#test").addRowData(lastsel2, datarow, "last") ;
//                  if (su) { 
//                          jQuery('#test').editRow(lastsel2,true); 
//                                            
//                     
//                           }  


                      };

我的完整代码是......

<!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 runat="server">
    <title>Untitled Page</title>
     <!--<script type="text/javascript">

     var check=\$retrivexml;
      alert(check);
      </script>-->
    <link href="../../Content/css/Style/style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="../../Content/css/Style/jquery-ui-1.7.2.custom.css"
        type="text/css" media="screen" />
    <link rel="stylesheet" href="../../Content/css/Style/ui.jqgrid.css" type="text/css"
        media="screen" />
    <!--  <link rel="stylesheet" type="text/css" media="screen" href="../../Content/css/Style/jqModal.css" />-->
    <!--<link rel="stylesheet" type="text/css" media="screen" href="../../Content/css/Style/datePicker.css" />-->
    <!--<link href="../../Content/css/Test/jquery-ui.css" rel="stylesheet" type="text/css" />-->
    <!--<link href="../../Content/css/Test/ui.jqgrid.css" rel="stylesheet" type="text/css" />-->

    <script src="../../Content/js/Jgrid js/jquery.js" type="text/javascript"></script>
    <script src="../../Content/js/Jgrid js/grid.locale-en.js" type="text/javascript"></script>
    <script src="../../Content/js/Jgrid js/jquery.jqGrid.min.js" type="text/javascript"></script>
    <!--<script src="../../Content/js/Jgrid js/celledit.js" type="text/javascript"></script>-->
    <script src="../../Content/js/Jgrid js/ui.datepicker.js" type="text/javascript"></script>
    <script src="../../Content/js/Jgrid js/json2.js" type="text/javascript"></script>
    <script src="../../Content/js/Jgrid js/JsonXml.js" type="text/javascript"></script>
    <script src="../../Content/js/Jgrid js/grid.import.js" type="text/javascript"></script>

    <!--<script type="text/javascript" src="../../Content/js/Jgrid js/grid.base.js"></script> 
    <script type="text/javascript" src="../../Content/js/Jgrid js/grid.formedit.js"></script> -->

    <script type="text/javascript">

     jQuery(document).ready( function() {

      var i=3;  
     var grid = jQuery("#test");
            var MyExportToXml = function (grid) {
                var dataFromGrid = {row: grid.jqGrid('getGridParam', 'data') };
                var xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>\n<rows>\n' +
                              xmlJsonClass.json2xml (dataFromGrid, '\t') + '</rows>';
                alert(xmldata);
            };


    var AddNewRow = function(id){
                      var datarow = {Consultant:"",Role:"",Task:"",SDate:"",EDate:"",
                                       Deliverables:"",Complete:""};

                      var lastsel2 = i+1;
                      //debugger;
                      var su=jQuery("#test").addRowData(lastsel2, datarow, "last") ;
//                  if (su) { 
//                          jQuery('#test').editRow(lastsel2,true); 
//                                            
//                     
//                           }  


                      };

     var lastsel3;
     jQuery("#test").jqGrid({
        url:'/Content/xml/user.xml',
        //url: $!retrivexml,
     datatype: "xml",
        colNames:['Consultant','Project Role', 'Task', 'Start Date','End Date','Deliverables','Complete'],
        colModel:[
         {name:'Consultant',index:'Consultant', width:90, editable: true,edittype:"select",editoptions:{value:"K:Kin;R:Rajesh;R:Renee;S:Sandeep"}},
         {name:'Role',index:'Role', width:80, align:"right",editable:true},
         {name:'Task',index:'Task', width:80, align:"right",editable:true},
         {name:'SDate',index:'SDate', width:90,editable:true},
         {name:'EDate',index:'EDate', width:90,editable:true},
         {name:'Deliverables',index:'Deliverables', width:200, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"25"}},
         {name:'Complete',index:'Complete', width:60, editable: true,edittype:"checkbox",editoptions: {value:"Complete:Incomplete"}}  
        ],


        rowNum:10,
        //rowList:[10,20,30],
        pager: '#pagediv',
        sortname: 'id',
        viewrecords: true,
        sortorder: "desc",
        caption:"Project Planning",
     forceFit : false,
     cellEdit: true,
     editurl:'clientArray',
     cellsubmit: 'clientArray',
     height: "100%",

     ondblClickRow: function(id, iRow, iCol, e) {alert(iRow);}, 

     afterEditCell: function (id,name,val,iRow,iCol){     

        if(name=='SDate') {
       jQuery("#"+iRow+"_SDate","#test").datepicker({dateFormat:"yy-mm-dd"});

      }

     if(name=='EDate') {
       jQuery("#"+iRow+"_EDate","#test").datepicker({dateFormat:"yy-mm-dd"});
      }
     },


    });
    jQuery("#test").jqGrid('navGrid','#pagediv',{edit:false,add:false,del:false}).navButtonAdd('#pagediv', { caption:"New", buttonicon :'ui-icon-circle-plus', title: "Add New Row",
       onClickButton: function() {
           AddNewRow (grid);
       }
    });


    $("#export").click(function(){
                MyExportToXml (grid);
            });


      });
    </script>

</head>
<body>
    <form id="form1">
        <div align="center">
            <div id="pagediv">
            </div>
            <table id="test" align="center">
            </table>
            <input type="BUTTON" id="export" value="Export" />
        </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

你的代码在我看来基本上没问题。 ID可能存在的唯一潜在问题是可能与/Content/xml/user.xml中的ID冲突。您应该小心,不要在/Content/xml/user.xml中将整数ID格式化为3。

对您的代码提出更多评论。我个人认为在上下文中使用名称为lastsel2的变量并不是很好。 i范围内名称为jQuery(document).ready的变量的使用(因此该变量几乎是全局的,因为它随处可见)不是很好。稍后修改代码有时可能会重置i的值。

此外,我建议您始终在HTML验证器(例如http://validator.w3.org)中验证HTML代码,并在JSLint中验证您的JavaScript代码。

JSLint将帮助您在代码中找到一些问题,例如

newDom.loadXML('<?xml version="1.0"?><rows><page>1</page><total>1</total><records>'+
               + entries.length + '</records></rows>');

('+'将使用两次:在一行的末尾和第一行的开头)或未使用的变量,如groupingView函数内的gridCompleteJSLint可以帮助您提高代码的稳定性。

HTML Validator会向您显示XHTML代码中的其他错误,这些错误可能与您在测试Web应用程序时使用的其他浏览器版本中的错误有关:

1)runat<head>"-//W3C//DTD XHTML 1.0 Transitional//EN"元素的未知属性中。因此,您应将<head runat="server">替换为<head>

2)要避免XHTML验证错误,您应将JavaScript代码置于//<![CDATA[ //]]>块内:

<script type="text/javascript">
//<![CDATA[
  /* you can place your code here */
//]]>
</script>

3)将<input type="BUTTON" ...>替换为<input type="button" ...>,因为XHTML区分大小写。

4)将强制属性action=""添加到<form>以关注"-//W3C//DTD XHTML 1.0 Transitional//EN"。如果您不想使用表单提交,也可以将<form>替换为<fieldset>

5)在<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />元素中加入<head>

6)在<tr><td /></tr>内加<table>跟随"-//W3C//DTD XHTML 1.0 Transitional//EN"

答案 1 :(得分:1)

我用...解决了这个问题。

我计算网格中的所有行,然后在计数中加1 ..

onClickButton: function() {
    var last = jQuery('#test').jqGrid('getGridParam','records'); 
    AddNewRow (last);
}

var AddNewRow = function(id){

    var datarow = {Consultant:"",Role:"",Task:"",SDate:"",EDate:"",
                   Deliverables:"",Complete:""};

    var lastsel2 = parseInt(id) + 1;

    var su=jQuery("#test").addRowData(lastsel2, datarow, "last") ;

};