forked from mindmup/editable-table
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumeric-input-1.2.js
More file actions
114 lines (106 loc) · 4.56 KB
/
numeric-input-1.2.js
File metadata and controls
114 lines (106 loc) · 4.56 KB
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
/* ulongx ->https://github.com/ulongx/editable-table
numeric-input-1.2.js
这是一个对 editableTableWidget 的一个小扩展,可以定义表格哪几列需要修改,
哪一列是汇总列,还有汇总的计算方式 type * 相乘 + 相加 - 相减
*/
(function () {
"use strict";
$.fn.numericInput = function (options) {
//type * 相乘 + 相加 - 相减
var defaults = {
columns: [], //需要更改的列,默认全部
totalColIndex: -1, //汇总列,默认没有
type: '*'
};
options = $.extend(defaults, options);
var element = $(this),
footer = element.find('tfoot tr'),
dataRows = element.find('tbody tr'),
initialTotal = function () {
var column,total,totalSum;
if(!options.columns.length) {
for (column = 1; column < footer.children().size(); column++) {
total = 0;
dataRows.each(function () {
var row = $(this);
total += parseFloat(row.children().eq(column).text());
});
footer.children().eq(column).text(total);
}
} else {
for (var x in options.columns) {
total = 0, totalSum = 0;
dataRows.each(function () {
var row = $(this);
total += parseFloat(row.children().eq(options.columns[x]).text());
if (options.totalColIndex !== -1){
totalSum += parseFloat(row.children().eq(options.totalColIndex).text());
}
});
footer.children().eq(options.columns[x]).text(total);
if (options.totalColIndex !== -1){
footer.children().eq(options.totalColIndex).text(totalSum);
}
}
}
};
element.find('td').on('change', function (evt) {
var cell = $(this),
column = cell.index(),
total = 0,
totalSum = 0,
totalSumEnd = 0;
if (options.columns.length && $.inArray(column,options.columns) === -1) {
//$.Message.info('本单元不可编辑');
alert('本单元不可编辑');
return false;
}
if (options.columns.length && options.totalColIndex !== -1){
var parentTr = cell.parent().children();
options.columns.map(function (item, i) {
switch(options.type){
case '*':
if (totalSum === 0){
totalSum = parseFloat(parentTr.eq(item).text());
} else {
totalSum *= parseFloat(parentTr.eq(item).text());
}
break;
case '+':
totalSum += parseFloat(parentTr.eq(item).text());
break;
case '-':
totalSum -= parseFloat(parentTr.eq(item).text());
break;
default:
break;
}
});
parentTr.eq(options.totalColIndex).text(totalSum);
}
element.find('tbody tr').each(function () {
var row = $(this);
total += parseFloat(row.children().eq(column).text());
if (options.totalColIndex !== -1) {
totalSumEnd += parseFloat(row.children().eq(options.totalColIndex).text());
}
});
// footer.children().eq(column).text(total);
if (options.totalColIndex !== -1) {
footer.children().eq(options.totalColIndex).text(totalSumEnd);
}else{
footer.children().eq(column).text(total);
}
}).on('validate', function (evt, value) {
var cell = $(this),
column = cell.index();
if (column === 0) {
return !!value && value.trim().length > 0;
} else {
return !isNaN(parseFloat(value)) && isFinite(value);
}
});
initialTotal();
return this;
};
})(window.jQuery);