固定表头jquery datatable的使用与定制

jquery datatable的固定表头的官方文档
https://datatables.net/extensions/fixedcolumns/
一、使用

1.引用3个js与2个css

<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript" src="jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" src="dataTables.fixedColumns.js"></script>

<link rel="stylesheet" type="text/css" href="jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="dataTables.fixedColumns.css">


2.数据生成普通的table标签,例:

<table id="example" class="stripe row-border order-column" width="100%" cellspacing="0" class=" xhe-border">
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger</td>
            <td>Nixon</td>
        </tr>
    </tbody>
</table>


3.文档加载完毕后将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:

	<!--https://examples.wenzhixin.net.cn/examples/bootstrap_table/data-->
	<table id="table" data-show-columns="true" data-pagination="true"
		data-search="true" data-url="http://image.highersoft.net/data.json"
		data-side-pagination="server" data-row-style="rowStyle" class=" xhe-border">
		<thead>
			<tr>
				<th data-field="id" data-sortable="true">ID</th>
				<th data-field="name" data-sortable="true">Item Name</th>
				<th data-field="price" data-sortable="true">Item Price</th>
			</tr>
		</thead>
	</table>

	
	
	

	



bootstrap-table示例源码下载

另外还有一个不用插件的方案:http://www.highersoft.net/html/notice/notice_596.html

文/程忠 浏览次数:0次   2015-02-14 10:33:50

相关阅读


评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑