Skip to content

Commit 3f4ecf8

Browse files
format columns
1 parent 3715b1d commit 3f4ecf8

File tree

2 files changed

+218
-8
lines changed

2 files changed

+218
-8
lines changed

classes/Visualizer/Render/Sidebar/Type/DataTable.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function hooks() {
7575
* @access public
7676
*/
7777
function load_assets( $deps, $is_frontend ) {
78-
wp_register_script( 'visualizer-datatables', self::$_js, array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
78+
wp_register_script( 'visualizer-datatables', self::$_js, array( 'jquery-ui-core', 'moment' ), Visualizer_Plugin::VERSION );
7979
wp_enqueue_style( 'visualizer-datatables', self::$_css, array(), Visualizer_Plugin::VERSION );
8080

8181
wp_register_script(
@@ -115,7 +115,7 @@ protected function _toHTML() {
115115
$this->_supportsAnimation = false;
116116
$this->_renderGeneralSettings();
117117
$this->_renderTableSettings();
118-
// $this->_renderColumnSettings();
118+
$this->_renderColumnSettings();
119119
$this->_renderAdvancedSettings();
120120
}
121121

@@ -370,4 +370,72 @@ protected function _renderColumnSettings() {
370370
self::_renderGroupEnd();
371371
}
372372

373+
/**
374+
* Renders format field according to series type.
375+
*
376+
* @since 1.3.0
377+
*
378+
* @access protected
379+
* @param int $index The index of the series.
380+
*/
381+
protected function _renderFormatField( $index = 0 ) {
382+
switch ( $this->__series[ $index ]['type'] ) {
383+
case 'number':
384+
self::_renderTextItem(
385+
esc_html__( 'Thousands Separator', 'visualizer' ),
386+
'series[' . $index . '][format][thousands]',
387+
isset( $this->series[ $index ]['format']['thousands'] ) ? $this->series[ $index ]['format']['thousands'] : ',',
388+
null,
389+
','
390+
);
391+
self::_renderTextItem(
392+
esc_html__( 'Decimal Separator', 'visualizer' ),
393+
'series[' . $index . '][format][decimal]',
394+
isset( $this->series[ $index ]['format']['decimal'] ) ? $this->series[ $index ]['format']['decimal'] : '.',
395+
null,
396+
'.'
397+
);
398+
self::_renderTextItem(
399+
esc_html__( 'Precision', 'visualizer' ),
400+
'series[' . $index . '][format][precision]',
401+
isset( $this->series[ $index ]['format']['precision'] ) ? $this->series[ $index ]['format']['precision'] : '',
402+
esc_html__( 'Round values to how many decimal places?', 'visualizer' ),
403+
''
404+
);
405+
self::_renderTextItem(
406+
esc_html__( 'Prefix', 'visualizer' ),
407+
'series[' . $index . '][format][prefix]',
408+
isset( $this->series[ $index ]['format']['prefix'] ) ? $this->series[ $index ]['format']['prefix'] : '',
409+
null,
410+
''
411+
);
412+
self::_renderTextItem(
413+
esc_html__( 'Suffix', 'visualizer' ),
414+
'series[' . $index . '][format][suffix]',
415+
isset( $this->series[ $index ]['format']['suffix'] ) ? $this->series[ $index ]['format']['suffix'] : '',
416+
null,
417+
''
418+
);
419+
break;
420+
case 'date':
421+
case 'datetime':
422+
case 'timeofday':
423+
self::_renderTextItem(
424+
esc_html__( 'Display Date Format', 'visualizer' ),
425+
'series[' . $index . '][format][to]',
426+
isset( $this->series[ $index ]['format']['to'] ) ? $this->series[ $index ]['format']['to'] : '',
427+
sprintf( esc_html__( 'Enter custom format pattern to apply to this series value, similar to the %1$sdate and time formats here%2$s.', 'visualizer' ), '<a href="https://momentjs.com/docs/#/displaying/" target="_blank">', '</a>' ),
428+
'Do MMM YYYY'
429+
);
430+
self::_renderTextItem(
431+
esc_html__( 'Source Date Format', 'visualizer' ),
432+
'series[' . $index . '][format][from]',
433+
isset( $this->series[ $index ]['format']['from'] ) ? $this->series[ $index ]['format']['from'] : '',
434+
sprintf( esc_html__( 'What format is the source date in? Similar to the %1$sdate and time formats here%2$s.', 'visualizer' ), '<a href="https://momentjs.com/docs/#/displaying/" target="_blank">', '</a>' ),
435+
'YYYY-MM-DD'
436+
);
437+
break;
438+
}
439+
}
440+
373441
}

js/render-datatables.js

Lines changed: 148 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@
120120
type = 'date';
121121
break;
122122
}
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 };
124127
if(typeof chart.settings['cssClassNames'] !== 'undefined' && typeof chart.settings['cssClassNames']['tableCell'] !== 'undefined'){
125128
col['className'] = chart.settings['cssClassNames']['tableCell'];
126129
}
@@ -132,9 +135,6 @@
132135
row = [];
133136
for (j = 0; j < series.length; j++) {
134137
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-
}
138138
row[ series[j].label ] = datum;
139139
}
140140
rows.push(row);
@@ -152,16 +152,41 @@
152152
$('.loader').remove();
153153
}
154154

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+
156161
switch(type){
157162
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]);
158180
break;
159181
case 'date':
160182
case 'datetime':
161183
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+
}
162187
break;
163188
}
164-
return datum;
189+
return render;
165190
}
166191

167192
function render(v) {
@@ -189,3 +214,120 @@
189214

190215
})(jQuery);
191216

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

Comments
 (0)