perak / kitchen-site

Meteor Kitchen site source code
MIT License
282 stars 38 forks source link

Interactive Filtering of Dataview #92

Open bartonhammond opened 9 years ago

bartonhammond commented 9 years ago

While displaying a table of records in dataview, interactive filtering is not possible.

I would like to filter data with All, Active and Completed based on the boolean field "completed".

See Meteor Kitchen Todo App Code

As an example of what I am seeking, with the following changes I can interactively filter the data.

data_view.js: starting at line 5 by adding pageSession variable "TodosViewFilter"

        //Filter data?
        var filter = pageSession.get("TodosViewFilter");
        var searchString = pageSession.get("SEARCH_STRING_SESSION_VAR");
        var sortBy = pageSession.get("SORT_BY_SESSION_VAR");
        var sortAscending = pageSession.get("SORT_ASCENDING_SESSION_VAR");
        if(typeof(sortAscending) == "undefined") sortAscending = true;

        var raw;
        if (!filter || filter == "") {
                 raw = cursor.fetch();
        } else {
                 raw = Todos.find(filter).fetch(); //expose Todos and filter appropriately
        }
    .....  //unchanged...

Then in my dataview custom_component I can respond to href click events by updating the reactive pageSession variable with the mongo filter:

Template.filterOptions.events({
  "click [href=all]": function(e) {
    e.preventDefault();
    pageSession.set("TodosViewFilter","");
  },
  "click [href=active]": function(e) {
    e.preventDefault();
    pageSession.set("TodosViewFilter",{completed: false});
  },
  "click [href=completed]": function(e) {
    e.preventDefault();
    pageSession.set("TodosViewFilter",{completed: true});
  }
});
perak commented 9 years ago

Filtering requires general multi-purpose solution: possibility define "filterable" fields (no matter are they boolean or another type) and to create interface for filtering. Or... maybe solution for this would be something like "Auto filter" available in "MS Excel".

However, I'l try to make quick solution for this because I wish to include your "todos" application into "official" meteor kitchen examples, and filtering on "completed" field is must-have feature for this kind of app.