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
| /* ulongx ->https://github.com/ulongx/editable-table
numeric-input-1.2.js
*/
(function () {
"use strict";
$.fn.numericInput = function (options) {
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('LOL);
alert('MDR');
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); |
Partager