在backgridjs中的页脚

时间:2014-07-23 02:24:59

标签: javascript json backbone.js backgrid

我在下面有一张backgridjs表。

var columns = [{
    name: "_id",
    label: "account#",
    editable:"false",
    cell: "string"
  }, {
    name: "class",
    label: "class",
    editable:"false",
    cell: "string"
  },{
    name: "date",
    label: "Date",
    editable:"false",
    cell: "string"
  },{
    name: "id",
    label: "id",
    editable:"false",
    cell: "string"
  },{
    name: "total",
    label: "Total",
    editable:"false",
    cell: Backgrid.NumberCell.extend({
      orderSeparator: ','
    }),
    cell: "number"
  }];

var PageableTerritories = Backbone.PageableCollection.extend({
  model: Territory,
  url: "/payroll",
  state: {
    pageSize: 15
  },
  mode: "client" // page entirely on the client side
});

var pageableTerritories = new PageableTerritories();

var CaptionFooter = Backgrid.Footer.extend({
  render: function () {
    this.el.innerHTML = '<tr><td></td><td></td></tr>'
    return this;
  }
});

// Set up a grid to use the pageable collection
var pageableGrid = new Backgrid.Grid({
  columns: [{
    // enable the select-all extension
    name: "",
    cell: "select-row",
    headerCell: "select-all",
  }].concat(columns),
  collection: pageableTerritories,
  footer: CaptionFooter // <--
});

// Render the grid
var $example1 = $("#example-1-result");
$example1.append(pageableGrid.render().el)

// Initialize the paginator
var paginator = new Backgrid.Extension.Paginator({
  collection: pageableTerritories
});

// Render the paginator
$example1.after(paginator.render().el);

// Initialize a client-side filter to filter on the client
// mode pageable collection's cache.
var filter = new Backgrid.Extension.ClientSideFilter({
  collection: pageableTerritories,
  fields: ['_id',"Order Class","techId"],
  placeholder: "search"
});

// Render the filter
$example1.before(filter.render().el);

// Add some space to the filter and move it to the right
$(filter.el).css({float: "right", margin: "20px"});

// Fetch some data
pageableTerritories.fetch({reset: true});

一切都很好,正如预期的那样,除了我很难弄清楚如何为总列设置总页脚单元格。

如果我在控制台中输入以下命令,我会获得所需的页脚单元格。

for (var i = 0,total = 0, len = pageableTerritories.toJSON().length; i < len; ++i) {
  total = total + pageableTerritories.toJSON()[i].total;
  $('tfoot').text(total)
};

但我希望在呈现表格时显示页脚单元格。我尝试了以下但总计显示$ 0.

var CaptionFooter = Backgrid.Footer.extend({

  render: function () {
   for (var i = 0,total = 0, len = pageableTerritories.toJSON().length; i < len; ++i) {
      total = total + pageableTerritories.toJSON()[i].total;
      $('tfoot').text(total)
    };
    this.el.innerHTML = '<tr><td></td><td></td></tr>' + total <---- $0 here
    return this;
  }

});

如果有人能帮我解决这个问题,我将不胜感激。就像提前感谢一样。

2 个答案:

答案 0 :(得分:0)

这解决了我的页脚总数问题,但是当我分页时页脚总数没有更新。

var PageableTerritories = Backbone.PageableCollection.extend({
  model: Territory,
  url: "/payroll",
  total: function() {
      return this.reduce(function(memo, value) {
        return memo + value.get("total");
      }, 0);
    },
  state: {
    pageSize: 15
  },
  mode: "client" // page entirely on the client side
});

var CaptionFooter = Backgrid.Footer.extend({
    initialize: function() {
      _.bindAll(this);
      this.collection.bind('sync', this.total);
    },
    total: function(evt) {
      var total = evt.total()
      this.$el.html("<tr><td colspan='7' class='text book bold align right'>Gross Pay: $" + total + "</td></tr>");
    }
  });

答案 1 :(得分:-1)

看看Backgridjs页脚周围的这个简单包装器,提供一个Totals Footer。

https://bitbucket.org/base79/backgridjs-total-footer/overview

对于您的示例,请更新列以包含:

var columns = [{
    ...
    {
        name: 'total',
        label: 'Total',
        ...
        footerCell: TotalFooter.Cell.extend({
            getValue: function () {
                return '$' + this.formatter.fromRaw(this._sumColumn());
            }
    })
}];

然后pageableGrid变为:

var pageableGrid = new Backgrid.Grid({
  columns: [{
    // enable the select-all extension
    name: "",
    cell: "select-row",
    headerCell: "select-all",
  }].concat(columns),
  collection: pageableTerritories,
  footer: TotalFooter.Footer
});