jquery datatables各引數詳細說明及簡單應用
v1.9.0
下載後,將media
資料夾裡面的css,images,js
資料夾拷貝到你的網站即可。接下來引入以下內容:
<style type="text/css" title="currentStyle">
@import "./style/datatable/css/demo_page.css";
@import "./style/datatable/css/demo_table.css";
</style>
<script type="text/javascript" src="./style/datatable/js/jquery.js"></script>
<script type="text/javascript" src="./style/datatable/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();//其中example為table的id,table中必須有thead!
} );
</script>
$(document).ready(function() {
$('#example').dataTable();
} );
"bPaginate": true, //翻頁功能
"bLengthChange": true, //改變每頁顯示資料數量
"bFilter": true, //過濾功能
"bSort": true, //排序功能
"bInfo": true,//頁尾資訊
"bAutoWidth": true,//自動寬度
說明:以上引數配置實現的顯示效果和第一個是一樣的。
- 預設排序設定,
aaSorting
http://www.datatables.net/release-datatables/examples/basic_init/table_sorting.html
"aaSorting": [[ 4, "desc" ]],//設定第5個元素為預設排序
說明:
將表單id
改為class
:
初始化:
$('.example').dataTable(
即可。
- 隱藏部分列的內容,
aoColumnDefs
http://www.datatables.net/release-datatables/examples/basic_init/hidden_columns.html
"aoColumnDefs": [
{ "bSearchable": false, "bVisible": false, "aTargets": [ 2 ] },//bSearchable:是否可搜尋;bVisible:是否可見;aTargets:哪一列
{ "bVisible": false, "aTargets": [ 3 ] }//
]
"sDom": '<"top"i>rt<"bottom"flp><"clear">'
表示的html為:
<div class="top">這裡顯示 當前條數,總共條數</div>
這裡顯示 請求中的提示資訊,表單內容
<div class="bottom">這裡顯示 搜尋框,每頁數量選擇,翻頁按鈕</div>
<div class="clear"></div>
//l - 每頁數量選擇select
//f – 搜尋框search
//t – 表單內容table
//i – 當前條數,總共條數information
//p – 翻頁按鈕pagination
//r – 請求中的提示資訊
//< 和 > – 一個div的開始與結束
//<"class"> – class為div的class名稱
- 儲存當前頁面資訊為
cookie
,預設關閉
http://www.datatables.net/release-datatables/examples/basic_init/state_save.html
"bStateSave": true
如果使用者關閉頁面後重新開啟該頁面,該列表會和關閉前的狀態完全一樣(長度,過濾,分頁和排序)
"sPaginationType": "full_numbers"
"sScrollX": "100%",
"sScrollXInner": "110%",
"bScrollCollapse": true
用的很少
"sScrollY": "200px",
"bPaginate": false //該引數為是否顯示分頁,如果設定為true貌似就沒什麼意義了
"sScrollY": 200,
"sScrollX": "100%",
"sScrollXInner": "110%"
需要拷貝examples/examples_support/themes
資料夾到style/datatable
裡面
@import "./style/datatable/css/demo_table.css";
替換為:
@import "./style/datatable/css/demo_table_jui.css";
主題一:
引入
@import "./style/datatable/themes/smoothness/jquery-ui-1.8.4.custom.css";
主題二:
引入
@import "./style/datatable/themes/ui-lightness/jquery-ui-1.8.4.custom.css";
主題二是橘色系的,木有第一個smoothness
好看,這裡就不截圖了!
"oLanguage": {
"sLengthMenu": "每頁顯示 _MENU_條",
"sZeroRecords": "沒有找到符合條件的資料",
"sProcessing": "<img src=’./loading.gif’ />",
"sInfo": "當前第 _START_ - _END_ 條 共計 _TOTAL_ 條",
"sInfoEmpty": "木有記錄",
"sInfoFiltered": "(從 _MAX_ 條記錄中過濾)",
"sSearch": "搜尋:",
"oPaginate": {
"sFirst": "首頁",
"sPrevious": "前一頁",
"sNext": "後一頁",
"sLast": "尾頁"
}
}
也可以直接指定語言包,txt
檔案:
"sUrl": "media/language/de_DE.txt" //檔案格式和上面一樣
最前面加入:
var asInitVals = new Array();
$('#example').dataTable 修改為:var oTable =$('#example').dataTable
加入:
/*過濾程式碼開始*/
$("tfoot input").keyup( function () {
oTable.fnFilter( this.value, $("tfoot input").index(this) );
} );
$("tfoot input").each( function (i) {
asInitVals[i] = this.value;
} );
$("tfoot input").focus( function () {
if ( this.className == "search_init" )
{
this.className = "";
this.value = "";
}
} );
$("tfoot input").blur( function (i) {
if ( this.value == "" )
{
this.className = "search_init";
this.value = asInitVals[$("tfoot input").index(this)];
}
} );
tfoot
裡面加入:
<tr>
<th><input type="text" name="search_engine" value="Search engines" class="search_init" /></th>
<th><input type="text" name="search_browser" value="Search browsers" class="search_init" /></th>
<th><input type="text" name="search_platform" value="Search platforms" class="search_init" /></th>
<th><input type="text" name="search_version" value="Search versions" class="search_init" /></th>
<th><input type="text" name="search_grade" value="Search grades" class="search_init" /></th>
</tr>
在最前面引入函式:
/* 構造每一行詳情的函式 fnFormatDetails*/
function fnFormatDetails ( oTable, nTr ){
var aData = oTable.fnGetData( nTr );
var sOut = '<table cellpadding="6" cellspacing="0" border="0" style="padding-left:50px;">'; //在這裡定義要返回的內容
sOut += '<tr><td>Rendering engine:</td><td>'+aData[1]+' '+aData[4]+'</td></tr>';
sOut += '<tr><td>Link to source:</td><td>Could provide a link here</td></tr>';
sOut += '<tr><td>Extra info:</td><td>And any further details here (images etc)</td></tr>';
sOut += '</table>';
return sOut;
}
ready(function)
裡面開頭加入:
/*顯示每一行詳情用_start*/
var nCloneTh = document.createElement( 'th' );
var nCloneTd = document.createElement( 'td' );
nCloneTd.innerHTML = '<img src="./style/datatable/images/details_open.png">';
nCloneTd.className = "center";
$('#example thead tr').each( function () {
this.insertBefore( nCloneTh, this.childNodes[0] );
} );
$('#example tbody tr').each( function () {
this.insertBefore( nCloneTd.cloneNode( true ), this.childNodes[0] );
} );
/*顯示每一行詳情用_end*/
ready(function)
裡面末尾加入:
/*加入展開,收縮每一行詳情按鈕用*/
$('#example tbody td img').live('click', function () {
var nTr = $(this).parents('tr')[0];
if ( oTable.fnIsOpen(nTr) )
{
this.src = "./style/datatable/images/details_open.png";
oTable.fnClose( nTr );
}
else
{
this.src = "./style/datatable/images/details_close.png";
oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
}
} );
值得注意的是,如果加入了tfoot
,必須手動在裡面加入多一行th
!
最後,是寒風寫的簡單的php+mysql+datatables
的簡單示例,很多寒風都做了詳細的註釋說明:
<?php
$mysqli=new mysqli("localhost","root","","database");
$mysqli->query("SET NAMES utf8");
$result=$mysqli->query("SELECT * FROM `table` limit 500");
if(!$result){
echo "查詢出錯!";
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" title="currentStyle">
@import "./style/datatable/css/demo_page.css";
@import "./style/datatable/css/demo_table_jui.css";
@import "./style/datatable/themes/smoothness/jquery-ui-1.8.4.custom.css";
body{ font-size:12px;}
table{ font-size:12px;}
</style>
<script type="text/javascript" src="./style/datatable/js/jquery.js"></script>
<script type="text/javascript" src="./style/datatable/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
/* 構造每一行詳情的函式 fnFormatDetails*/
function fnFormatDetails ( oTable, nTr ){
var aData = oTable.fnGetData( nTr );
var sOut = '<table cellpadding="6" cellspacing="0" border="0" style="padding-left:50px;">'; //在這裡定義要返回的內容
sOut += '<tr><td>Rendering engine:</td><td>'+aData[1]+' '+aData[4]+'</td></tr>';
sOut += '<tr><td>Link to source:</td><td>Could provide a link here</td></tr>';
sOut += '<tr><td>Extra info:</td><td>And any further details here (images etc)</td></tr>';
sOut += '</table>';
return sOut;
}
/*頁面元素載入完成後開始執行*/
$(document).ready(function() {
/*顯示每一行詳情用_start*/
var nCloneTh = document.createElement( 'th' );
var nCloneTd = document.createElement( 'td' );
nCloneTd.innerHTML = '<img src="./style/datatable/images/details_open.png">';
nCloneTd.className = "center";
$('#example thead tr').each( function () {
this.insertBefore( nCloneTh, this.childNodes[0] );
} );
$('#example tbody tr').each( function () {
this.insertBefore( nCloneTd.cloneNode( true ), this.childNodes[0] );
} );
/*顯示每一行詳情用_end*/
var asInitVals = new Array(); //用於每一列搜尋過濾
var oTable =$('#example').dataTable( //var oTable用於每一列搜尋過濾
{
/*基本引數設定,以下引數設定和預設效果一致*/
"bPaginate": true, //翻頁功能
"bLengthChange": true, //改變每頁顯示資料數量
"bFilter": true, //過濾功能
"bSort": true, //排序功能
"bInfo": true,//頁尾資訊
"bAutoWidth": true,//自動寬度
/*預設排序設定*/
"aaSorting": [[ 4, "desc" ]],//設定第5個元素為預設排序
/*預設翻頁樣式設定*/
"sPaginationType": "full_numbers",
/*是否開啟主題*/
"bJQueryUI": true,
/*語言設定*/
"oLanguage": {
"sLengthMenu": "每頁顯示 _MENU_條",
"sZeroRecords": "沒有找到符合條件的資料",
"sProcessing": "<img src=’./loading.gif’ />",
"sInfo": "當前第 _START_ - _END_ 條 共計 _TOTAL_ 條",
"sInfoEmpty": "木有記錄",
"sInfoFiltered": "(從 _MAX_ 條記錄中過濾)",
"sSearch": "搜尋:",
"oPaginate": {
"sFirst": "首頁",
"sPrevious": "前一頁",
"sNext": "後一頁",
"sLast": "尾頁"
}
}
}
);
/*每一列搜尋過濾程式碼開始*/
$("tfoot input").keyup( function () {
oTable.fnFilter( this.value, $("tfoot input").index(this) );
} );
$("tfoot input").each( function (i) {
asInitVals[i] = this.value;
} );
$("tfoot input").focus( function () {
if ( this.className == "search_init" )
{
this.className = "";
this.value = "";
}
} );
$("tfoot input").blur( function (i) {
if ( this.value == "" )
{
this.className = "search_init";
this.value = asInitVals[$("tfoot input").index(this)];
}
} );
/*加入展開,收縮每一行詳情按鈕用*/
$('#example tbody td img').live('click', function () {
var nTr = $(this).parents('tr')[0];
if ( oTable.fnIsOpen(nTr) )
{
this.src = "./style/datatable/images/details_open.png";
oTable.fnClose( nTr );
}
else
{
this.src = "./style/datatable/images/details_close.png";
oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
}
} );
} );
</script>
</head>
<body>
<div style=" width:800px; margin:0 auto;">
<table cellpadding="0" cellspacing="0" class="display" border="0" id="example">
<thead>
<tr>
<th>ID</th>
<th>時間</th>
<th>數量</th>
<th>次數</th>
<th>消耗</th>
</tr>
</thead>
<tbody>
<?php
while($rows=$result->fetch_assoc()){
$rows['time']=date("Y-m-d H:i:s",$rows['time']);
echo "
<tr>
<td>{$rows['ID']}</td>
<td>{$rows['time']}</td>
<td>{$rows['r_num']}</td>
<td>{$rows['r_times']}</td>
<td>{$rows['money']}</td>
</tr>";
}
?>
</tbody>
<tfoot>
<tr>
<th></th>
<th>ID</th>
<th>時間</th>
<th>數量</th>
<th>次數</th>
<th>消耗</th>
</tr>
<tr>
<th></th>
<th><input type="text" name="search_engine" value="Search engines" class="search_init" /></th>
<th><input type="text" name="search_browser" value="Search browsers" class="search_init" /></th>
<th><input type="text" name="search_platform" value="Search platforms" class="search_init" /></th>
<th><input type="text" name="search_version" value="Search versions" class="search_init" /></th>
<th><input type="text" name="search_grade" value="Search grades" class="search_init" /></th>
</tr>
</tfoot>
</table>
<style>
#example tr.even:hover {background-color: #ECFFB3;}
#example tr.even:hover td.sorting_1 {background-color: #DDFF75;}
#example tr.even:hover td.sorting_2 {background-color: #E7FF9E;}
#example tr.even:hove3 {background-color: #E2FF89;}
#example tr.odd:hover {background-color: #E6FF99;}
#example tr.odd:hover td.sorting_1 {background-color: #D6FF5C;}
#example tr.odd:hover td.sorting_2 {background-color: #E0FF84;}
#example tr.odd:hover td.sorting_3 {background-color: #DBFF70;}
</style>
</div>
</body>
</html>
實現了上面絕大部分功能!以上php+datatables
示例僅適用於資料量少時的情況.
相關文章
- JQuery Datatables Columns API 引數詳細說明jQueryAPI
- mysqldump引數詳細說明MySql
- MySQL mysqldump命令的引數詳細說明MySql
- MySQL引數DELAY_KEY_WRITE的詳細說明MySql
- 01 . Shell詳細入門介紹及簡單應用
- Goldengate引數簡要說明Go
- [FFmpeg]ffmpeg各類引數說明與使用示例
- DataTables表格外掛使用說明
- mysql常用引數使用說明及查詢MySql
- TOP引數說明
- mysqldump引數說明MySql
- mysqldump 引數說明MySql
- MySQL引數說明MySql
- mysql processlist詳細說明MySql
- Android清單AndroidManifest詳細說明Android
- nginx 詳解 – 詳細配置說明Nginx
- nginx 詳解 - 詳細配置說明Nginx
- Spring 對於事務上的應用的詳細說明Spring
- 資料泵的TRANSFORM引數說明及使用ORM
- Kafka 配置引數彙總及相關說明Kafka
- Java基礎學習總結(120)——JVM 引數使用詳細說明JavaJVM
- mysql replace into用法詳細說明MySql
- redis info命令詳細說明Redis
- Emacs詳細使用說明(轉)Mac
- memset函式詳細說明函式
- Elasticsearch 引數配置說明Elasticsearch
- kafka 引數配置說明Kafka
- redis 3.0 引數說明Redis
- golden gate 引數說明Go
- oracle引數說明(zt)Oracle
- xming工具的簡單實用說明
- 各類電商平臺批次獲取商品資訊 API 詳細操作說明API
- Flask-Limit使用詳細說明FlaskMIT
- Linux sed命令詳細說明Linux
- VNC安裝配置詳細說明VNC
- Nginx配置檔案詳細說明Nginx
- CocoaPods | iOS詳細使用說明iOS
- session的詳細說明和用法Session