2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('datatable-formatters', function (Y, NAME) {
11 Adds predefined cell formatters to `Y.DataTable.BodyView`.
13 @module datatable-formatters
16 isValue = Lang.isValue,
17 escape = Y.Escape.html,
19 getCName = Y.ClassNameManager.getClassName,
20 cName = function (name) {
21 return getCName('datatable', name);
23 stringValue = function (value, def) {
24 return (isValue(value) ? escape(value.toString()) : def || '');
27 Registry of function producing cell formatting functions.
28 Allows for names to be used in the column
29 definition `formatter` property:
31 {key:"myColumn", formatter:"date"}
33 These functions are not meant to be used directly. Instead, they will be
34 automatically called when their names are used as values for the `formatter`
35 property in a columnd definition.
36 They will be called just once per rendering cycle and will receive
37 the column configuration. They are expected to return a function that will
38 then be called once per row and will do the actual formatting.
39 They are expected to do all the preparatory once-per-render work
40 so that the actual formatting function doesn't need to repeat it.
42 @class DataTable.BodyView.Formatters
47 Returns a formatter that produces a BUTTON element using the value of
48 the [buttonLabel](DataTable.Column.html#property_buttonLabel)
49 column definition attribute as its label or the text
52 Applies the CSS className `yui3-datatable-button` to the cell.
55 @param col {Object} The column definition.
56 @return {Function} A formatter function that produces a `<button>` element.
59 button: function (col) {
60 var className = cName('button'),
61 html = '<button>' + (col.buttonLabel || 'Click') + '</button>';
64 o.className = className;
71 Returns a formatter function that returns the texts `"true"` or `"false"`
72 and assigns the CSS classNames `yui3-datatable-true` or `yui3-datatable-false`
73 based on the value of the cell.
75 If either a [booleanLabels](DataTable.Column.html#property_booleanLabels)
76 configuration object is defined for the column
77 or a [booleanLabels](DataTable.html#attr_booleanLabels)
78 configuration attribute is defined for the datatable,
79 the formatter will use the values for the properties `true` or `false`
80 of either of those objects as the text to show.
82 It returns `null`s or `undefined`s unchanged so that the `emptyCellValue`
83 configuration attribute will eventually apply.
85 {key:"active", formatter: "boolean", booleanLabels: {
92 @param col {Object} The column definition.
93 @return {Function} A formatter function that formats boolean data.
96 'boolean': function (col) {
97 var labels = col.booleanLabels || this.get('booleanLabels') || {'true':'true', 'false':'false'};
100 if (!value && value !== false) {
103 value = value?'true':'false';
104 o.className = cName(value);
105 return labels[value];
110 Returns a formatter function that formats values as currency using
111 the [Number.format](Number.html#method_format) method.
112 It looks for the format to apply in the
113 [currencyFormat](DataTable.Column.html#property_currencyFormat) property
114 of the column or in the
115 [currencyFormat](DataTable.html#attr_currencyFormat)
116 attribute of the whole table.
118 {key: "amount", formatter: "currency", currencyFormat: {
120 decimalSeparator: ",",
121 thousandsSeparator: ".",
125 See [Number.format](Number.html#method_format) for the available format specs.
127 Anything that cannot be parsed as a number will be returned unchanged.
129 Applies the CSS className `yui3-datatable-currency` to the cell.
132 @param col {Object} The column definition.
133 @return {Function} A formatter function that formats numerical data as currency.
136 currency: function (col) {
137 var className = cName('currency'),
138 format = col.currencyFormat || this.get('currencyFormat'),
139 fn = Y.Number.format;
140 return function (o) {
141 o.className = className;
142 var value = parseFloat(o.value);
143 if (!value && value !== 0) {
146 return fn(value, format);
152 Returns a date formatting function based on the given format.
155 @param format {String} The format spec definition.
156 @return {Function} A formatter function that formats numerical data as currency.
160 _date: function (format) {
161 var className = cName('date'),
163 format = {format: format};
164 return function (o) {
165 o.className = className;
166 return fn(o.value, format);
170 Returns a date formatting function.
171 It looks for the format to apply in the
172 [dateFormat](DataTable.Column.html#property_dateFormat)
173 property of the column or in the
174 [dateFormat](DataTable.html#attr_dateFormat)
175 attribute of the whole table.
177 {key: "DOB", formatter: "date", dateFormat: "%I:%M:%S %p"}
179 See [Date.format](Date.html#method_format) for the available format specs.
181 Anything that is not a date is returned unchanged.
183 Applies the CSS className `yui3-datatable-date` to the cell.
186 @param col {Object} The column definition.
187 @return {Function} A formatter function that formats dates.
190 'date' : function (col) {
191 return Formatters._date(col.dateFormat || this.get('dateFormat'));
194 Returns a date-only (no time part) formatting function using the current locale.
196 {key: "DOB", formatter: "localDate"}
198 Anything that is not a date is returned unchanged.
200 Applies the CSS className `yui3-datatable-date` to the cell.
202 @return {Function} A formatter function that formats dates.
205 localDate : function () {
206 return Formatters._date('%x');
209 Returns a time-only (no date part) formatting function using the current locale.
211 {key: "startTime", formatter: "localTime"}
213 Anything that is not a date is returned unchanged.
215 Applies the CSS className `yui3-datatable-date` to the cell.
217 @return {Function} A formatter function that formats dates.
220 localTime : function () {
221 return Formatters._date('%X');
224 Returns a date formatting function using the current locale.
226 {key: "DOB", formatter: "localDateTime"}
228 Anything that is not a date is returned unchanged.
230 Applies the CSS className `yui3-datatable-date` to the cell.
231 @method localDateTime
232 @return {Function} A formatter function that formats dates.
235 localDateTime : function () {
236 return Formatters._date('%c');
241 Returns a function that produces email links.
242 If the column definition contains a property
243 [linkFrom](DataTable.Column.html#property_linkFrom) it will use the value
244 in that field for the link, otherwise, the same column value will be used for both
247 {key: "contact", formatter: "email", linkFrom: "contactEmail"}
249 It will use the respective
250 [emptyCellValue](DataTable.Column.html#property_emptyCellValue)
251 column configuration attribute
252 for each of the value and the link if either is empty.
253 If the link value is still empty, it will return the value with no link.
255 Applies the CSS className `yui3-datatable-email` to the cell.
258 @param col {Object} The column definition.
259 @return {Function} A formatter function that adds a mailto: link to the value.
263 email: function (col) {
264 var className = cName('email'),
265 linkFrom = col.linkFrom,
266 emptyValue = col.emptyCellValue,
267 emptyLink = (this.getColumn(linkFrom) || {}).emptyCellValue;
268 col.allowHTML = true;
269 return function (o) {
270 var value = stringValue(o.value, emptyValue),
271 link = (linkFrom ? stringValue(o.data[linkFrom], emptyLink) : value);
272 o.className = className;
274 return '<a href="mailto:' + link + '">' + value + '</a>';
282 Returns a function that produces links.
283 If the column definition contains a property
284 [linkFrom](DataTable.Column.html#property_linkFrom) it will use the value
285 in that field for the link, otherwise, the same column value will be used for both
288 {key: "company", formatter: "link", linkFrom: "webSite"}
290 It will use the respective
291 [emptyCellValue](DataTable.Column.html#property_emptyCellValue)
292 column configuration attribute
293 for each of the value and the link if either is empty.
294 If the link value is still empty, it will return the value with no link.
296 Applies the CSS className `yui3-datatable-link` to the cell.
298 @param col {Object} The column definition.
299 @return {Function} A formatter function that adds a link to the value.
303 link: function (col) {
304 var className = cName('link'),
305 linkFrom = col.linkFrom,
306 emptyValue = col.emptyCellValue,
307 emptyLink = (this.getColumn(linkFrom) || {}).emptyCellValue;
308 col.allowHTML = true;
309 return function (o) {
310 var value = stringValue(o.value, emptyValue),
311 link = (linkFrom ? stringValue(o.data[linkFrom], emptyLink) : value);
312 o.className = className;
314 return '<a href="' + link + '">' + value + '</a>';
322 Returns a formatter function that formats values using
323 the [Number.format](Number.html#method_format) method.
324 It looks for the format to apply in the
325 [numberFormat](DataTable.Column.html#property_numberFormat)
326 property of the column or in the
327 [numberFormat](DataTable.html#attr_numberFormat)
328 attribute of the whole table.
330 {key: "weight", formatter: "number", numberFormat: {
332 decimalSeparator: ",",
333 thousandsSeparator: ",",
337 See [Number.format](Number.html#method_format) for the available format specs.
339 Anything that cannot be parsed as a number will be returned unchanged.
341 Applies the CSS className `yui3-datatable-number` to the cell.
344 @param col {Object} The column definition.
345 @return {Function} A formatter function that formats numerical data as currency.
348 number: function (col) {
349 var className = cName('number'),
350 format = col.numberFormat || this.get('numberFormat'),
351 fn = Y.Number.format;
352 return function (o) {
353 o.className = className;
354 var value = parseFloat(o.value);
355 if (!value && value !== 0) {
358 return fn(value, format);
362 Returns a formatter function that returns texts from a lookup table
363 based on the stored value.
365 It looks for the translation to apply in the
366 [lookupTable](DataTable.Column.html#property_lookupTable) property of the
367 column in either of these two formats:
369 {key: "status", formatter: "lookup", lookupTable: {
375 {key: "otherStatus", formatter: "lookup", lookupTable: [
376 {value:0, text: "unknown"},
377 {value:1, text: "requested"},
378 {value:2, text: "approved"},
379 {value:3, text: "delivered"}
382 Applies the CSS className `yui3-datatable-lookup` to the cell.
385 @param col {Object} The column definition
386 @return {Function} A formatter function that returns the `text`
387 associated with `value`.
390 lookup: function (col) {
391 var className = cName('lookup'),
392 lookup = col.lookupTable || {},
395 if (Lang.isArray(lookup)) {
399 for (i = 0, len = entries.length; i < len; ++i) {
400 lookup[entries[i].value] = entries[i].text;
403 return function (o) {
404 o.className = className;
405 return lookup[o.value];
410 Y.mix(Y.DataTable.BodyView.Formatters, Formatters);
412 Label to be shown in the face of a button produced by the
413 [button](DataTable.BodyView.Formatters.html#method_button) formatter
415 @property buttonLabel
417 @for DataTable.Column
420 Determines the texts to be shown to represent Boolean values when the
421 [boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter
424 The attribute is an object with text values for properties `true` and `false`.
426 {key:"active", formatter: "boolean", booleanLabels: {
431 @property booleanLabels
433 @for DataTable.Column
437 Determines the texts to be shown to represent Boolean values when the
438 [boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter
439 is used on any column.
441 It works like the column-specific
442 [booleanLabels](DataTable.Column.html#property_booleanLabels) but
443 for all columns using the
444 [boolean](DataTable.BodyView.Formatters.html#method_boolean) formatter at once.
445 The values are often retrieved from a resource of localized texts.
447 @attribute booleanLabels
452 Format specification for columns using the
453 [currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
454 It contains an object as described in
455 [Number.format](Number.html#method_format).
457 @property currencyFormat
459 @for DataTable.Column
462 Format specification for columns using the
463 [currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
464 It contains an object as described in
465 [Number.format](Number.html#method_format).
468 [currencyFormat](DataTable.Column.html#property_currencyFormat)
469 but it applies to any column using the
470 [currency](DataTable.BodyView.Formatters.html#method_currency) formatter.
471 The values are often retrieved from a resource of localized configuration.
473 @attribute currencyFormat
479 Format specification for columns using the
480 [date](DataTable.BodyView.Formatters.html#method_date) formatter.
481 It contains a string as described in
482 [Date.format](Date.html#method_format).
486 @for DataTable.Column
489 Format specification for columns using the
490 [date](DataTable.BodyView.Formatters.html#method_date) formatter.
491 It contains an object as described in
492 [Date.format](Date.html#method_format).
495 [dateFormat](DataTable.Column.html#property_dateFormat)
496 but it applies to any column using the
497 [date](DataTable.BodyView.Formatters.html#method_date) formatter.
498 The values are often retrieved from a resource of localized configuration.
500 @attribute dateFormat
505 Name of the field that is to provide the link for a column using the
506 [email](DataTable.BodyView.Formatters.html#method_email) or
507 [link](DataTable.BodyView.Formatters.html#method_link)
512 @for DataTable.Column
516 Format specification for columns using the
517 [number](DataTable.BodyView.Formatters.html#method_number) formatter.
518 It contains an object as described in
519 [Number.format](Number.html#method_format).
521 @property numberFormat
523 @for DataTable.Column
526 Format specification for columns using the
527 [number](DataTable.BodyView.Formatters.html#method_number) formatter.
528 It contains an object as described in
529 [Number.format](Number.html#method_format).
532 [numberFormat](DataTable.Column.html#property_numberFormat)
533 but it applies to any column using the
534 [number](DataTable.BodyView.Formatters.html#method_number) formatter.
535 The values are often retrieved from a resource of localized configuration.
537 @attribute numberFormat
542 Map of values to text used to translate internal values to human readable text
543 in columns using the [lookup](DataTable.BodyView.Formatters.html#method_lookup)
546 The map can be given in either of two formats:
548 {key: "status", formatter: "lookup", lookupTable: {
554 {key: "otherStatus", formatter: "lookup", lookupTable: [
555 {value:0, text: "unknown"},
556 {value:1, text: "requested"},
557 {value:2, text: "approved"},
558 {value:3, text: "delivered"}
561 The last format is compatible with the [dropdown](DataTable.Editors.html#property_dropdown)
562 and autocomplete-based editors, where the order of the items in the dropdown matters.
564 @property lookupTable
565 @type Object || Array
566 @for DataTable.Column
569 }, '3.13.0', {"requires": ["datatable-body", "datatype-number-format", "datatype-date-format", "escape"]});