tigrang / cakephp-datatable

JQuery DataTable plugin server-side processing component for CakePHP
47 stars 29 forks source link

Enable footer with the datatable helper ? #76

Closed fdalex closed 6 years ago

fdalex commented 6 years ago

Hi ! How can I display the footer with the helper $this->DataTable->render(...) ? I've seen in the plugin's code 'tfoot' and 'footOptions' but I dont know how to use this. I always get as result :

<div class="dataTables_scrollFoot" style="overflow: hidden; border: 0px; width: auto;">
    <div class="dataTables_scrollFootInner">
        <table class="dataTable no-footer" data-config="requester_view" role="grid" style="margin-left: 0px;">
            <tfoot></tfoot>
        </table>
    </div>
</div>

My final goal is to display total of each columns in the footer with this code (working on a simple datatable project) :

<script>
    datatable.columns('.sorting').every(function (i) {
        if (i > 1) {
            if (this.data().length > 0) {
                var sum = this.data().reduce(function (a, b) {
                    return parseInt(a) + parseInt(b);
                });

                $(this.footer()).html(Intl.NumberFormat('fr-FR', {style: 'decimal'}).format(Math.round(sum)));
            } else {
                $(this.footer()).html('');
            }
        }
    });
</script>

Thanks foy your help ;)

haalmaguer commented 6 years ago

Just replicate the same that you have for the head, on the render method change this.:

$tableFooter = $this->tag('tfoot', $tableHeaders, $theadOptions);

then add your JS and you are done.

fdalex commented 6 years ago

Working perfectly ! Thanks !

$trFooter = $this->Html->tableHeaders($columns);
 echo $this->DataTable->render('requester_view', ['id' => 'datatable', 'tfoot' => $trFooter], $jsOptions);