1
YUI.add('datatable-formatters', function (Y, NAME) {
4
Adds predefined cell formatters to `Y.DataTable.BodyView`.
6
@module datatable-formatters
9
isValue = Lang.isValue,
10
escape = Y.Escape.html,
12
getCName = Y.ClassNameManager.getClassName,
13
cName = function (name) {
14
return getCName('datatable', name);
16
stringValue = function (value, def) {
17
return (isValue(value) ? escape(value.toString()) : def || '');
20
Registry of function producing cell formatting functions.
21
Allows for names to be used in the column
22
definition `formatter` property:
24
{key:"myColumn", formatter:"date"}
26
These functions are not meant to be used directly. Instead, they will be
27
automatically called when their names are used as values for the `formatter`
28
property in a columnd definition.
29
They will be called just once per rendering cycle and will receive
30
the column configuration. They are expected to return a function that will
31
then be called once per row and will do the actual formatting.
32
They are expected to do all the preparatory once-per-render work
33
so that the actual formatting function doesn't need to repeat it.
35
@class DataTable.BodyView.Formatters
40
Returns a formatter that produces a BUTTON element using the value of
41
the [buttonLabel](DataTable.Column.html#property_buttonLabel)
42
column definition attribute as its label or the text
45
Applies the CSS className `yui3-datatable-button` to the cell.
48
@param col {Object} The column definition.
49
@return {Function} A formatter function that produces a `<button>` element.
52
button: function (col) {
53
var className = cName('button'),
54
html = '<button>' + (col.buttonLabel || 'Click') + '</button>';
57
o.className = className;
64
Returns a formatter function that returns the texts `"true"` or `"false"`
65
and assigns the CSS classNames `yui3-datatable-true` or `yui3-datatable-false`
66
based on the value of the cell.
68
If either a [booleanLabels](DataTable.Column.html#property_booleanLabels)
69
configuration object is defined for the column
70
or a [booleanLabels](DataTable.html#attr_booleanLabels)
71
configuration attribute is defined for the datatable,
72
the formatter will use the values for the properties `true` or `false`
73
of either of those objects as the text to show.
75
It returns `null`s or `undefined`s unchanged so that the `emptyCellValue`
76
configuration attribute will eventually apply.
78
{key:"active", formatter: "boolean", booleanLabels: {
85
@param col {Object} The column definition.
86
@return {Function} A formatter function that formats boolean data.
89
'boolean': function (col) {
90
var labels = col.booleanLabels || this.get('booleanLabels') || {'true':'true', 'false':'false'};
93
if (!value && value !== false) {
96
value = value?'true':'false';
97
o.className = cName(value);
103
Returns a formatter function that formats values as currency using
104
the [Number.format](Number.html#method_format) method.
105
It looks for the format to apply in the
106
[currencyFormat](DataTable.Column.html#property_currencyFormat) property
107
of the column or in the
108
[currencyFormat](DataTable.html#attr_currencyFormat)
109
attribute of the whole table.
111
{key: "amount", formatter: "currency", currencyFormat: {
113
decimalSeparator: ",",
114
thousandsSeparator: ".",
118
See [Number.format](Number.html#method_format) for the available format specs.
120
Anything that cannot be parsed as a number will be returned unchanged.
122
Applies the CSS className `yui3-datatable-currency` to the cell.
125
@param col {Object} The column definition.
126
@return {Function} A formatter function that formats numerical data as currency.
129
currency: function (col) {
130
var className = cName('currency'),
131
format = col.currencyFormat || this.get('currencyFormat'),
132
fn = Y.Number.format;
133
return function (o) {
134
o.className = className;
135
var value = parseFloat(o.value);
136
if (!value && value !== 0) {
139
return fn(value, format);
145
Returns a date formatting function based on the given format.
148
@param format {String} The format spec definition.
149
@return {Function} A formatter function that formats numerical data as currency.
153
_date: function (format) {
154
var className = cName('date'),
156
format = {format: format};
157
return function (o) {
158
o.className = className;
159
return fn(o.value, format);
163
Returns a date formatting function.
164
It looks for the format to apply in the
165
[dateFormat](DataTable.Column.html#property_dateFormat)
166
property of the column or in the
167
[dateFormat](DataTable.html#attr_dateFormat)
168
attribute of the whole table.
170
{key: "DOB", formatter: "date", dateFormat: "%I:%M:%S %p"}
172
See [Date.format](Date.html#method_format) for the available format specs.
174
Anything that is not a date is returned unchanged.
176
Applies the CSS className `yui3-datatable-date` to the cell.
179
@param col {Object} The column definition.
180
@return {Function} A formatter function that formats dates.
183
'date' : function (col) {
184
return Formatters._date(col.dateFormat || this.get('dateFormat'));
187
Returns a date-only (no time part) formatting function using the current locale.
189
{key: "DOB", formatter: "localDate"}
191
Anything that is not a date is returned unchanged.
193
Applies the CSS className `yui3-datatable-date` to the cell.
195
@return {Function} A formatter function that formats dates.
198
localDate : function () {
199
return Formatters._date('%x');
202
Returns a time-only (no date part) formatting function using the current locale.
204
{key: "startTime", formatter: "localTime"}
206
Anything that is not a date is returned unchanged.
208
Applies the CSS className `yui3-datatable-date` to the cell.
210
@return {Function} A formatter function that formats dates.
213
localTime : function () {
214
return Formatters._date('%X');
217
Returns a date formatting function using the current locale.
219
{key: "DOB", formatter: "localDateTime"}
221
Anything that is not a date is returned unchanged.
223
Applies the CSS className `yui3-datatable-date` to the cell.
224
@method localDateTime
225
@return {Function} A formatter function that formats dates.
228
localDateTime : function () {
229
return Formatters._date('%c');
234
Returns a function that produces email links.
235
If the column definition contains a property
236
[linkFrom](DataTable.Column.html#property_linkFrom) it will use the value
237
in that field for the link, otherwise, the same column value will be used for both
240
{key: "contact", formatter: "email", linkFrom: "contactEmail"}
242
It will use the respective
243
[emptyCellValue](DataTable.Column.html#property_emptyCellValue)
244
column configuration attribute
245
for each of the value and the link if either is empty.
246
If the link value is still empty, it will return the value with no link.
248
Applies the CSS className `yui3-datatable-email` to the cell.
251
@param col {Object} The column definition.
252
@return {Function} A formatter function that adds a mailto: link to the value.
256
email: function (col) {
257
var className = cName('email'),
258
linkFrom = col.linkFrom,
259
emptyValue = col.emptyCellValue,
260
emptyLink = (this.getColumn(linkFrom) || {}).emptyCellValue;
261
col.allowHTML = true;
262
return function (o) {
263
var value = stringValue(o.value, emptyValue),
264
link = (linkFrom ? stringValue(o.data[linkFrom], emptyLink) : value);
265
o.className = className;
267
return '<a href="mailto:' + link + '">' + value + '</a>';
275
Returns a function that produces links.
276
If the column definition contains a property
277
[linkFrom](DataTable.Column.html#property_linkFrom) it will use the value
278
in that field for the link, otherwise, the same column value will be used for both
281
{key: "company", formatter: "link", linkFrom: "webSite"}
283
It will use the respective
284
[emptyCellValue](DataTable.Column.html#property_emptyCellValue)
285
column configuration attribute
286
for each of the value and the link if either is empty.
287
If the link value is still empty, it will return the value with no link.
289
Applies the CSS className `yui3-datatable-link` to the cell.
291
@param col {Object} The column definition.
292
@return {Function} A formatter function that adds a link to the value.
296
link: function (col) {
297
var className = cName('link'),
298
linkFrom = col.linkFrom,
299
emptyValue = col.emptyCellValue,
300
emptyLink = (this.getColumn(linkFrom) || {}).emptyCellValue;
301
col.allowHTML = true;
302
return function (o) {
303
var value = stringValue(o.value, emptyValue),
304
link = (linkFrom ? stringValue(o.data[linkFrom], emptyLink) : value);
305
o.className = className;
307
return '<a href="' + link + '">' + value + '</a>';
315
Returns a formatter function that formats values using
316
the [Number.format](Number.html#method_format) method.
317
It looks for the format to apply in the
318
[numberFormat](DataTable.Column.html#property_numberFormat)
319
property of the column or in the
320
[numberFormat](DataTable.html#attr_numberFormat)
321
attribute of the whole table.
323
{key: "weight", formatter: "number", numberFormat: {
325
decimalSeparator: ",",
326
thousandsSeparator: ",",
330
See [Number.format](Number.html#method_format) for the available format specs.
332
Anything that cannot be parsed as a number will be returned unchanged.
334
Applies the CSS className `yui3-datatable-number` to the cell.
337
@param col {Object} The column definition.
338
@return {Function} A formatter function that formats numerical data as currency.
341
number: function (col) {
342
var className = cName('number'),
343
format = col.numberFormat || this.get('numberFormat'),
344
fn = Y.Number.format;
345
return function (o) {
346
o.className = className;
347
var value = parseFloat(o.value);
348
if (!value && value !== 0) {
351
return fn(value, format);
355
Returns a formatter function that returns texts from a lookup table
356
based on the stored value.
358
It looks for the translation to apply in the
359
[lookupTable](DataTable.Column.html#property_lookupTable) property of the
360
column in either of these two formats:
362
{key: "status", formatter: "lookup", lookupTable: {
368
{key: "otherStatus", formatter: "lookup", lookupTable: [
369
{value:0, text: "unknown"},
370
{value:1, text: "requested"},
371
{value:2, text: "approved"},
372
{value:3, text: "delivered"}
375
Applies the CSS className `yui3-datatable-lookup` to the cell.
378
@param col {Object} The column definition
379
@return {Function} A formatter function that returns the `text`
380
associated with `value`.
383
lookup: function (col) {
384
var className = cName('lookup'),
385
lookup = col.lookupTable || {},
388
if (Lang.isArray(lookup)) {
392
for (i = 0, len = entries.length; i < len; ++i) {
393
lookup[entries[i].value] = entries[i].text;
396
return function (o) {
397
o.className = className;
398
return lookup[o.value];
403
Y.mix(Y.DataTable.BodyView.Formatters, Formatters);
405
Label to be shown in the face of a button produced by the
406
[button](DataTable.BodyView.Formatters.html#method_button) formatter
408
@property buttonLabel
410
@for DataTable.Column
413
Determines the texts to be shown to represent Boolean values when the
414
[boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter
417
The attribute is an object with text values for properties `true` and `false`.
419
{key:"active", formatter: "boolean", booleanLabels: {
424
@property booleanLabels
426
@for DataTable.Column
430
Determines the texts to be shown to represent Boolean values when the
431
[boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter
432
is used on any column.
434
It works like the column-specific
435
[booleanLabels](DataTable.Column.html#property_booleanLabels) but
436
for all columns using the
437
[boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter at once.
438
The values are often retrieved from a resource of localized texts.
440
@attribute booleanLabels
445
Format specification for columns using the
446
[currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
447
It contains an object as described in
448
[Number.format](Number.html#method_format).
450
@property currencyFormat
452
@for DataTable.Column
455
Format specification for columns using the
456
[currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
457
It contains an object as described in
458
[Number.format](Number.html#method_format).
461
[currencyFormat](DataTable.Column.html#property_currencyFormat)
462
but it applies to any column using the
463
[currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
464
The values are often retrieved from a resource of localized configuration.
466
@attribute currencyFormat
472
Format specification for columns using the
473
[date](DataTable.BodyView.Formatters.html#method_date) formatter.
474
It contains a string as described in
475
[Date.format](Date.html#method_format).
479
@for DataTable.Column
482
Format specification for columns using the
483
[date](DataTable.BodyView.Formatters.html#method_date) formatter.
484
It contains an object as described in
485
[Date.format](Date.html#method_format).
488
[dateFormat](DataTable.Column.html#property_dateFormat)
489
but it applies to any column using the
490
[date](DataTable.BodyView.Formatters.html#method_date) formatter.
491
The values are often retrieved from a resource of localized configuration.
493
@attribute dateFormat
498
Name of the field that is to provide the link for a column using the
499
[email](DataTable.BodyView.Formatters.html#method_email) or
500
[link](DataTable.BodyView.Formatters.html#method_link)
505
@for DataTable.Column
509
Format specification for columns using the
510
[number](DataTable.BodyView.Formatters.html#method_number) formatter.
511
It contains an object as described in
512
[Number.format](Number.html#method_format).
514
@property numberFormat
516
@for DataTable.Column
519
Format specification for columns using the
520
[number](DataTable.BodyView.Formatters.html#method_number) formatter.
521
It contains an object as described in
522
[Number.format](Number.html#method_format).
525
[numberFormat](DataTable.Column.html#property_numberFormat)
526
but it applies to any column using the
527
[number](DataTable.BodyView.Formatters.html#method_number) formatter.
528
The values are often retrieved from a resource of localized configuration.
530
@attribute numberFormat
535
Map of values to text used to translate internal values to human readable text
536
in columns using the [lookup](DataTable.BodyView.Formatters.html#method_lookup)
539
The map can be given in either of two formats:
541
{key: "status", formatter: "lookup", lookupTable: {
547
{key: "otherStatus", formatter: "lookup", lookupTable: [
548
{value:0, text: "unknown"},
549
{value:1, text: "requested"},
550
{value:2, text: "approved"},
551
{value:3, text: "delivered"}
554
The last format is compatible with the [dropdown](DataTable.Editors.html#property_dropdown)
555
and autocomplete-based editors, where the order of the items in the dropdown matters.
557
@property lookupTable
558
@type Object || Array
559
@for DataTable.Column
562
}, '@VERSION@', {"requires": ["datatable-body", "datatype-number-format", "datatype-date-format", "escape"]});