过滤集合

时间:2015-10-13 04:52:55

标签: javascript backbone.js pagination underscore.js backgrid

我正在尝试在我的主干集合上实现backgrid分页。它工作正常,直到我实现过滤器。在我过滤我的集合后,分页器的页码会改变,但是当我点击其他页面时,没有任何反应。这是我的模型和带有过滤功能的集合:

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();
    },
});

这是我的分页视图:

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;
    }
});

这是我桌子的HTML:

<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>

这是我的模板

<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> 

我在做什么?我似乎无法在任何地方找到答案。另外我应该提一下,我是骨干js的新手。请帮我解决一下这个。感谢。

1 个答案:

答案 0 :(得分:0)

最后找到了解决方案。不知道问题的根源,但这是我解决它的方法:

  • 首先触发resetCollection功能
  • filterBy函数返回this

因此在我的代码中,resetCollection函数与之前的filterBy函数类似。

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
},

我的filterBy方法就是

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

所以...这就解决了我的背景分页问题。我认为以前的方法也应该有效。我认为这可能是背景或主干的错误....或者我的代码有问题:D。无论如何,希望这有助于面对类似问题的人。