0

I have a DataTable and I want to filter it's content depending on what user selects in form. Here is the sample of code I use:

$(document).on('click', '#filter_btn', filterList)

function filterList (event) {

  event.preventDefault()

  var form_data = $('.filter-form').serialize()
  var url = window.location.origin + '/my-amazing-url/'

  $('#dataTable-x').DataTable({
    ajax: {
       url: url,
       type: 'get',
       dataType: 'json',
       data: form_data
    }
  })

  $('#dataTable-x').DataTable().ajax.reload()

}

On server side Django returns following:

...
data = self.get_queryset().values()
    return JsonResponse(data)
...

Yet nothing is changed. How should I modify the code? Thanks.

2 Answers2

0

Here is how I would structure this:

Assumptions:

  1. You have a form: <form id="filter-form">..</form>. Here I am using an ID for the form, rather than a class name.

  2. Inside my "document ready" script, I have the following:

var form_data;

$( "#filter-form" ).submit(function( event ) {
  event.preventDefault();
  form_data = $( this ).serializeArray(); 
  table.ajax.reload();
});

Note the reference to the form ID using #. I prefer this to using a css class name.

The above script takes care of capturing the form data, and then re-running the DataTables ajax call, using table.ajax.reload().

For the table itself, I use a function for the ajax data:

var table = $('#example').DataTable( {
  ajax: { 
    method: "POST", // or GET if you prefer
    url: your URL here!
    data: function() { 
      return form_data 
    }
  },
  // the rest of your table options here
  });

Points to note:

The table is named var table = ... - so that the table can be used by the first fragment I showed: table.ajax.reload().

The data option is a function. This is specific to DataTables (it's not part of the underlying jQuery ajax syntax). More about this is here in the official DataTables documentation.

Assuming you handle the POST form data (or GET query parameters) correctly on the server, this should work.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
0

DataTables can have it's ajax source, where only an URL is needed. Documentation - Ajax source.

OR

You can use you own form to select some select data to provide to DataTables.

And in this case, it you would have to provide the new data to DataTables form the ajax success callback. Half of this answer comes this other answer.

$(document).on('click', '#filter_btn', filterList)

function filterList (event) {

  event.preventDefault()

  var form_data = $('.filter-form').serialize()
  var url = window.location.origin + '/my-amazing-url/'

  $.ajax: {
      url: url,
      type: 'get',
      dataType: 'json',
      data: form_data,
      success: function(response){

        // From the "other answer"

        $('#dataTable-x').clear().draw();
        $('#dataTable-x').rows.add({data:response.data}); // Where response.data is an array
        datatable.columns.adjust().draw();
      }
    }
  })
}
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64