jquery dataTable標題和列內容居中

     標題居中需要在全局css文件中定義table的樣式,假設在一個html的div中這樣定義一個dataTable:

<table class="table table-striped table-bordered table-hover" id="datatable">
                                
      <thead>
           <tr>
              <th></th>
              <th></th>

              <th> 賬號 </th>
              <th> 密碼 </th>
              <th> 數量 </th>
              <th> 類型 </th>
              <th> 時間 </th>
              <th> 操作 </th>
           </tr>
     </thead>
</table>

         前面兩個th內容爲空是因爲我這個表格不需要顯示覆選框和序號列,所以內容爲空,然後在js中dataTable初始化時 ,columns的前兩列data設爲null,visible設爲false,就隱藏了這兩列:

columns: [
          {"data": null, "visible": false},
          {"data": null, "visible": false},
          ....

        然後在全局css文件中重寫table裏面的th樣式,代碼如下: 

.table th { 
    text-align: center;
    vertical-align: middle !important;
}

        最後是實現每列內容居中,通過在dataTable初始化時的createRow參數實現,初始化時繪製每一列時設置樣式:

createdRow: function( row, data, dataIndex ) {
                $(row).children('td').attr('style', 'text-align: center;vertical-align: middle;');
            },

            

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章