10

如何将类添加到我在数据表中添加的行?

如果不可能,我该如何使用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);
4

8 回答 8

19

尝试将您的更改fnRowCallback为以下内容:

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

可以参考官方文档进一步了解这个回调函数。

于 2011-01-05T18:59:22.400 回答
9

您可以按照文档中的说明在数据本身中添加类名。

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

使用DT_RowId为任何行添加 id
使用DT_RowClass为任何行添加类
使用DT_RowData为任何行添加 html5 数据对象

例如:

“数据”:[{
“DT_RowId”:“row_5”,
“first_name”:“Airi”,
“last_name”:“Satou”,
“职位”:“会计师”,
“办公室”:“东京”,
“start_date”: “2008 年 11 月 28 日”,
“薪水”:“162,700 美元”
}]

于 2014-10-13T07:15:52.240 回答
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++;
}
于 2011-01-07T01:10:54.870 回答
3

官方文档说:

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

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

请阅读:rows.add()

于 2016-06-14T11:46:38.093 回答
2
$(document).ready(function() {
    oTable = $('#table_id').dataTable( {"fnInitComplete": after_init} );
} );
function after_init(){
    $("#table_id tbody tr").addClass("gradeA");
}
于 2013-07-19T07:58:56.187 回答
1

阅读文档后,这项工作对我有用:

var my_dataTable = $('#my-table').DataTable();
my_dataTable.row.add( [
                'Hello',
                'Hello2',
                'Hello3',
                'Hello4'
            ] ).draw().nodes().to$().addClass("my_class");
于 2016-10-04T08:52:14.863 回答
1

这应该可以解决问题:

var r = t.row.add( [
    ....
] ).node();
$(r).css({"color":"red"});
于 2015-08-21T13:22:04.200 回答
-4

好吧,也许我不明白你的问题是什么,但如果你只是添加行,为什么不在添加之前设置类?像这样,有点草率,例子:

jQuery("<tr />")
  .html(your_var_containing_the_interior_dom)
  .addClass("yourClass")
  .appendTo(jQuery("#yourTable"))
于 2010-07-09T19:18:37.470 回答