我在应用程序中使用引导数据表。我使用服务器端使用ajax从mysql数据库中获取值。
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><thcolspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");
}
}
} );
在employee-grid-data.php 中
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
所以我想要的是——我想为每个tr行添加单独的类。在数据表中,我看不到任何tr。那么我如何向tr添加一个类。(根据条件,tr的背景颜色不同)。
这个数据表示例可能会对您有所帮助,它所做的事情与您想要完成的非常相似。
它使用createdRow
init选项来定义一个函数,该函数对于(通过ajax或任何其他源)添加到数据表的每一行调用一次。在这个函数中,您可以应用您的逻辑将类添加到任何符合条件的行中。
$(document).ready(function() {
$('#example').DataTable( {
"createdRow": function ( row, data, index ) {
if ( data[5].replace(/['$,]/g, '') * 1 > 150000 ) {
$('td', row).eq(5).addClass('highlight');
}
}
} );
} );
(代码取自数据表文档)