Search code examples
ajaxlaraveldatatableyajra-datatable

A non well formed numeric value encountered when creating a custom Yajra Datatable


I am using Laravel and I want to create a custom server side Yajra Datatable.

My Ajax call is below:

let table = $('#myTable').DataTable({
            "bLengthChange": false,
            "iDisplayLength": 20,
            "info": false,
            processing: true,
            serverSide: true,
            ajax: {
                url: "/myURL",
                dataSrc: '',
                data: function (d) {
                    d.start = '2020-04-01';
                    d.end = '2020-07-20';
                    d.table = true;
                },
            },
            columns: [
                {data: 'name', name: 'name'},
                {data: 'nameMerged', name: 'nameMerged'},
                {data: 'count', name: 'count'},
            ],
        });

        $("#myTable").append('<tfoot><tr><th>' + 'Total:' + '</th><th></th><th>'
            + total + '</th></tr></tfoot>');

The controller for the ajax call is getting an array from another function that looks like (tableObject) and transforms the array into a Datatable.

DataTable transform function:

public function transformTable($start, $end)
{
    $tableObject = $this->getTableData($start, $end);

    return DataTables::of($tableObject['datasets'])
        ->addIndexColumn()
        ->addColumn('name', function ($row) {
            return $row->name;
        })
        ->addColumn('nameMerged', function ($row) {
            return $row->nameMerged;
        })
        ->addColumn('count', function ($row) {
            return $row->count;
        })
        ->setRowClass(function ($data) {
            return 'tr-basic';
        })
        ->with('total', $tableObject['total'])
        ->make(true);
}

Table in Blade file:

<div>
    <h2>Employees:</h2>
    <table id="myTable" class="bravo-list">
        <thead>
            <tr>
                <th class="th-toggler"></th>
                <th class="th-fullname" id="th-employee">Mitarbeiter</th>
                <th class="th-fullname" id="th-count"># Bravos</th>
            </tr>
        </thead>
    </table>
</div>

This, however, returns the following error:

A non well formed numeric value encountered


Solution

  • The error occurs beacause the variables "start" and "end" in the ajax call are reserved keywords. I changed the name of the variables and it works now as aspected.