Monday, March 6, 2017

DataTables warning 7: Column Filtering not working

I am using the yajra/laravel-datatables package to generate DataTables in my Laravel application. I have a datatables working, but the custom filtering isn't working. I have tried several examples shown in the GitHub repo and their docu website, but I can't get it to work. The error log shows and 500 External Error when I enable a custom search. Before using the custom search, the datatable is generated properly. The view:

<div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Custom Filter</h3>
            </div>
            <div class="panel-body">
                <form method="POST" id="search-form" class="form-inline" role="form">

                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" name="name" id="name" placeholder="search name">
                    </div>
                    <button type="submit" class="btn btn-primary">Search</button>
                </form>
            </div>
        </div>

Partial Controller code:

    $advicePreparations = $advicePreparationsQuery->get();

    $datatables = Datatables::of($advicePreparations)           

        if ($name = $datatables->request->get('name')) {
            $datatables->where('name', 'like', "$name%");
        }

        return $datatables->make(true);

I have tried changing this query to for example: $datatables->where('advice_protocols.name', 'like', "$name%"); but still, nothing happens!

Script:

<script type="text/javascript">
    $(document).ready(function() {
        oTable = $('#advicePreparations-table').DataTable({

            "processing": true,
            "serverSide": true,
            "ajax": {
                url: "",
                data: function (d){
                    d.name = $('input[name=name]').val();
                }
            },
            "columns": [
                {data: 'name', name: 'name'},
                {data: 'category', name: 'category'},
                {data: 'question_name', name: 'goal'},
                {data: 'mergeColumn', name: 'mergeColumn'},
                {data: 'autheur', name: 'autheur'},
                {data: 'active', name: 'active'},
                {data: 'acties', name: 'acties', orderable: false, searchable: false},
                {data: 'delete', name:'delete', orderable: false, searchable: false}
            ]
        });
        $('#search-form').on('submit', function(e) {
            oTable.draw();
            e.preventDefault();
        });
    });
</script>



via Anna Jeanine

Advertisement