In my Phalcon project, I'm trying to change the color of a row based on some data. I'd appreciate any help. This is what I'm working with. In my controller :
private function dataTablesAjax($request)
{
$params = $columns = $totalRecords = $data = [];
$params = $_REQUEST;
$columns = array(
0 => 'payID',
1 => 'userName',
2 => 'refundDays',
);
$builder = $this->modelsManager->createBuilder()
->columns([
'payID' => 'a.payID',
'userName' => 'a.userName',
'refundDays' => 'a.days',
])
->addFrom(AccountsRefunds::class, 'ar');
$builder->orderBy($columns[$params['order'][0]['column']] . " " . $params['order'][0]['dir']);
$builder->limit($params['length'], $params['start']);
$result = $builder->getQuery()->execute();
foreach ($result as $value) {
$nokey = array();
$nokey[] = $value->payID;
$nokey[] = $value->userName;
$nokey[] = $value->refundDays;
$data[] = $nokey;
}
return [
"draw" => intval($params['draw']),
"recordsTotal" => intval($totalRecords),
"recordsFiltered" => intval($totalRecords),
"data" => $data
];
}
Here I just get the data from a query builder and pass the rows in a corresponding view :
<div class="card-body">
<table class="table table-hover responsive" id="dataTables" width="100%" cellspacing="0">
<thead>
<tr>
<th>payID</th>
<th>userName</th>
<th>refundDays</th>
</tr>
</thead>
</table>
</div>
The data from datatables gets rendered as a table body. I just want to somehow wrap this part :
foreach ($result as $value) {
$nokey = array();
$nokey[] = $value->payID;
$nokey[] = $value->userName;
$nokey[] = $value->refundDays;
$data[] = $nokey;
}
In a table row so that I can dynamically render a class based on some data. I don't know if that's possible though.I'd appreciate any help, thank you !
$('#dataTables').dataTable( {
"createdRow": function( row, data, dataIndex){
if( data[2] == `someVal`){
$(row).addClass('dark-background');
} elseif(data[2] == `someOtherVal`) {
$(row).addClass('light-background');
}
}
});
Replace data[2]
with the specific variabele to check, someVal
and someOtherVal
with the value to check for
You can extend this with some more logic, different CSS classes, or even pass things like classnames/colours etc through the data from the API.