This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackgrid-sum.js
124 lines (106 loc) · 3.77 KB
/
backgrid-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function (window) {
var SummationUtility = {
columnsToSum: [],
multiplier: null,
getColumnsToSum: function () {
var columns = this.columns.without(this.columns.findWhere({ name: this.multiplier }));
if (_(this.columnsToSum).isEmpty() === false) {
columns = columns.filter(function (column) {
return this.columnsToSum.indexOf(column.get('name')) !== -1;
}, this);
}
return columns;
},
getSum: function () {
return _(this.getColumnsToSum()).reduce(this.addColumnValue, 0, this);
},
addColumnValue: function (memo, column) {
var value = this.model.get(column.get('name'));
var multiplier = 1;
if (this.multiplier) {
if (isNaN(parseFloat(this.multiplier))) {
multiplier = this.model.get(this.getColumnByName(this.multiplier).get('name'));
} else {
multiplier = parseFloat(this.multiplier);
}
}
return memo + (parseFloat(value) * multiplier);
}
};
var SummedRow = window.Backgrid.SummedRow = window.Backgrid.Row.extend({
formatter: Backgrid.StringFormatter,
render: function () {
this.$el.empty();
var fragment = document.createDocumentFragment();
_(this.cells).each(function (cell) {
fragment.appendChild(cell.render().el);
});
fragment.appendChild(this.getSumCell().render().el);
this.el.appendChild(fragment);
this.delegateEvents();
return this;
},
getColumnByName: function (name) {
return this.columns.findWhere({ name: name });
},
getSumCell: function () {
var _this = this;
return new (
Backgrid.Cell.extend({
className: _this.className || '',
initialize: function () { },
render: function () {
this.$el.html(new _this.formatter().fromRaw(_this.getSum(), _this.model));
return this;
}
})
);
}
});
var SummedColumnBody = window.Backgrid.SummedColumnBody = window.Backgrid.Body.extend({
formatter: Backgrid.StringFormatter,
template: _.template('<td class="<%= className %>"><%= sum %></td>'),
initialize: function () {
Backgrid.Body.prototype.initialize.apply(this, arguments);
this.listenTo(this.collection, 'change add remove reset', this.render);
this.listenTo(this.columns, 'change add remove reset', this.render);
},
render: function () {
window.Backgrid.Body.prototype.render.apply(this, arguments);
this.el.appendChild(this.getSumRow().render().el);
return this;
},
getFormatterForColumn: function (column) {
if (this.formatters && this.formatters[column.get('name')]) {
return new this.formatters[column.get('name')]();
} else {
return new window.Backgrid.StringFormatter();
}
},
getSumRow: function () {
var _this = this;
return new (
Backbone.View.extend({
className: _this.className || '',
tagName: 'tr',
render: function () {
_(_this.getColumnsToSum()).each(function (column) {
var sum = '';
if (_this.columnsToIgnore.indexOf(column.get('name')) === -1) {
var values = _this.collection.pluck(column.get('name'));
var sum = _.reduce(values, function (memo, num) {
return memo + parseFloat(num);
}, 0);
sum = _this.getFormatterForColumn(column).fromRaw(sum, _this.model);
}
this.$el.append(_this.template({ className: _this.className, sum: sum }));
}, this);
return this;
}
})
);
}
});
_(SummedRow.prototype).extend(SummationUtility);
_(SummedColumnBody.prototype).extend(SummationUtility);
})(window);