最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to apply filters dynamically in nicolaskruchten pivottable.js - Stack Overflow

programmeradmin0浏览0评论

I am using nicolaskruchten pivottable, for displaying my data using:

$('#output').pivot([
    {country: "USA", city: "Boston"},
    {country: "USA", city: "NYC"},
    {country: "UK", city: "London"},
],
{
    rows: ["country"],
    cols: ["city"]
});

Its displaying table properly but now I want to filter my data dynamically in the table.

One of the way is, to filter my available data and redraw pivot table with filter data. I want to know if there is any inbuilt filter method available.

There is an provision for a filter object in the options array of pivot(input [,options]) method but not able to find any example to implement.

If anyone can suggest me how to proceed further ?

I am using nicolaskruchten pivottable, for displaying my data using:

$('#output').pivot([
    {country: "USA", city: "Boston"},
    {country: "USA", city: "NYC"},
    {country: "UK", city: "London"},
],
{
    rows: ["country"],
    cols: ["city"]
});

Its displaying table properly but now I want to filter my data dynamically in the table.

One of the way is, to filter my available data and redraw pivot table with filter data. I want to know if there is any inbuilt filter method available.

There is an provision for a filter object in the options array of pivot(input [,options]) method but not able to find any example to implement.

If anyone can suggest me how to proceed further ?

Share Improve this question edited Aug 16, 2015 at 12:59 nicolaskruchten 27.5k8 gold badges89 silver badges104 bronze badges asked May 28, 2015 at 13:10 MayurMayur 1,1433 gold badges22 silver badges38 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You must re-render the table every time you filter. You can pass a filter attribute which is a function that takes a row as a parameter and returns a boolean. See https://github./nicolaskruchten/pivottable/wiki/Parameters#pivotinput-options

I should add that in general, you should file a Github Issue to get help with this library, rather than asking a question on StackOverflow.

Below is the way i have done it. and it solved my query.

 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 search/select box to filter data dynamically. Data to the filter is fed dynamically from Back-end.

In index.html:

-------------- //search-select box 1 -------------- //search-select box 2 ...

============== //pivot table

You can actually pass a filter attribute. But this will be a better implementation in the memory perspective.

By implementing a default set of records to be displayed, you can limit the data that is getting loaded to Front-end using dynamic filters.

发布评论

评论列表(0)

  1. 暂无评论