[英]How to stylize table <td> based on its value from ajax with datatables
I have a page showing datatables using php. 我有一个页面显示使用php的数据表。 As the data is increasing day by day, I have considered datatable ajax function to get it processed server side to reduce time taken to load all the data at once.
由于数据每天都在增加,因此我考虑过使用datatable ajax函数在服务器端进行处理,以减少一次加载所有数据所需的时间。
But the problem is I can't figure out how to style based on its values. 但是问题是我不知道如何根据其值进行样式设置。 For example:
例如:
What I'm using with PHP:
我在PHP中使用的是什么:
<table>
<thead>
<tr>
<th>Date</th>
<th>Amt</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($data = $sql->fetch(PDO::FETCH_OBJ)) {
echo "
<tr>
<td>".$data->date."</td>
<td>".$data->amt."</td>";
// Please note this step...
if ($data->status == "Paid") {
echo '
<td>
<label class="label label-succcess">'.$data->status.'</label>
</td>';
}
elseif ($data->status == "Unpaid"){
echo '
<td>
<label class="label label-danger">'.$data->status.'</label>
</td>';
}
elseif ($data->status == "Pending"){
echo '
<td>
<label class="label label-warning">'.$data->status.'</label>
</td>';
}
echo '</tr>';
}
</tbody>
</table>
How to achieve the same
<label>
style on<td>
with data from datatables ajax:如何使用来自数据表ajax的数据在
<td>
上实现相同的<label>
样式:
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead
<tr>
<th>Date</th>
<th>Amt</th>
<th>Status</th>
</tr>
</thead>
</table>
$(document).ready(function() {
//$('#datatable-buttons').DataTable( {
var table = $('#datatable-ajax').DataTable( {
"ajax": {
"url": "scripts/json.php",
"dataSrc": ""
},
"columns": [
{ "data": "date" },
{ "data": "amt" },
{ "data": "status" },
]
} );
Add following function to $(document).ready(function() { . I have kept 3 seconds delay to take care of ajax. You can adjust the same. 将以下函数添加到$(document).ready(function(){。为了保留ajax,我保持了3秒的延迟。您可以对其进行调整。
setTimeout(
function()
{
//do something special
$('table>tbody>tr>td:nth-child(3)').each(function() {
//alert($(this).text());
if($(this).text() === "Paid" ){
$(this).addClass('label label-succcess');
}
else if($(this).text() === "Unpaid" ) {
$(this).addClass('label label-danger');
}
else if($(this).text() === "Pending" ) {
$(this).addClass('label label-warning');
}
});
}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.