Laravel-datatables: How to hide column in the datatable service implemenation

Created on 18 Jul 2018  路  6Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

I am using yajra datatable service implementation . I want to hide one column in the view and in the print section but I am not being able to do so .

Code of UsersDataTable

class UsersDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @return \Yajra\DataTables\DataTableAbstract|\Yajra\DataTables\EloquentDataTable
     */
    public function dataTable()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('action',function ($user){
                if (auth()->user()->hasPermission("UsuariosFull")){
                    return "
                     <a href=\"".route("users.edit",$user->ID_Usuario)."\" class=\"btn btn-success \" >".trans("action.edit")."</a> ".
                        "<a  href=\"#\" onclick=\"deleteItem('$user->ID_Usuario')\" class=\"btn btn-danger\">".trans("action.delete")."</a>";
                }else{
                    return "";
                }

            })
            ;
    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function query()
    {
        $query = User::query()->select($this->getColumns());

        return $this->applyScopes($query);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->minifiedAjax('')
                    ->addAction(['printable' => false])
                    ->parameters([
                        'dom'     => 'Bfrtip',
                        'order'   => [[0, 'desc']],
                        'buttons' => [
                            'print',
                        ],
                    ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'ID_Usuario',
            'Nombre',
            'Login',
            'CodigoBarras',
            'EnUso'
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'users_' . time();
    }
}

I am using this two version of package

  "yajra/laravel-datatables-buttons": "^2.0",
        "yajra/laravel-datatables-oracle": "~8.0"

I want to hide ID_Usuario column . I tried other option also but I think they are all for older package

question

Most helpful comment

In your UsersDataTable you can define the columns as visible or not.

/**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'ID_Usuario',
            'Nombre',
            'Login',
            'CodigoBarras',
            'EnUso' => ['visible' => false, 'printable' => false, 'exportable' => true], // Will be hidden in html and print, but included in export
        ];
    }

All 6 comments

Hi,
For hide ID_Usuario in print view you can do something like this in your print view(resources/views/vendor/datatables/print.blade.php):

@foreach($data as $row)
                @php
                    $count = count($row);
                    $a = count($row);
                @endphp
                @if ($row == reset($data))
                    <tr>
                        @foreach($row as $key => $value)
                            @if($key!='ID_Usuario')
                                <th>{!! $key !!}</th>
                            @endif
                        @endforeach
                    </tr>
                @endif
                <tr>
                    @foreach($row as $key => $value)
                        @if(is_string($value) || is_numeric($value))
                            @if(--$count == $a-1)
                                @continue
                            @endif
                            <td>{!! $value !!}</td>
                        @else
                            <td></td>
                        @endif
                    @endforeach
                </tr>
            @endforeach

in view do something like this

Thanks for the answer . But if I want to hide more of the column then it would be hard , wouldn't it ?. I have ID to hide from another table also

I think you cant remove column in your UsersDataTable you should do as i said before.

In your UsersDataTable you can define the columns as visible or not.

/**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'ID_Usuario',
            'Nombre',
            'Login',
            'CodigoBarras',
            'EnUso' => ['visible' => false, 'printable' => false, 'exportable' => true], // Will be hidden in html and print, but included in export
        ];
    }

@oranges13 I just help him to hide columns in every where

Hello,
Also on service implementation, I'm trying to export to excel a table of 45 columns (with only 10 column visible on datatable). as a result datatable does not show any records and jquery return this msg:
net::ERR_CONNECTION_CLOSE on draw

details from jquery line 9631:
// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
xhr.send( ( options.hasContent && options.data ) || null );

Could it be a limitation on the size of the request?

if so, is there a way to export all columns without having to charge the request?

many thanks for your support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yahav picture Yahav  路  16Comments

jay-shah-rushiinfotech picture jay-shah-rushiinfotech  路  19Comments

ZAZmaster picture ZAZmaster  路  15Comments

ruchisheth picture ruchisheth  路  16Comments

fanjavaid picture fanjavaid  路  32Comments