2010-07-09 46 views
10

如何向我在數據表中添加的行添加類?如何將類添加到jQuery數據表中的新行?

如果不行,我該如何使用fnRowCallbackfnDrawCallback來更改類?

oTable = $('#example').dataTable({ 
    "bJQueryUI": true, 
    "bSortClasses": false, 
    "sDom":'T<"clear">', 
    "sPaginationType": "full_numbers", 
    "sDom": 'T<"clear"><"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"lfr>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ip>', 
    "fnRowCallback": function(nRow, aData, iDisplayIndex) { 

    var oSettings = oTable.fnSettings(); 
    oSettings.aoData[iDisplayIndex].nTr.className = "gradeX odd"; 
    } 
}); 

上面的代碼給了我一個錯誤。

這是我添加的行:

oTable.fnAddData(arr); 
+0

懷疑它會真正幫助,但完成。我嘗試了很多其他的東西,但沒有結果。 – Pierluc 2010-07-09 15:31:11

+0

還是有沒有辦法,我可以簡單地添加一個html行,通過datatables功能 – Pierluc 2010-07-09 15:38:33

回答

-4

好吧,也許我不理解你的問題是什麼,但如果你是剛剛加入該行,爲什麼不設置你之前類添加它?與此類似,有點草率,例如:

jQuery("<tr />") 
    .html(your_var_containing_the_interior_dom) 
    .addClass("yourClass") 
    .appendTo(jQuery("#yourTable")) 
19

試着改變你的fnRowCallback以下幾點:

"fnRowCallback": function(nRow, aData, iDisplayIndex) { 
    nRow.className = "gradeX odd"; 
    return nRow; 
} 

您可以參考offical documentation進一步瞭解這個回調函數。

+1

數據表我找到了最佳解決方案。我想添加一個類到特定的列而不是行。 $($(nRow).children()[1])。attr('class','status-indicator'); – rhigdon 2012-12-12 23:57:06

4

試試這個:

"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { 
      var id = aData[0]; 
      $(nRow).attr("id",id); 
      if (jQuery.inArray(aData[0], gaiSelected) != -1) 
      { 
       $(nRow).addClass('row_selected'); 
      } 
      return nRow; 
} 

對於添加一行數據表試試這個代碼:

http://datatables.net/examples/api/add_row.html

/* Global var for counter */ 
var giCount = 1; 

$(document).ready(function() { 
    $('#example').dataTable(); 
}); 

function fnClickAddRow() { 
    $('#example').dataTable().fnAddData([ 
     giCount+".1", 
     giCount+".2", 
     giCount+".3", 
     giCount+".4" ]); 

    giCount++; 
} 
2
$(document).ready(function() { 
    oTable = $('#table_id').dataTable({"fnInitComplete": after_init}); 
}); 
function after_init(){ 
    $("#table_id tbody tr").addClass("gradeA"); 
} 
9

您可以在數據本身添加類名描述在文檔中。

http://www.datatables.net/examples/server_side/ids.html

使用DT_RowId用於添加ID爲任何行
使用DT_RowClass用於添加類的任何行
使用DT_RowData用於添加HTML5數據對象的任何行

例如:

「data」:[ {
「DT_RowId」: 「row_5」,
「FIRST_NAME」: 「愛理」,
「姓氏」: 「佐藤」,
「位置」: 「會計」,
「辦公室」:「東京」
「起始日期」: 「08年11月28日」,
「工資」: 「$ 162 700」
}]

1

這應該做的伎倆:

var r = t.row.add([ 
    .... 
]).node(); 
$(r).css({"color":"red"}); 
+0

我不知道爲什麼這是低調投票,這解決了問題(除了它添加內聯CSS) 要使用此技術添加一個類只需將$(r).css更改爲$(r).addClass('class 「); – 2016-03-22 16:46:38

1

官方文件說:

var table = $('#example').DataTable(); 

table 
    .rows.add([ 
     new Pupil(43), 
     new Pupil(67), 
     new Pupil(102) 
    ]) 
    .draw() 
    .nodes() 
    .to$() 
    .addClass('new'); 

請閱讀:rows.add()

+0

這段代碼是用於多行查看我的單行。 – 2016-10-04 08:53:01

0

閱讀文檔這個工作對我來說後:

var my_dataTable = $('#my-table').DataTable(); 
my_dataTable.row.add([ 
       'Hello', 
       'Hello2', 
       'Hello3', 
       'Hello4' 
      ]).draw().nodes().to$().addClass("my_class"); 
相關問題