Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将类添加到jquery datatables中的新行?_Jquery_Datatables_Addclass - Fatal编程技术网

如何将类添加到jquery datatables中的新行?

如何将类添加到jquery datatables中的新行?,jquery,datatables,addclass,Jquery,Datatables,Addclass,如何将类添加到要添加到datatable中的行中 如果不可能,如何使用fnRowCallback或fnDrawCallback更改类 oTable = $('#example').dataTable( { "bJQueryUI": true, "bSortClasses": false, "sDom":'T<"clear">', "sPaginationType": "full_numbers", "sDom": 'T<"clear"><"fg-

如何将类添加到要添加到datatable中的行中

如果不可能,如何使用
fnRowCallback
fnDrawCallback
更改类

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";
  }
});

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

jQuery("<tr />")
  .html(your_var_containing_the_interior_dom)
  .addClass("yourClass")
  .appendTo(jQuery("#yourTable"))
jQuery(“”)
.html(包含内部dom的变量)
.addClass(“您的类”)
.appendTo(jQuery(“yourTable”))

尝试将
fRowCallback
更改为以下内容:

"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
  nRow.className = "gradeX odd";
  return nRow;
}
您可以参考以进一步了解此回调函数。

尝试以下操作:

"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;
}
要将行添加到datatable,请尝试以下代码:


您可以按照文档中的描述在数据本身中添加类名

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

例如:

“数据”:[ {
“DT_RowId”:“row_5”,
“名字”:“Airi”,
“姓氏”:“Satou”,
“职位”:“会计”,
“办公室”:“东京”
“开始日期”:“2008年11月28日”,
“工资”:“$162700”

}]

这应该可以做到:

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

官方文件说:

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

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

请阅读:

阅读文档后,为我完成这项工作:

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

我怀疑这真的会有帮助,但已经完成了。我尝试了很多其他的方法,但是没有结果。或者有没有一种方法可以简单地通过我找到的datatables functionsBest解决方案向datatable添加一个html行。我想将一个类添加到特定的列而不是行$($(nRow.children()[1]).attr('class','status indicator');我不知道为什么会被否决,这解决了使用这种技术添加类的问题(除了添加内联CSS),只需将$(r).CSS更改为$(r).addClass('class');此代码适用于多行,请查看我的单行代码。
var r = t.row.add( [
    ....
] ).node();
$(r).css({"color":"red"});
var table = $('#example').DataTable();

table
    .rows.add( [
        new Pupil( 43 ),
        new Pupil( 67 ),
        new Pupil( 102 )
    ] )
    .draw()
    .nodes()
    .to$()
    .addClass( 'new' );
var my_dataTable = $('#my-table').DataTable();
my_dataTable.row.add( [
                'Hello',
                'Hello2',
                'Hello3',
                'Hello4'
            ] ).draw().nodes().to$().addClass("my_class");