sdvnksv
sdvnksv

Reputation: 9678

How do I filter an output of an AJAX call in Datatables?

I've succesfully set up a Datatables plugin, created a new table and filled it with content using an AJAX call:

var table= $("#mytable").DataTable({

    ajax: "list.json",
    columns: [
        {"data": "name"},
        {"data": "location"},
        {"data": "date"}
    ]
});

The example above has output all of the items from the JSON file I am importing.

What I want is to filter the output, e.g. fill the table with only the data of those users, whose location is "England".

Datatables is an extremely powerful plugin, so I got lost on this. Any help would be appreciated.

Upvotes: 0

Views: 2889

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58920

You have a few options:

  • Filter the data on the server-side if you're not planning to show locations other than "England"
  • Manipulate data received via Ajax using either ajax.dataSrc option or xhr event
  • Use search or searchCols options to define initial search, either globally or for specific column:

    var table= $("#mytable").DataTable({
       ajax: "list.json",
       columns: [
          {"data": "name"},
          {"data": "location"},
          {"data": "date"}
       ],
       searchCols: [
          null,
          { "search": "England" },
          null
       ]
    });
    

    See this jsFiddle for code and demonstration.

Upvotes: 1

Related Questions