Filter using datatables instead of GET?

Filter using datatables instead of GET?

rob1strob1st Posts: 84Questions: 22Answers: 0
edited August 2022 in Editor

So for a number of my pages they have a location, and currently I have a drop down where they select the location, and I reload the page with the extra info in the URL, pass that via GET into session and to the server side.

While this works fine, it does reload the page each time, which can be frustrating.

I am sure I could be doing this via the AJAX call much more efficiently, but I don't know how, so thought I would ask.

Is there a better way of injecting those code into the serverside and having it perform the filter just as it does with the search? Is there examples I could use?

If needed I can link to our test server for an example, but I think the question is simple enough, just I don't know how to do it.

Answers

  • rob1strob1st Posts: 84Questions: 22Answers: 0
  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin

    Can you populate a table using Ajax? That would be the first step. The second would then be to pass your filter value using ajax.data (as a function).

    Allan

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Yes, the page is already populated via ajax using serverSide processing.

    What isn't clear to me is which example I should use.

    Order is:
    1. Page loaded with data
    2. new location selected
    3. data should filter or refresh to the new selected location.

    I assumed this:

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "data": function ( d ) {
          return $.extend( {}, d, {
            "extra_search": $('#extra').val()
          } );
        }
      }
    } );
    

    but didn't seem to do anything.

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Let me put it into the test side and link an example for you Allan, will update for tomorrow. Thanks

  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin

    If you are using server-side processing already (serverSide) then absolutely - all you need to do is add the data you want to what DataTables is sending to the server:

    data: function (d) {
      d.extra = $('#myInput').val();
    }
    

    Allan

Sign In or Register to comment.