-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathFooTable.NumberColumn.js
64 lines (61 loc) · 2.83 KB
/
FooTable.NumberColumn.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
(function($, F){
F.NumberColumn = F.Column.extend(/** @lends FooTable.NumberColumn */{
/**
* The number column class is used to handle simple number columns.
* @constructs
* @extends FooTable.Column
* @param {FooTable.Table} instance - The parent {@link FooTable.Table} this column belongs to.
* @param {object} definition - An object containing all the properties to set for the column.
* @returns {FooTable.NumberColumn}
*/
construct: function(instance, definition){
this._super(instance, definition, 'number');
this.decimalSeparator = F.is.string(definition.decimalSeparator) ? definition.decimalSeparator : '.';
this.thousandSeparator = F.is.string(definition.thousandSeparator) ? definition.thousandSeparator : ',';
this.decimalSeparatorRegex = new RegExp(F.str.escapeRegExp(this.decimalSeparator), 'g');
this.thousandSeparatorRegex = new RegExp(F.str.escapeRegExp(this.thousandSeparator), 'g');
this.cleanRegex = new RegExp('[^\-0-9' + F.str.escapeRegExp(this.decimalSeparator) + ']', 'g');
},
/**
* This is supplied either the cell value or jQuery object to parse. Any value can be returned from this method and will be provided to the {@link FooTable.Column#formatter} function
* to generate the cell contents.
* @instance
* @protected
* @param {(*|jQuery)} valueOrElement - The value or jQuery cell object.
* @returns {(number|null)}
* @this FooTable.NumberColumn
*/
parser: function(valueOrElement){
if (F.is.element(valueOrElement) || F.is.jq(valueOrElement)){
var data = $(valueOrElement).data('value');
valueOrElement = F.is.defined(data) ? data : $(valueOrElement).text().replace(this.cleanRegex, '');
}
if (F.is.string(valueOrElement)){
valueOrElement = valueOrElement.replace(this.thousandSeparatorRegex, '').replace(this.decimalSeparatorRegex, '.');
valueOrElement = parseFloat(valueOrElement);
}
if (F.is.number(valueOrElement)) return valueOrElement;
return null;
},
/**
* This is supplied the value retrieved from the {@link FooTable.NumberColumn#parse} function and must return a string, HTMLElement or jQuery object.
* The return value from this function is what is displayed in the cell in the table.
* @instance
* @protected
* @param {number} value - The value to format.
* @param {object} options - The current plugin options.
* @param {object} rowData - An object containing the current row data.
* @returns {(string|HTMLElement|jQuery)}
* @this FooTable.NumberColumn
*/
formatter: function(value, options, rowData){
if (value == null) return '';
var s = (value + '').split('.');
if (s.length == 2 && s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, this.thousandSeparator);
}
return s.join(this.decimalSeparator);
}
});
F.columns.register('number', F.NumberColumn);
})(jQuery, FooTable);