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;
}, }
);
服务器端php代码
$sEcho = $_GET['sEcho'];
$start = $_GET['iDisplayStart'];//从多少开始
$length = $_GET['iDisplayLength'];//数据长度
$sort_th = $_GET['iSortCol_0'];//被排序的列
$sort_type = $_GET['sSortDir_0'];//排序规则 if(!empty($_GET['sSearch'])){
$search = $_GET['sSearch'];
} $where = "";
$order = "";
if ($search) {
$where = " where concat( xx,xx,xx,xx) like '%$search%' ";//查询
} if ($sort_th == 1) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 2) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 4) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 4) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 5) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 6) {
$order = " order by xx " . $sort_type . " ";
} elseif ($sort_th == 8) {
$order = " order by xx " . $sort_type . " ";
}
$condition = $where . $order; $hosts = getLimitRow("xx,xx,xx,xx", 'table', $condition, $start, $length);
$count = getRecordCount('table');
if(count($hosts) > 0){
$host_screenid = getScreenid();
foreach ($hosts as $index => $values) {
foreach ($values as $field => $value) {
if ($values['status'] == 0) {
$status_str = toutf8('线上');
} else {
$status_str = toutf8('线下');
}
$aaData[$index][$field] = toutf8($value);
$aaData[$index]['statusstr'] = $status_str;
$aaData[$index]['hostscreenid'] = $host_screenid[$aaData[$index]['ip']];
}
} if ($search) {
$dcount = getRecordCount('table', "where concat( xxx,xx,xx) like '%$search%' ");
} else {
$dcount = $count;
}
$json_data = array('sEcho' => $sEcho, 'iTotalRecords' => $count, 'iTotalDisplayRecords' => $dcount, 'aaData' => $aaData);
echo json_encode($json_data);
exit;
}else{
$aaData = array();
$json_data = array('sEcho' => $sEcho, 'iTotalRecords' => $count, 'iTotalDisplayRecords' => 0, 'aaData' => $aaData);
echo json_encode($json_data);
}