Search code examples
jqueryjsonjquery-filter

Returning ordered json data inside filter.js using JsonQuery


I'm using filter.js from https://github.com/jiren/filter.js - great script.

var FJS = FilterJS(products, '#products', {
    template: '#productfinder-template',
    search: {ele: '#searchbox'},
    callbacks: {
        beforeAddRecords: function(records){
            var jq = JsonQuery(records); 
            console.log(jq.order({'artnr': 'asc'}).exec());
        },
        afterFilter: function(result){
            $('#total_products').text(result.length+' records found');
        }
    }
});

Inside the callback "beforeAddRecord" i called the JsonQuery "order" function and the result is an ordered Json list, just the way i wanted. The problem is getting this result on the screen.

Has anyone have experience with this script?


Solution

  • Did some testing and found following working. I put the code on a button click for testing purposes.

    jQuery("#xselected").on('click', function(){ 
        var jq = JsonQuery(products); 
        products = jq.order({'artnr': 'asc','category':'asc'}).exec();
        FJS.removeRecords({});
        FJS.addRecords(products);
        FJS.filter;
    });
    

    Removed all records from the filter and added them back after using the order function from JsonQuery.