jquery datatable的固定表头的官方文档
https://datatables.net/extensions/fixedcolumns/
一、使用
1.引用3个js与2个css
2.数据生成普通的table标签,例:
First nameLast name
TigerNixon3.文档加载完毕后将2的table与jquery dataTables绑定
$(document).ready(function() {
var table = $(’#example’).DataTable( {
scrollY: "300px",
scrollX: true,
scrollCollapse: true,
paging: false
} );
new $.fn.dataTable.FixedColumns( table, {
leftColumns: 1,
rightColumns: 1
} );
} );
二 定制
1.固定第一行,最左行等配置
A.固定第一行,超过300px出垂直滚动条
scrollY: "300px",
B.宽度超长出水平滚动条
scrollX: true,
C.固定最左行
leftColumns: 1,
2.table样式
th, td { white-space: nowrap; }
div.dataTables_wrapper {
width: 800px;
margin: 0 auto;
}
如上代码,空格不换行,宽度等。这些还要与自己项目的样式调合,这是最主要的工作量。
另外官方文档中demo最左列与正文的行有1,2像素没对齐,还需再研究。
三 boostrap datatable设置行样式:
function rowStyle(row, index){
//bookState是ajax返回的状态值
let bookState=row.bookState;
if (bookState!=undefined && bookState == "1" ) {
/*return {css: {'color': 'gray'}};*/
return {classes:'gray-bg-ignor'};
}
return {};
}表格html与js:
data-search="true" data-url="http://image.highersoft.net/data.json"
data-side-pagination="server" data-row-style="rowStyle" class=" xhe-border">
IDItem NameItem Price
另外还有一个不用插件的方案:http://www.highersoft.net/html/notice/notice_596.html