How to apply filters dynamically in nicolaskruchten pivottable.js
I am using nicolaskruchten pivottable to display my data using:
$('#output').pivot([
{country: "USA", city: "Boston"},
{country: "USA", city: "NYC"},
{country: "UK", city: "London"},
],
{
rows: ["country"],
cols: ["city"]
});
Its displaying the table as expected, but now I want to dynamically filter my data in the table.
One way is to filter my available data and redraw the pivot table with filter data. I want to know if there is a built-in filtering method.
There is a condition for a filter object in the options array of the pivot (input [, options]) method, but cannot find any example to implement.
If anyone can suggest me how to proceed further?
You have to re-render the table every time you filter. You can pass an attribute filter
, which is a function that takes a string as a parameter and returns a boolean value. See https://github.com/nicolaskruchten/pivottable/wiki/Parameters#pivotinput-options
I must add that in general you should submit a Github Issue to get help with this library, not ask a question on StackOverflow.
Below is the way I did it. and he solved my request.
var filterBy={filterCol:"country",filterValue:["USA"]};
optObject['filter']=function(rowObj){
if(filterBy.filterValue.indexOf(rowObj[filterBy.filterCol])>-1) return true;
else return false;
};
I used a search / select box to filter the data dynamically. Data is supplied to the filter dynamically from the Back-end.
In index.html:
-------------- // search-select box 1 -------------- // search-select box 2 ...
================ pivot table
You can actually pass a filter attribute. But that would be the best implementation from a memory perspective.
By using the default recordset that will be displayed, you can limit the data that is loaded into the Front-end using dynamic filters.