2014-08-27 50 views
0

我正在創建一個jqgrid,我使用下拉框創建了一個包含2列的行。下拉列表使用ajax調用進行填充。我的要求是我想複製這一行上單擊UI中的添加按鈕。例如,現在一行進入jqgrid,單擊ADD按鈕後,將顯示具有相同內容的新行,並刷新第一行中更改後的值。有沒有辦法做到這一點?我的jqgrid代碼是將數據複製到JQGrid中

$("#tbl").jqGrid('GridUnload'); 
$("#tbl").jqGrid(
     { 
      url : "/searchCriteria.do", 
      datatype : 'json', 
      colModel : 
      [ 
       {name : "test1",label : "TEST1", search : false,cellEdit:true, editable: true,edittype:"select",width:150 ,formatter: createDropDown,title:false}, 
       {name : "test2",label : "TEST2", search : false,cellEdit:true, editable: true,edittype:"select",width:150 ,formatter: createDropDown,title:false} 
      ], 
      viewrecords: true, 
      loadonce:true, 
      width: 1000, 
      multiselect : true 
     }); 
}); 

回答

1

您可以使用getLocalRow和addRowData方法的組合來實現您的功能。 Docs for these methods

讓我們在你的HTML說你有一個按鈕:

<button id="add" type="button">ADD</button> 

在JavaScript中,你可以有:

<script> 
$("#add").click(function(){ 
    var id_of_new_row = something; //you haven't specified which are the exact rows you'd like selected. 
    //you could use the getDataIDs method to get the row-IDs you're looking for 
    var new_row_data = $("#gridId").jqGrid('getLocalRow', id_of_new_row)); 
    $("#gridId").jqGrid('addRowData',id_of_new_row+1, new_row_data, "last"); 
    }); 
</script> 
+0

非常感謝。這段代碼工作得很好。 – Neha 2014-08-28 07:30:41