Laravel-datatables: Column Visibility in Datatable as Service

Created on 15 Dec 2015  路  9Comments  路  Source: yajra/laravel-datatables

Hello, i want to hide some column in Ver. 6.0 Library.
Here is my html() code :

return $this->builder()
            ->columns([
                'id_paket',
                'nama_paket',
                'pagu',
                'satker_name',
                'tanggal_awal_pengadaan',
                'tipe',
                'tanggal_pengumuman',
                'status',
                'action'
            ])
            ->ajax('')
            ->parameters([
                // 'dom' => 'lBfrtip',
                'dom' => '<"dt-panelmenu clearfix"lBfr>t<"dt-panelfooter clearfix"ip>',
                'buttons' => ['csv', 'pdf', 'print'],
                'aoColumnDefs' => [
                    'aTargets' => [ 5 ],
                    'mRender' => function ( data, type, full ) {
                        return '<strong>'+ (data == 0) ? 'Penyedia' : 'Swakelola' +'</strong>';
                    }
                ],
            ]);

I know in section aoColumnDefs will get error.
How to implement that?

Most helpful comment

@fanjavaid, it's not yet possible to pass a function from builder class. However, you can just directly set visible: false from your columns scripts like:

return $this->builder()
            ->columns([
                'id_paket' => ['visible' => false],
                'nama_paket',
                'pagu',
                'satker_name',
                'tanggal_awal_pengadaan',
                'tipe',
                'tanggal_pengumuman',
                'status',
                'action'
            ])

All 9 comments

@fanjavaid, it's not yet possible to pass a function from builder class. However, you can just directly set visible: false from your columns scripts like:

return $this->builder()
            ->columns([
                'id_paket' => ['visible' => false],
                'nama_paket',
                'pagu',
                'satker_name',
                'tanggal_awal_pengadaan',
                'tipe',
                'tanggal_pengumuman',
                'status',
                'action'
            ])

@fanjavaid, as of this PR #300, you can now add a function callback js on column render property. Check it out! Thanks!

For visibility, just set visible => false.

I couldn't figure out, how to hide column using render property. Tried from PR #300 , but wasn't any result. Can you share an example code please?

Why don't u write it in to getColumns method like:

 /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            ["title" => "title 1", "data" => "created_at", "name" => "created_at"]
            ["title" => "title 2", "data" => "updated_at", "name" => "updated_at","visible"=>false],
            ["title" => "title 3", "data" => "updated_by.full_name", "name" => "full_name","orderable"=>false],
        ];
    }

This is more readable then getBuilderParameters method :)

@salihklc "visible"=>false This method doesn't work, tried a lot of time. orderable, searchable works, visible doesn't work.

This code tested and works like charm maybe you miss something else in your service class. Would you paste of your YourCustomServiceDataTable.php code here maybe I could help :)

Abi, amina koydugum bilmirem neden chalishmiyor :)
Want to hide id column. Of course, i can just delete the ['data' => 'id', 'title' => 'Id', 'visible' => false]. But in this case, I will not able order by id.

public function html()
{
    return $this->builder()
                ->columns($this->getColumns())
                ->ajax('')
                ->addAction(['width' => '80px', 'orderable' => false, 'searchable' => false])
                ->parameters($this->getBuilderParameters());
}



protected function getColumns()
{
    return [
        ['data' => 'id', 'title' => 'Id', 'visible' => false],
        ['data' => 'published_at', 'title' => 'Date', 'searchable' => false],
        ['data' => 'title', 'title' => 'Title','orderable' => false],
        ['data' => 'name', 'title' => 'Category', 'searchable' => false],
        ['data' => 'status', 'title' => 'Status', 'searchable' => false],
        ['data' => 'featured', 'title' => 'Featured', 'searchable' => false],
        ['data' => 'published_by', 'title' => 'Published by','orderable' => false, 'searchable' => false],
    ];
}



protected function getBuilderParameters()
{
    return [
        'responsive' => true,
        'filter' => true,
        'order' => [ [0,'desc'] ],
        'lengthMenu' => [10,25,50]
    ];
}

Looks like u miss ajax url. What is controller index url, you have to add the url to ->ajax('HERE') :)
Did you get any alert or network error message from datatable ? Do you have colvis button ?

I'm having trouble with this too.

Updated to v7
The columns are hidden until the ajax loads, at which point they are made visible again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alejandri picture alejandri  路  3Comments

macnux picture macnux  路  3Comments

nasirkhan picture nasirkhan  路  3Comments

hohuuhau picture hohuuhau  路  3Comments

techguydev picture techguydev  路  3Comments