Laravel-datatables: individual column searching

Created on 18 May 2016  路  8Comments  路  Source: yajra/laravel-datatables

Hello,

In issue #25 it is mentioned that it is possible to add single column searching and an example was added. Is this still valid, as i cant seem to find the demo with this example, nor am i able to implement this with the datatables. I keep getting the this.api(...).columns(...).every is not a function javascript error when implementing this into my code.

Most helpful comment

You need to add a footer on your table, try passing a second param on table method of true like to draw the footer.

{{ $dataTable->table(['class' => 'table'], true) }}

All 8 comments

Yes it's possible, see this demo for ref.

But i am using the Datatables Service classes, and in this case it is not working.

You need to add a footer on your table, try passing a second param on table method of true like to draw the footer.

{{ $dataTable->table(['class' => 'table'], true) }}

See this PR for some ref.

I dont think that this is the problem, i added the footer but i'm still getting this issue.

this is my getBuilderParameters as requested by the DataTables class:

protected function getBuilderParameters()
    {
        return [
            'order'   => [[1, 'desc']],
            'initComplete' => "function () {
                this.api().columns().every(function () {
                var column = this;
                var input = document.createElement(\"input\");
                $(input).appendTo($(column.footer()).empty())
                .on('change', function () {
                    column.search($(this).val(), false, false, true).draw();
                });
            });
            }"
        ];
    }

The error is throw on the first line of the initComplete function -> 'this.api().columns() is not a function'

Has someone already have a working example of column searching through the DataTables Service class?

Hey, i'm actually trying to implement this demo.
Does we need any server processing ? I just make the same exact code with my model, and all search inputs make the table refresh with no data (but it should).
Anything i miss ? thx

PS: related to my comment at #594

I just implement this issue on demo https://datatables.yajrabox.com/users and works well for me. Thanks!

Thanks a lot for answer. I finally did it :)
But i'm using the non-POST method.

I cannot get the POST way to get working. It seems that the code in the filterColumn is never triggered (also tried with the same .whereRaw code but result is unchanged)

Previously, i was using the ->filter(function ($query) to do something like :

  $user_id = Input::get('id');
                 if ($user_id) {
                     $query->where('users.id', 'like', "%$user_id%");
                 }

And it is working perfectly. But doing this, i override and lost the default text search.

I tried the same in filterColumn but nothing change, even a dd() dont do anything.
So :

  • Am I doing something wrong with the filterColumn ?
  • Is the POST demo only have code for ID column filtering (in code sample) even if all column are searchable in demo ? (I only see ID related code in the filter part of the code sample provided).
  • Latest solution: how can i in my override of ->filter re-implement the global text search (in case i cannot do it with filterColumn ?

Thanks a lot, really !

EDIT :
I was able to do what i wanted using this :

 $dt = app('datatables')->of($data);
 if ($t = Input::get('id')) {
            $dt->where('users.id', 'like', "%$t%");
        }

Now i got only two problem left :

  • On a relation column, i use
->editColumn(
                'account_type_id', function ($users_data) {
                return $users_data->accountType->name;
            })

To get the name of the relation (User hasOne accountType). But column search is working on the accountType ID, not the name, even if the name is correctly displayed.

  • When $dt->where(), is there any way to make it work using belongsToMany/hasMany with Pivot table and clean Laravel relation on correct Models ?

Thx again !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

jackrsantana picture jackrsantana  路  3Comments

t0n1zz picture t0n1zz  路  3Comments

hari-web picture hari-web  路  3Comments

techguydev picture techguydev  路  3Comments