2012-08-08 110 views
0

我有兩個jqGrid(「in_table」和「out_table」)除了它們的數據以外都是相同的。感謝我在this post收到的幫助,現在我明白如何添加可定製的按鈕。當按下按鈕時,我想從表格中刪除該行並將其添加到另一個。從一個jqGrid中刪除行並將其添加到另一個

下面的代碼,當按下按鈕時被調用,是不可預知的 - 它會工作一段時間,然後停止工作!
控制檯顯示一個錯誤:

Uncaught TypeError: Cannot read property 'name' of undefined 

代碼:

function sign_in_out_action(myself,rowid,icol,cellcontent,e){ 
    var this_row = myself.getRowData(rowid); 
    if(in_out_button_content(cellcontent)== "In"){ 
    alert('Signing OUT'); 
    this_row.in_out = "Out"; 
    $('#out_table').jqGrid('addRowData',1,this_row); 
    myself.delRowData(rowid); 
    } 
    else{ 
    if(in_out_button_content(cellcontent)== "Out"){ 
     alert('Signing in'); 
     this_row.in_out = "In"; 
     $('#in_table').jqGrid('addRowData',1,this_row); 
     myself.delRowData(rowid); 
    } 
    else{ 
     alert("what? "+in_out_button_content(cellcontent)); 
    } 
    } 

這似乎很簡單,刪除和添加數據。我希望能夠深入瞭解我做錯了什麼。

回答

0

我認爲你可以定義兩個函數,一個是爲網格添加一行,另一個是刪除網格的一行。

然後,您可以爲自定義按鈕添加點擊功能。

// delete row 
function deleteRow(tableId, rowId) { 
     $('#' + tableId).jqGrid('delRowData', rowId); 
     return false; 
} 


    var index = 999; 
// add a row 
    function addRow(tableId, c1, c2) { 
     var datarow = { Id: c1, Name: c2 }; 

     var lastsel2 = index; 
     index++; 
     var su = jQuery('#' + tableId).addRowData(lastsel2, datarow, 'last'); 
     if (su) { 
      jQuery('#' + tableId).jqGrid('editRow', 0, true); 
     } 
    } 

的C1,C2是列值。(你可能有五列,你可以定義C1,C2,C3,C4,C5)。

下面的代碼獲取單元格的值:

var c1 = $('#' + tableId).getCell(rowId, 'Id'); 
var c2 = $('#' + tableId).getCell(rowId, 'Name'); 
+0

我看到你所得到的單元格值一個接一個,使用getCell方法。但爲什麼這是必要的?不應該我的行:var this_row = myself.getRowData(rowid);立即獲取所有行數據的對象?另外,代碼中的'id'和'index'是什麼?它們是全局變量嗎?感謝您看我的問題! – jpl 2012-08-08 19:27:33

+0

我很抱歉代碼不對。索引是一個全局值,不需要id變量。在這裏,我將索引聲明爲999,而不是網格中的最大ID,您也可以編寫自己的函數,如「GetMaxId()」,以便從一個網格中的其他行獲取不同的ID。 – 2012-08-09 00:45:13

相關問題