2010-07-06 97 views
13

我想實現jQuery的數據表中的函數,返回的第一個和點擊行JQuery的DataTable中單擊單元格

我下面這個例子中,第4列,這讓我操縱一個被點擊排 http://datatables.net/examples/api/select_single_row.html

以爲我可以改變這個處理程序做讀取單元格值程序,並用我自己的邏輯值

/* Add a click handler to the rows - this could be used as a callback */ 
$("#example tbody").click(function(event) { 
    $(oTable.fnSettings().aoData).each(function(){ 
     $(this.nTr).removeClass('row_selected'); 
    }); 
    $(event.target.parentNode).addClass('row_selected'); 
}); 

我也用這個小小的代碼段FR過來OM的dataTable論壇http://datatables.net/forums/comments.php?DiscussionID=1384&page=1#Item_0

$('#example tbody tr').click(function() { 
    // Alert the contents of an element in a SPAN in the first TD 
    alert($('td:eq(0) span', this).html()); 
}); 

可我有任何指針,所以我可以得到點擊場的第1和第4列?

下一部分 我上面的解決了,感謝尼克

但是我的問題的一個部分。當我初始化表 我使用

/* Init the table */ 
    oTable = $('#filetable').dataTable({ 
     "bProcessing": true, 
     "bServerSide": true, 
     "sAjaxSource": "/crvWeb/jsonFileList.do", 
     "fnServerData": function (sSource, aoData, fnCallback) { 
      $.ajax({ 
       "dataType": 'json', 
       "type": "POST", 
       "url": sSource, 
       "data": aoData, 
       "success": fnCallback 
      }); 
     } 
    }); 

我的servlet需要一個目錄請求參數和返回的列表到DataTable的JSON響應。

/crvWeb/jsonFileList.do 

我該如何添加和獲得serlvet回覆與發佈請求,所以我可以讓我的表更新?

回答

25

你可以用最簡單的.delegate()這裏,像這樣:

$("#example tbody").delegate("tr", "click", function() { 
    var firstCellText = $("td:first", this).text(); 
    var fourthCellText = $("td:eq(3)", this).text(); 
}); 

You can try out a demo here

隨着.delegate()this<tr>因爲這是我們正在處理的點擊,使事情頗有幾分清潔..並且它仍然只有一個事件處理程序在<tbody>級別,而不是每<tr>一個。

+0

再一次跟進問題。如果我想將值傳遞給網格來更新它,我應該如何使用函數的功能來更新動態表格? – nokheat 2010-07-07 03:12:05

+0

@nokheat - 你是什麼意思?如果你可以更詳細地描述值的來源和去向,我可能會幫助...我必須在夜間崩潰,但在這裏回覆,我會在早上檢查:) – 2010-07-07 03:13:22

+0

我需要顯示一些代碼段,所以請參考我最近編輯的 – nokheat 2010-07-07 04:25:54

0

這應該做的伎倆,如果我正確地讀你的代碼:

$("tr.row_selected td:nth-child(1), tr.row_selected td:nth-child(4)"); 

應該返回所有TR元素的第一和第四個孩子與類row_selected

+0

我正在檢查我的代碼和您的解決方案,但我仍然試圖解決它。主要的convern是,我真的可以從處理程序的輸入參考嗎?像$(「#example tbody」),並使用這個標識符來獲取單元格的引用,從而得到它的值? – nokheat 2010-07-06 10:50:48

相關問題