Home > Enterprise >  Pre-Filter jQuery DataTables AJAX result based on specific column value
Pre-Filter jQuery DataTables AJAX result based on specific column value

Time:04-22

I have a JSON file containing all the data, so basically it's the data for 10 different tables in one file. Now, when I'm on a certain page (A) I only wish to output the data from the file where the value of column[0] (my identifier sits in the first column) is "A".

I'm really not sure what to put where:

$('#example').DataTable( {
    "ajax": '../ajax/data/arrays.txt'
} );

Any ideas? Thanks!

Got it working.

$('#example').dataTable( {
    "ajax": {
        "url": "../ajax/data/arrays.txt",
        "dataSrc": function(json) {
            let data = json.data;
            let filter = data.filter(function(value, index, arr){
                return value[0] === 'A';
            });
            return filter;
        }
    }
} );

CodePudding user response:

You can use dataSrc option of ajax configuration:

$('#example').DataTable( {
    "ajax": {
        url: '../ajax/data/arrays.txt', 
        dataSrc: function(json) {
            // run logic on json to select only your desired rows
            return result;
       } 
});
  • Related