laravel-datatables icon indicating copy to clipboard operation
laravel-datatables copied to clipboard

Only show table header (without content) using Html builder via method injection

Open ardianumam opened this issue 7 years ago • 3 comments

I use this method and only show header table without its contents.

But, when I change to this.

if (true) { //change $request->ajax() with true
            $authors = Author::select(['id', 'name']);
            return Datatables::of($authors)->make(true);
}

It can show the content like this:

{"draw":0,"recordsTotal":17,"recordsFiltered":17,"data":[{"id":1,"name":"Ardian"},{"id":2,"name":"Ardian"},{"id":3,"name":"Ardian"},{"id":4,"name":"Cek Nama"},{"id":5,"name":"Cek Nama"},{"id":6,"name":"Cek Nama"},{"id":7,"name":"Cek Nama"},{"id":8,"name":"Mas Jan"},{"id":9,"name":"satu"},{"id":10,"name":"Dua"},{"id":11,"name":"Tiga"},{"id":12,"name":"Jiwa"},{"id":13,"name":"Nama2"},{"id":14,"name":"Nama3"},{"id":15,"name":"Nama4"},{"id":16,"name":"Nama5"},{"id":17,"name":"Nama6"}],"queries":[{"query":"select count(*) as aggregate from (select '1' as row_count from authors) count_row_table","bindings":[],"time":0.72},{"query":"select id, name from authors","bindings":[],"time":0.64}],"input":[]}

How can I resolve this?

Thanks :)

ardianumam avatar Jul 27 '17 12:07 ardianumam

When I insert this code, it returns to "HTTP". What about this?

if($request->ajax()){
                return "AJAX";
            }
            return "HTTP";

ardianumam avatar Jul 27 '17 14:07 ardianumam

if you tested this on your browser directly, it's normal.

if you want to see the json on standard GET requests, for debug :

       if($request['draw'])
        {
            return $datatable->ajax();
        }

lk77 avatar Jul 31 '17 14:07 lk77

SOLUTION 1 Look for this file in your project

vendor/datatables/buttons.server-side.js

The copy it and put it in your assets public folder and then make sure you include it as a scribe before

{{ $dataTable->scripts() }}

so, it should be like this

<script src="{{ url('/') }}/assets/js/buttons.server-side.js"></script>
{{ $dataTable->scripts() }}

and hopefully it will be fixed.

SOLUTION 2 Remove buttons. On your build method in the datatable class, remove buttons by adding something like this

 ->parameters([
        'dom' => 'Bfrtip',
        'buttons' => [],
    ]);

So, the final thing should look like this

public function html()
{
    return $this->builder()
                ->setTableId('users-table')
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->dom('Bfrtip')
                ->orderBy(1)
                -->parameters([
                    'dom' => 'Bfrtip',
                    'buttons' => [],
                ]);
}

CONCLUSION If you still get an error, go to inspect and see if there is any warning. All the best!

mubahood avatar Apr 11 '22 00:04 mubahood

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Dec 31 '23 00:12 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Jan 07 '24 00:01 github-actions[bot]