Format numbers displayed in table
Format numbers displayed in table
Hi - please excuse this really noob question - I'm sure it's very simple but I'm just not getting it through lack of knowledge / experience.
I have an array object being served by a PHP back-end (Laravel 5). In my HTML view I iterate the array to build the table like so...
<table id="tableID">
<thead>
<th>Item 1</th>
............
<th>Item x</th>
</thead>
<tbody>
blade foreach($array_elements as $element)
<tr>
<td>{{ $element->1 }}</td>
............
<td>{{ $element->x }}</td>
</tr>
endforeach
</tbody>
</table>
The JS...
$('#tankleveltable').DataTable({
searching: false,
paging: false,
info: false,
ordering: false })
The data elements in the array are strings and numbers. What I need is for my numbers which are coming through raw (e.g. 100000) to be displayed in the table like so 100000 -> 100,000
I know this is a really really stupid question as formatting like this is just standard stuff but how do I do it? Or should I be pre-formatting the numbers before serving to the view?
Thanks in advance.
Len
This question has an accepted answers - jump to answer
Answers
PS - ignore that the table id="tableID" is not the same as $('#tankleveltable').DataTable....
That is not the issue. Everything works - the issue is specifically how to get DT to display numbers formatted correctly... Thanks
Anybody?
Did you try this:
https://datatables.net/reference/option/formatNumber
?
Hi - thanks for the reply.
It turned out to be a real newbie thing just as I suspected ->
https://datatables.net/examples/advanced_init/html5-data-attributes.html
So my solution was to format the numbers how I wanted them on the back-end but also set the 'data-order=' tag inside the <td> element.
BTW - the above also turned out to be a very useful / easy way to sort icons / images (like yes/no ticks)...