SSP - formatting null or zero values ...has this changed?
SSP - formatting null or zero values ...has this changed?
Hi. I am using datatables at the core of an app I'm working on, and am loving it. But I've just noticed that where I am using formatting functions to output some html according to a value being one or zero, that the 'zero' html is no longer showing. I am not sure if this is an update behavioural change that I've missed, or something I've inadvertently introduced with my own code. For example, in my SSP script I have the following:
foreach ($configDatatables->getDatatablesCols($category, $dtConfig) as $this->item) {
switch ($this->item['format']) {
case 'text_lim_15':
$format = function($d) { return htmlentities(mb_substr($d, 0, 15)); };
break;
[snip]
case 'mark4':
$format = function($d) { return $d ? '<span class="mark4">4</span>' : '<span class="mark0">4</span>'; };
break;
[snip]
default:
$format = function($d) { return htmlentities($d); };
}
// 'db' = the database column name from which to pull the data
// 'dt' = a unique name for the data so that Datatables can target it after formating
// 'format' = the datatables column formatting data
$columns[] = array(
'db' => $this->item['column'],
'dt' => $this->item['unique_id'],
'formatter' => $format,
);
}
You will note that if I format a column with 'mark4', a span element should be output regardless of the value of $d, just styled differently according to if $d has a value of one or zero. This was working fine the last time I looked, but now I only get an html element if $d has a value.
I have confirmed that the entire loop is being ignored wherever a value is zero or null, but the inner-workings of datatables are beyond my skillset, so I'm not sure how to fix this issue without changing all the data such that all boolean values are 1/2, for example, instead of the more logical 0/1. This would be a huge headache as there is a ton of legacy data to deal with, covering hundreds of tables.
If I study the contents of $columns, I can see this for 'mark4':
[12] => Array
(
[db] => cli_mark4
[dt] => cli_mark4
[formatter] => Closure Object
(
[this] => App\DatatablesController Object
(
[userNames:App\DatatablesController:private] =>
[logActionModel:App\DatatablesController:private] =>
[View] => App\View Object()
[item_url] => /data/resource/edit/client/
[new_url] => /data/resource/new/
[deletion_url] => /ajax/deletion/deleteRecord/
[update_url] => /ajax/update/
[other_url] => /data/resource/edit/
[item] => Array
(
[label] => Details
[column] => cli_ref
[unique_id] => link
[format] => link_edit
[th_class] => all
[td_class] =>
[col_width] =>
[data_type] =>
[filter_type] =>
[other_filters] =>
[orderable] =>
)
)
[parameter] => Array
(
[$d] => <required>
)
)
)
Note the last array element: [$d] => <required> . So I'm thinking that I might be able to configure Datatables to 'not require' a value for $d in order to process it. Am I right, please, and if so, what is the config syntax please?
Thanks in advance
This question has an accepted answers - jump to answer
Answers
Did you update DataTables or the SSP demo script and then saw the error? If so, what version did you update from and to?
Allan
Thanks for responding Allan. According to the logs on my package.json file, I am currently running 1.10.19 but last updated from 1.10.16 a month ago. That fits with my recollection of events. I only noticed the issue today as I have been working on other parts of the system of late. My SSP Class script has been forked beyond recognition from the master I used a couple of years ago (it's a big and complex project), but from my limited understanding of the code-flow, I didn't think that would be relevant to this issue, and cannot see anything pertaining to it, either. Am I wrong?
OK, so I've run through my heavily altered SSP class in comparison to your master and found the problem. When totalling up columns, I needed to be able to process data collected but not shown on the table. I therefore added a conditional that would ignore any column without a 'db' value:
Clearly, after testing, this is the cause of my problem, so I thank you for your time, apologise for wasting it, and thank you, as always, for building and maintaining this great project
I still don't understand why this has caused this problem, but I'll work it out.
I'm operating on the limit of my understanding of your project here, but the following amendment to data_output() does what I want it to without causing any other issues (that I can find so far):
Thanks again