Laravel-datatables: Custom filter in initComplete on a separate blade file

Created on 1 Mar 2018  路  4Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

Hello everyone, specially @yajra for this awesome work you have done with DataTables on Laravel.

I'm trying to put the code of function 'initComplete' to a separate blade file, but with no luck. The error I'm getting is from JS console:

b.fn.apply is not a function

But I supose that is happening here is basically that new blade file is not found by DataTable plugin.

Code snippet of problem

What I'm doing:

Partial of file EmployeesDataTable.php:

    public function html()
    {
        return $this->builder()
        ->columns($this->getColumns())
        ->minifiedAjax()
        ->parameters([
            'order'   => [[0, 'asc']],
            'initComplete' => 'employees.filters',
        ]);
    }

File /resources/views/employees/filters.blade.php:

function () {
    $(".dataTables_filter label input").attr("placeholder", "Search all employees").removeClass("form-control-sm");

    var filters =
    `<form class="form-row align-items-center ml-4">

    <span class="mr-1" title="Filters"><i class="fas fa-filter"></i></span>

    <div class="col-auto d-inline">

    <select class="form-control" id="exampleFormControlSelect1">
        <option>Type</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
    </select>

    </div>

    <button type="submit" class="btn btn-secondary ml-1"><i class="fas fa-filter"></i> Filter</button>

    </form>`; $("#filters").append(filters);  }
  • Operating System: Windows 10
  • PHP Version: 7.1
  • Laravel Version: 5.5
  • Laravel-Datatables Version: 8

Most helpful comment

I found the way to do this, at the end.
It's as easy as that:

'initComplete' => view('employees.filters'),

I hope this helps anyone, and please, consider adding it at the docs.
Thanks @Skintillion for the answers.

All 4 comments

@Skintillion I answer by this thread, because the other was closed. The proposed '@include' don't work: I have no one JS error, and the datatable is working but the filters are not added in DOM.
I followed this thread: https://github.com/yajra/laravel-datatables/issues/422, where @yajra says exactly that, 'initComplete' => 'path.to.view'.

But this don't work. And I need it to multiple reasons:

  • Better organization of code
  • Control if is Admin or not to show more or less filters

Thanks for the replies.

I could not find reference to that anywhere, thank you.

Is it being properly included in your script? What does the source show in your dev tools (Chrome etc)?

Any other errors in console?

I found the way to do this, at the end.
It's as easy as that:

'initComplete' => view('employees.filters'),

I hope this helps anyone, and please, consider adding it at the docs.
Thanks @Skintillion for the answers.

Ah, I meant that when I said @include. I was thinking blade by accident.

And no problem. Dont forget to close issue.

Was this page helpful?
0 / 5 - 0 ratings