yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 858 forks source link

fnCreatedCell: passing a JS function from PHP #1518

Open Bango1999 opened 6 years ago

Bango1999 commented 6 years ago

I can't seem to pass a valid JS function from the fnCreatedCell field of my getColumns() function in PHP

I can pass the JS function, which is encapsulated in a string, however that fails in the client-side with the error: Uncaught TypeError: n.fnCreatedCell.call is not a function because it isn't a function, its a string.

I don't know how else to pass this function without getting a PHP syntax error from trying to render the JS as PHP...

I have done the same thing when defining the createdRow parameter, using a string-encapsulation of the JS function, and that works as expected. Just not in this case, with fnCreatedCell

Snippets

OrderDataTable.php

private function getColumns()
{
    return [
        'customer' => [
          'name' => 'customer.name',
          'data' => 'customer.fullname',
          'fnCreatedCell' => 'function (nTd, sData, oData, iRow, iCol) {console.log("hit");}'
        ],
       ...
       ...
    ];
}

Here is where I'm doing pretty much the same thing on the createdRow parameter, which is successful

public function html()
{
    return $this->builder()
        ->columns($this->getColumns())
        ->addAction(['width' => '10%', 'title' => 'Actions'])
        ->ajax('')
        ->parameters(
            [
               ...
               ...
                "createdRow" => 'function( row, data, dataIndex ) {
                  if (data.regular_order) {
                    if (data.regular_order.is_active) {
                      $(row).addClass("regular_order_row");
                    } else {
                      $(row).addClass("inactive_regular_order_row");
                    }
                  }
                }',
            ]
        );
}

JS Comparison

I can see from comparing sources I've found online to my own rendered+prettified JS source code, that I need the function to be de-stringified in order for it to work properly.

JS given to the client in my app:

$("#dataTableBuilder").DataTable({
        "serverSide": true,
        "processing": true,
        "ajax": "",
        "columns": [{
            "name": "customer.name",
            "data": "customer.fullname",
            "fnCreatedCell": 'function (nTd, sData, oData, iRow, iCol) {console.log("hit");}',
            "title": "Customer",
            "orderable": true,
            "searchable": true,
            "attributes": []
        },
        ...
        ...

JS I've found online from working examples:

$('#users').dataTable( {
    "lengthMenu": [ [5,10, 25, 50, -1], [5,10, 25, 50, "All"] ],
    "ajax": "http://domain.com/Users.txt",
    "columns": [
     ...
     ...
    { "data": "TelephoneNumber",
        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
            $(nTd).html("<a href='tel:"+oData.TelephoneNumber+"'>"+oData.TelephoneNumber+"</a>");
        }
    },
    ...
    ...

So now hopefully you can see my problem. I need the value of fnCreatedCell to be an actual JS function, and not a string.

System details

yajra commented 6 years ago

It seems like this is not yet supported by the builder. Will try to implement this when I got the chance. Please do not hesitate to submit a PR if you can. Thanks!

Bango1999 commented 6 years ago

Thanks for the reply and explanation. I don't know how I would fix it, but good to know the issue has been confirmed and noted.

Also thanks for all your hard work and consistent support on this great lib!

Bango1999 commented 6 years ago

I was able to find a workaround, going off of #364.

Instead of addColumn, I used editColumn, and instead of performing cell functions via client side JS, I am instead using PHP to achieve the same effect, as per the linked issue.

Essentially, I am inserting a link into certain column(s), and then making sure it renders as HTML using rawColumns.

NOTE: This does affect column sorting according to the html (link) I am adding to the cells, which can very easily be a negative drawback, if adding the links makes the column sort in a different way. Luckily for me, in this specific instance, that is not the case.

I'd still like to be able to do it the right way, and not have to worry about HTML in cells breaking the sorting feature. But like I said, I have solved my immediate problem.