PHP.Newbie
PHP.Newbie

Reputation: 195

Search not functioning in DataTables using JQuery

I'm trying to use datatable with the search function. But I don't know how. Please help. here is my code in jQuery.

var oTable = $('#table').DataTable({
        "dom": "Bfrtip",
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": '<?php echo site_url('members/viewall'); ?>',
        "sPaginationType": "full_numbers",
        "fnServerData": function (sSource, aoData, fnCallback) {
            $.ajax
            ({
                'dataType': 'json',
                'type': 'POST',
                'url': sSource,
                'data': aoData,
                'success': fnCallback
            });
        }
});

Upvotes: 0

Views: 66

Answers (2)

markpsmith
markpsmith

Reputation: 4918

You've specified "bServerSide": true, so by default datatables will include your search term in the request to the URL set in sAjaxSource as a querystring parameter Request["sSearch_0"], and it's up to you to retrieve it in your server side code. You will then need to incorporate in the db query. See full list of the sent parameters here.

Assuming that you're using the default setup, your search will be triggered on the keyup event of the searchbox, you can check this by looking in Firebug's Net panel and look at the querystring for the sSearch_0 parameter.

Triggering the search from a custom button for example is as simple as calling fnDraw() in the click event:

oTable.fnDraw();

Note that I've given you a link to the v1.9 documentation. Although you have an uppercase DataTable in your init code, the syntax is very much 1.9 so I'm assuming you will be familiar with it. v1.10 works in a very similar way but the parameter names are different. Here are the params for 1.10

Upvotes: 1

Rafael Teles
Rafael Teles

Reputation: 2748

Check filtering

 table.column(0).search('what you want to filter').draw()

 *0 corresponds to the column index

Here is a fiddle example

Upvotes: 1

Related Questions