0

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
            });
        }
});

2 Answers 2

1

Check filtering

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

 *0 corresponds to the column index

Here is a fiddle example

Sign up to request clarification or add additional context in comments.

3 Comments

This will only work for client-side datatables, the OP is using server-side
I thought this also worked with client-side datatables. I thought this function worked like the user typing something in the search field which would "trigger" a server-side search
Yes, the default behaviour is to trigger a search but I assumed the OP was having problems accessing the search parameter in the server-side code. I didn't mean your code was wrong - I just meant that IMHO the answer needed to focus more on passing the parameter. Apologies.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.