Datatables js 複雜表頭 合併單元格

xcss

→Datatables官網←html

xspa

項目中用到的Table都是用Datatables插件來搞得;
之前都是生成通常性的table;
近期要生成一些複雜表頭,合併單元格之類的;
研究了一下.

x插件

去官網上也查到了[複雜表頭(rowspan 和 colspan)(Complex headers (rowspan and colspan))],[複雜表頭], 可是隻是html代碼,蛋疼啊...code

後來看到了[建立行回調(Row created callback)]哪裏的時候 ,豁然開朗!!!   先上行回調的官網js代碼>>>orm

$(document).ready(function() {
    $('#example').dataTable( {
        "createdRow": function ( row, data, index ) {
            if ( data[5].replace(/[\$,]/g, '') * 1 > 4000 ) {
                $('td', row).eq(5).css('font-weight',"bold").css("color","red");
            }
        }
    } );
} );

最終解決方案(修改下上面的代碼便可)

$(document).ready(function() {
    $('#example').dataTable( {
        "createdRow": function ( row, data, index ) {
                        //生成了行以後,開始生成表頭>>>
                            if (index == 1) {
                                var innerTh = '<tr><th rowspan="2">Name</th>';
                   var columnsCount = 3;//具體狀況
                                innerTh +='<th colspan="2">Information</th>';                             
                   innerTh +='<th colspan="3">Contact</th>';                               
                                innerTh += '</tr>';
                   //table的id爲"id_table"
                                document.getElementById('id_table').insertRow(0);
                                var $tr = $("#id_table tr").eq(0);
                                $tr.after(innerTh);
                            }
                        } } ); } );

 

xhtm

總結-

其實會用[建立行回調]以後,
複雜表頭只是一個demo了,
想搞其餘的只要操做table就好了,好比合併單元格,嘎嘎···
拋磚引玉了...
相關文章
相關標籤/搜索