0

I would like to display only a specific set of rows in JQuery Datatables based on some conditions (the query/URL of the page).

I am using REST API to pass the data in AJAX. Here is my full example

$(document).ready(function() {
    //datatables
    $('#mytable').DataTable({
      "ajax": "http://127.0.0.1:8000/api/v1/example/?format=datatables",

     "serverSide": true,
     "processing": true,

      "columns": [
          {"data": "col1"},
          {"data": "col2"},
          {"data": "col3"},
      ],

      "dom": "<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>>" +
             "<'row'<'col-sm-12'tr>>" +
             "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-4'p>>",
    });
} );

I would like to display only a subset of the rows based on some condition over col1 for example.

Is there a way to specify the rows like we specify the columns?

4
  • 4
    It would make the most sense to filter the data on the server side. Commented Mar 19, 2020 at 16:36
  • 1
    Does this answer your question? jQuery DataTables filter rows based on multiple values Commented Mar 19, 2020 at 16:46
  • @RoryMcCrossan yes server-side filtering with Django worked. Thanks for your help. Please add your answer so that I can close the question if this is not too trivial. Commented Mar 19, 2020 at 17:48
  • 1
    To be honest I don't feel that I answered your question as it was just a suggestion of how to best approach the problem. If you'd like you could add your own answer showing the code you used - which is perfectly acceptible. Commented Mar 19, 2020 at 18:41

1 Answer 1

1

As @RoryMcCrossan suggested the answer is to filter server-side. Since I am using Django, the filtering can be done in the views.py.

The page rendering can be done as follows based on some condition:

def page(request):
    items = Item.objects.all()
    items=items.filter(col1="somecondition")
    return render(request, 'page.html', {'items': items})

In this case, the datatable is not called from REST API anymore but directly in the template from the model as described in models.py

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

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.