Search code examples
javascriptbackbone.jspaginationunderscore.jsbackgrid

Backgrid paginator not working after filtering collection


I am trying to implement backgrid pagination on my backbone collection. It works fine until I implement the filter. After I filter my collection, the paginator's page number changes but when I click the other page, nothing happens. Here's my model and collection with the filter function:

var Project = Backbone.Model.extend({});
var ProjectCollection = Backbone.PageableCollection.extend({
    model: Project,
    state: {pageSize: 2},
    mode: "client",
    queryParams: {
        currentPage: "current_page",
        pageSize: "page_size"
    },
    filterBy: function(filterBy,projectName) {
        var filteredProj = this.fullCollection.filter(function(model) {
            if(model.get(filterBy).indexOf(projectName) !== -1) {
                return true;
            }
            return false;
        });
        return new ProjectCollection(filteredProj);
    },
    sortBy: function(sortingKey) {
        this.setSorting(sortingKey);
        this.fullCollection.sort();
    },
});

Here's my pagination view:

var PaginationView = Backbone.View.extend({
    tagName:"div",
    el:".backgrid-paginator",
    initialize: function(){
        _.bindAll(this,'render','filterRender');
        this.collection.on("reset",this.render);
        this.collection.on("filter-by-project", this.filterRender);
    },

    filterRender: function (filterBy,projectName) {
        if (!projectName) this.render;
        var filteredProj = this.collection.filterBy(filterBy,projectName);
        var filteredPaginator = new Backgrid.Extension.Paginator({
            collection: filteredProj
        });
        this.$el.html("");
        this.$el.append(filteredPaginator.render().el);
        return this;
    },
    render: function(){
        var paginator = new Backgrid.Extension.Paginator({
            collection: this.collection
        });
        this.$el.html("");
        this.$el.append(paginator.render().el);
        return this;
    }
});

Here's the HTML for my table:

<div class="form-group col-md-4 pull-right">
        <input type="text" id="filter" name="filter" class="form-control" placeholder="Filter Projects">
    </div>
        <div class="col-md-12 margin-top" id="responsive-table">
            <table class="table table-responsive" >
                <thead>
                    <th id="sortBy-name">Projects</th>
                    <th id="sortBy-sector">Sector</th>
                    <th id="sortBy-status">Status</th>
                    <th id="sortBy-org">Funding Organisation</th>
                    <th id="sortBy-location">Location</th>
                    <th id="sortBy-budget">Budget</th>
                </thead>
                <tbody id="projects-list">
                </tbody>
            </table>
        <div class="backgrid-paginator"></div>
    </div>

And here's my template

<script type="text/template" id="project-row-template">
            <td><a href="/frontend/activity/details/<%= id %>"><%= name %></a></td>
            <td><%= sector %></td>
            <td><%= status %></td>
            <td><%= fundingOrganisation %></td>
            <td><%= location %></td>
            <td><%= budget %></td>
    </script> 

What am I doing worng? I cannot seem to find the answer anywhere. Also I should mention that I am new to backbone js. Please help me with this. Thanks.


Solution

  • Finally figured out a solution to this. Don't know the root of the problem yet, but here's how I solved it:

    • Trigger resetCollection function first
    • Have filterBy function return this

    So in my code the resetCollection function would be similar to the previous filterBy function.

    resetCollection: function(filterBy,projectName) {
        var filteredProj = this.fullCollection.filter(function(model) {
            if(model.get(filterBy).indexOf(projectName) !== -1) {
                return true;
            }
            return false;
        });
        this.fullCollection.reset(filteredProj);  // Only change in previous filterBy function
    },
    

    And my filterBy method would be simply

    filterBy: function(filterBy,projectName) {
        return this;
    },
    

    So... this is what solved my backgrid pagination problem. I think the previous method should also have worked. I think it could be a bug with either backgrid or backbone.... or my code was faulty :D. Anyway, hope this helps with someone facing the similar problem to this.