120
120
type = 'date' ;
121
121
break ;
122
122
}
123
- var col = { title : series [ j ] . label , data : series [ j ] . label , type : type } ;
123
+
124
+ render = addRenderer ( series [ j ] . type , settings . series [ j ] ) ;
125
+
126
+ var col = { title : series [ j ] . label , data : series [ j ] . label , type : type , render : render } ;
124
127
if ( typeof chart . settings [ 'cssClassNames' ] !== 'undefined' && typeof chart . settings [ 'cssClassNames' ] [ 'tableCell' ] !== 'undefined' ) {
125
128
col [ 'className' ] = chart . settings [ 'cssClassNames' ] [ 'tableCell' ] ;
126
129
}
132
135
row = [ ] ;
133
136
for ( j = 0 ; j < series . length ; j ++ ) {
134
137
var datum = data [ i ] [ j ] ;
135
- if ( typeof settings . series [ j ] !== 'undefined' && typeof settings . series [ j ] . format !== 'undefined' ) {
136
- datum = formatData ( data [ i ] [ j ] , settings . series [ j ] . type , settings . series [ j ] . format ) ;
137
- }
138
138
row [ series [ j ] . label ] = datum ;
139
139
}
140
140
rows . push ( row ) ;
152
152
$ ( '.loader' ) . remove ( ) ;
153
153
}
154
154
155
- function formatData ( datum , type , format ) {
155
+ function addRenderer ( type , series ) {
156
+ var render = null ;
157
+ if ( typeof series === 'undefined' || typeof series . format === 'undefined' ) {
158
+ return render ;
159
+ }
160
+
156
161
switch ( type ) {
157
162
case 'number' :
163
+ var parts = [ '' , '' , '' , '' , '' ] ;
164
+ if ( typeof series . format . thousands !== '' ) {
165
+ parts [ 0 ] = series . format . thousands ;
166
+ }
167
+ if ( typeof series . format . decimal !== '' ) {
168
+ parts [ 1 ] = series . format . decimal ;
169
+ }
170
+ if ( typeof series . format . precision !== '' ) {
171
+ parts [ 2 ] = series . format . precision ;
172
+ }
173
+ if ( typeof series . format . prefix !== '' ) {
174
+ parts [ 3 ] = series . format . prefix ;
175
+ }
176
+ if ( typeof series . format . suffix !== '' ) {
177
+ parts [ 4 ] = series . format . suffix ;
178
+ }
179
+ render = $ . fn . dataTable . render . number ( parts [ 0 ] , parts [ 1 ] , parts [ 2 ] , parts [ 3 ] , parts [ 4 ] ) ;
158
180
break ;
159
181
case 'date' :
160
182
case 'datetime' :
161
183
case 'timeofday' :
184
+ if ( typeof series . format . to !== 'undefined' && typeof series . format . from !== 'undefined' && series . format . from != '' && series . format . to !== '' ) {
185
+ render = $ . fn . dataTable . render . moment ( series . format . from , series . format . to ) ;
186
+ }
162
187
break ;
163
188
}
164
- return datum ;
189
+ return render ;
165
190
}
166
191
167
192
function render ( v ) {
189
214
190
215
} ) ( jQuery ) ;
191
216
217
+
218
+ /**
219
+ * Date / time formats often from back from server APIs in a format that you
220
+ * don't wish to display to your end users (ISO8601 for example). This rendering
221
+ * helper can be used to transform any source date / time format into something
222
+ * which can be easily understood by your users when reading the table, and also
223
+ * by DataTables for sorting the table.
224
+ *
225
+ * The [MomentJS library](http://momentjs.com/) is used to accomplish this and
226
+ * you simply need to tell it which format to transfer from, to and specify a
227
+ * locale if required.
228
+ *
229
+ * This function should be used with the `dt-init columns.render` configuration
230
+ * option of DataTables.
231
+ *
232
+ * It accepts one, two or three parameters:
233
+ *
234
+ * $.fn.dataTable.render.moment( to );
235
+ * $.fn.dataTable.render.moment( from, to );
236
+ * $.fn.dataTable.render.moment( from, to, locale );
237
+ *
238
+ * Where:
239
+ *
240
+ * * `to` - the format that will be displayed to the end user
241
+ * * `from` - the format that is supplied in the data (the default is ISO8601 -
242
+ * `YYYY-MM-DD`)
243
+ * * `locale` - the locale which MomentJS should use - the default is `en`
244
+ * (English).
245
+ *
246
+ * @name datetime
247
+ * @summary Convert date / time source data into one suitable for display
248
+ * @author [Allan Jardine](http://datatables.net)
249
+ * @requires DataTables 1.10+
250
+ *
251
+ * @example
252
+ * // Convert ISO8601 dates into a simple human readable format
253
+ * $('#example').DataTable( {
254
+ * columnDefs: [ {
255
+ * targets: 1,
256
+ * render: $.fn.dataTable.render.moment( 'Do MMM YYYYY' )
257
+ * } ]
258
+ * } );
259
+ *
260
+ * @example
261
+ * // Specify a source format - in this case a unix timestamp
262
+ * $('#example').DataTable( {
263
+ * columnDefs: [ {
264
+ * targets: 2,
265
+ * render: $.fn.dataTable.render.moment( 'X', 'Do MMM YY' )
266
+ * } ]
267
+ * } );
268
+ *
269
+ * @example
270
+ * // Specify a source format and locale
271
+ * $('#example').DataTable( {
272
+ * columnDefs: [ {
273
+ * targets: 2,
274
+ * render: $.fn.dataTable.render.moment( 'YYYY/MM/DD', 'Do MMM YY', 'fr' )
275
+ * } ]
276
+ * } );
277
+ */
278
+ ( function ( factory ) {
279
+ "use strict" ;
280
+
281
+ if ( typeof define === 'function' && define . amd ) {
282
+ // AMD
283
+ define ( [ 'jquery' ] , function ( $ ) {
284
+ return factory ( $ , window , document ) ;
285
+ } ) ;
286
+ }
287
+ else if ( typeof exports === 'object' ) {
288
+ // CommonJS
289
+ module . exports = function ( root , $ ) {
290
+ if ( ! root ) {
291
+ root = window ;
292
+ }
293
+
294
+ if ( ! $ ) {
295
+ $ = typeof window !== 'undefined' ?
296
+ require ( 'jquery' ) :
297
+ require ( 'jquery' ) ( root ) ;
298
+ }
299
+
300
+ return factory ( $ , root , root . document ) ;
301
+ } ;
302
+ }
303
+ else {
304
+ // Browser
305
+ factory ( jQuery , window , document ) ;
306
+ }
307
+ }
308
+ ( function ( $ , window , document ) {
309
+
310
+
311
+ $ . fn . dataTable . render . moment = function ( from , to , locale ) {
312
+ // Argument shifting
313
+ if ( arguments . length === 1 ) {
314
+ locale = 'en' ;
315
+ to = from ;
316
+ from = 'YYYY-MM-DD' ;
317
+ }
318
+ else if ( arguments . length === 2 ) {
319
+ locale = 'en' ;
320
+ }
321
+
322
+ return function ( d , type , row ) {
323
+ if ( ! d ) return null ;
324
+ var m = window . moment ( d , from , locale , true ) ;
325
+
326
+ // Order and type get a number value from Moment, everything else
327
+ // sees the rendered value
328
+ return m . format ( type === 'sort' || type === 'type' ? 'x' : to ) ;
329
+ } ;
330
+ } ;
331
+
332
+
333
+ } ) ) ;
0 commit comments