Wednesday, March 8, 2017

Laravel Datatables advanced search - Ajax always returns default value from dropdown list

I have been trying to create a drop down filter for my laravel database following this example. However, my code always returns the default value from the dropdown list even through the url query changes correctly eg homestead.app/?search_group_filter=variables but my code always returns the default value of url.

php controller

public function globalFilter(Request $request)
{
    $tags = Unknown_Tag::groupBy('url');
    $datatables = Datatables::of($tags);

    debugbar::warning($request->get('searchGroup'));//both return url
    debugbar::warning($request->searchGroup);
    if ($name = $request->get('searchGroup')) {
        $datatables->where($request->searchGroup, 'like', 'http://b.scorecardresearch.com/p?c1=9');
    }
    return $datatables->make(true);
}

javascript

<script>
$(function() {
    var table = $('#ut-table');
    table.dataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: 'http://homestead.app/globalFilter',
            data: function (d) {
                d.searchGroup = $('select[name=search_group_filter]').val();
            }
        },
        columns: [
            { data: 'protocol'},
            { data: 'domain'},
            ...
        ]
    });
    $('#search_group_filter').on('submit', function(e) {
        table.draw();
        e.preventDefault();
    });
});

HTML

<table border="0" cellspacing="5" cellpadding="5">
    <tbody>
    Search
    <tr>
        <form>
        <select name='search_group_filter', id='search_group_filter'>
            <option value='url'>Full URL</option>
            <option value='protocol'>Protocol</option>
            <option value='domain'>Domain</option>
            <option value='path'>Path</option>
            <option value='query'>Full Query</option>
            <option value='variables'>Variables</option>
            <option value='values'>Values</option>
            <option value='occurrences'>Occurences</option>
        </select>
        <button type="submit">search</button>
        </form>
    </tr>
    </tbody>
</table>



via ConnorP

Advertisement