Skip to content

[Question] How to use columns.render in builder? #1512

@ghost

Description

I ran into an issue with rendering a datetime field using the render method. I have created the same table before I used the Datatable builder and I have no issues as shown in the picture below.

image

The JS code I used is below

        oTable = $('#users-table').DataTable({
            serverSide: true,
            processing: true,
            responsive: true,
            stateSave: true,
            order: [1, 'asc'],
            ajax: {
                url: '{!! route('datatables.data') !!}',
                'data': function (d) {
                    d.searchFor = $('input[name=searchBox]').val();
                    d.lastdate = $('select[name=filter\\[range\\]]').val();
                }
            },
            columns: [
                {data: 'email', name: 'email', orderable: true, searchable: true, width:'15%'},
                {
                    data: 'lastVisitDate',
                    name: 'lastVisitDate',
                    type: 'num',
                    render: { _: 'display', sort: 'timestamp'},
                    orderable: true,
                    searchable: false,
                    width:'10%'
                },
                {
                    data: 'created_at',
                    name: 'created_at',
                    type: 'num',
                    render: { _: 'display', sort: 'timestamp'},
                    orderable: true,
                    searchable: false,
                    width:'10%'
                },
                {data: 'id', name: 'users.id', orderable: true, searchable: false, width:'1%'}
            ],

When I used the builder I get this as an output:
image

The code I used:

 protected function getColumns()
    {
        return [
            ['data'=>'email','name'=>'email', 'title'=>'Email', 'orderable'=>true, 'searchable'=>false, 'width'=>'15%'],
            [
                'data'=>'lastVisitDate',
                'name'=>'lastVisitDate',
                'title'=>'Last Visit Date',
                'type'=>'num',
                'render'=>
                    '{ 
                        _: "display", 
                        sort: "timestamp"
                     }',
                'orderable'=>true,
                'searchable'=>false,
                'width'=>'10%'
            ],
            ['data'=>'created_at','name'=>'created_at', 'title'=>'Registration Date', 'type'=>'num', 'render'=>'{ _: \'display\', sort: \'timestamp\'}', 'orderable'=>true, 'searchable'=>false, 'width'=>'10%'],
            ['data'=>'id','name'=>'id', 'title'=>'Id', 'orderable'=>true, 'searchable'=>false, 'width'=>'2%']
        ];
    }

The response back from from AJAX

{  
   "draw":2,
   "recordsTotal":100,
   "recordsFiltered":100,
   "data":[  
      {  
         "email":"xfeeney@example.com",
         "lastVisitDate":{  
            "display":"1 year ago",
            "timestamp":"1469266436"
         },
         "created_at":{  
            "display":"1 year ago",
            "timestamp":"1469266436"
         },

The AJAX response shows the data is being created correctly but the columns.render is not working as I expected. Is there something I am missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions