js代碼以下: $('#mytable').dataTable( { "bServerSide": true, //開啓服務器模式,使用服務器端處理配置datatable。注意:sAjaxSource參數也必須被給予爲了給datatable源代碼來獲取所需的數據對於每一個畫。 這個翻譯有點彆扭。開啓此模式後,你對datatables的每一個操做 每頁顯示多少條記錄、下一頁、上一頁、排序(表頭)、搜索,這些都會傳給服務器相應的值。
"sAjaxSource": "ajax.php", //給服務器發請求的url
"aoColumns": [ //這個屬性下的設置會應用到全部列,按順序,沒有是空
{"sDefaultContent": ''},//checkbox column 至關於佔位符,等待填充。也能夠指定值如 {"mData": 'ip'}
{"sDefaultContent": ''},//ip column
{"sDefaultContent": ''},//nip column
{"sDefaultContent": ''},//role column
{"sDefaultContent": ''},//gamenserver column
{"sDefaultContent": ''},//serverid column
{"sDefaultContent": ''},//status column
{"sDefaultContent": ''},//remark column
{"sDefaultContent": ''},//ctime column
{"sDefaultContent": '', "sClass": "action"},//sClass 表示給本列加class
],
"aoColumnDefs": [//和aoColums相似,但他能夠給指定列賦予屬性
{"bSortable": false, "aTargets": [0,7,9]}, //這句話意思是第7,9列(從0開始算)不排序
],
"aaSorting": [[1, "asc"]], //默認排序
"fnRowCallback": function(nRow, aData, iDisplayIndex) {// 給各列填充內容
$('td:eq(0)', nRow).html('<label><input type="checkbox" id="hids[]" value="'+aData.id+'-'+aData.status+'" name="hids[]"></label>'); $('td:eq(2)', nRow).html(aData.nip); var role = aData.role.split(' '); var href = ""; for(var i=0;i<role.length;i++){ href += '<a href="host.php?ser='+role[i]+'">'+role[i]+'</a> '; } $('td:eq(3)', nRow).html(href); $('td:eq(4)', nRow).html(aData.gameserver); $('td:eq(5)', nRow).html(aData.serverid); $('td:eq(7)', nRow).html(aData.remark); $('td:eq(8)', nRow).html(aData.ctime); if (aData.status == 1) { if(aData.hostscreenid){ $('td:eq(1)', nRow).html('<a href="http://11.111.111.11:8081/screen/'+aData.hostscreenid+'"><span style="color:red;">'+aData.ip+'</span></a>'); }else{ $('td:eq(1)', nRow).html("<span style='color:red;'>"+aData.ip+"</span>"); } $('td:eq(6)', nRow).html("<span style='color:red;'>線下</span>"); } else if (aData.status == 0) { if(aData.hostscreenid){ $('td:eq(1)', nRow).html('<a href="http://11.111.111.11:8081/screen/'+aData.hostscreenid+'">'+aData.ip+'</a>'); }else{ $('td:eq(1)', nRow).html(aData.ip); } $('td:eq(6)', nRow).html("<span>線上</span>"); } $('td:eq(9)', nRow).html('<a href="host.php?op=del&id='+aData.id+'"> <button class="btn btn-danger btn-sm">刪除</button> </a> <button class="btn btn-success btn-sm" data-toggle="modal"data-target="#myModal'+aData.id+'">編輯 </button> '); return nRow; }, } );
1 服務器端php代碼 2 $sEcho = $_GET['sEcho']; 3 $start = $_GET['iDisplayStart'];//從多少開始
4 $length = $_GET['iDisplayLength'];//數據長度
5 $sort_th = $_GET['iSortCol_0'];//被排序的列
6 $sort_type = $_GET['sSortDir_0'];//排序規則
7
8 if(!empty($_GET['sSearch'])){ 9 $search = $_GET['sSearch']; 10 } 11
12 $where = ""; 13 $order = ""; 14 if ($search) { 15 $where = " where concat( xx,xx,xx,xx) like '%$search%' ";//查詢
16 } 17
18 if ($sort_th == 1) { 19 $order = " order by xx " . $sort_type . " "; 20 } elseif ($sort_th == 2) { 21 $order = " order by xx " . $sort_type . " "; 22 } elseif ($sort_th == 4) { 23 $order = " order by xx " . $sort_type . " "; 24 } elseif ($sort_th == 4) { 25 $order = " order by xx " . $sort_type . " "; 26 } elseif ($sort_th == 5) { 27 $order = " order by xx " . $sort_type . " "; 28 } elseif ($sort_th == 6) { 29 $order = " order by xx " . $sort_type . " "; 30 } elseif ($sort_th == 8) { 31 $order = " order by xx " . $sort_type . " "; 32 } 33 $condition = $where . $order; 34
35 $hosts = getLimitRow("xx,xx,xx,xx", 'table', $condition, $start, $length); 36 $count = getRecordCount('table'); 37 if(count($hosts) > 0){ 38 $host_screenid = getScreenid(); 39 foreach ($hosts as $index => $values) { 40 foreach ($values as $field => $value) { 41 if ($values['status'] == 0) { 42 $status_str = toutf8('線上'); 43 } else { 44 $status_str = toutf8('線下'); 45 } 46 $aaData[$index][$field] = toutf8($value); 47 $aaData[$index]['statusstr'] = $status_str; 48 $aaData[$index]['hostscreenid'] = $host_screenid[$aaData[$index]['ip']]; 49 } 50 } 51
52 if ($search) { 53 $dcount = getRecordCount('table', "where concat( xxx,xx,xx) like '%$search%' "); 54 } else { 55 $dcount = $count; 56 } 57 $json_data = array('sEcho' => $sEcho, 'iTotalRecords' => $count, 'iTotalDisplayRecords' => $dcount, 'aaData' => $aaData); 58 echo json_encode($json_data); 59 exit; 60 }else{ 61 $aaData = array(); 62 $json_data = array('sEcho' => $sEcho, 'iTotalRecords' => $count, 'iTotalDisplayRecords' => 0, 'aaData' => $aaData); 63 echo json_encode($json_data); 64 }