簡體   English   中英

在jQuery DataTables中所有行的前面放置一個按鈕

[英]Put a button in front of all rows in jQuery DataTables

我正在使用jQuery DataTables。 正在使用數據庫中的JSON數據填充它。 我不知道如何在每個記錄的前面顯示按鈕或鏈接。 我要這樣做,以便當用戶單擊該按鈕時,該特定記錄會添加到數據庫中,因此按鈕或鏈接應包含一個ID。 請幫助解決我的問題。 以下是我正在使用的代碼:

var oTable = $('#jsontable').dataTable();

$.ajax({
  url: 'process.php?method=fetchdata',
  dataType: 'json',
  success: function(s) {
    console.log(s);
    oTable.fnClearTable();
    for (var i = 0; i < s.length; i++) {
      oTable.fnAddData([
        s[i][3],
        s[i][4],
        s[i][0], // this contains id
      ]);
    }
  },
  error: function(e) {
    console.log(e.responseText);
  }
});
<table id="jsontable" class="display table table-bordered" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Class Number</th>
      <th>Subject</th>
      <th>ADD</th>
    </tr>
  </thead>
</table>

我希望這會有所幫助

 oTable = $('#jsontable').dataTable({ "fnRowCallback": function (nRow, aaData, iDisplayIndex) { //nRow Row Object //aaData Param //iDisplayIndex Row Index $('td:eq(0)', nRow).html("<input type='button'>Button</input>"); return nRow; } }); 

你是說要做這樣的事嗎? (另外,考慮到現在,這可能是針對較舊版本的數據表的。這是1.9版的語法,我想您正在使用的是較新版本(1.10+)。語法可能有所不同,但應在api文檔中進行記錄

    $('#jsontable').dataTable({
        "sAjaxSource" : "process.php?method=fetchdata",
        "aoColumns" : [
            { "mData": function(source) {
                    return '<input type="button" value=' + source[0] + '/>';
            }},
            { "mData": function(source) {
                    return source[1];
            }},
            { "mData": function(source) {
                    return source[2];
            }}
        }
    });

顯然,您的按鈕可以根據您的需要進行顯示,您可能不希望將ID存儲在值上。 您可以使用它來做您需要的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM