2
2
* The data table is a simple widget designed to list crossfilter focused data set (rows being
3
3
* filtered) in a good old tabular fashion.
4
4
*
5
- * Note: Unlike other charts, the data table (and data grid chart) use the {@link dc.dataTable#group group} attribute as a
6
- * keying function for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data
7
- * together in groups. Do not pass in a crossfilter group as this will not work.
8
- *
9
- * Another interesting feature of the data table is that you can pass a crossfilter group to the `dimension`, as
5
+ * An interesting feature of the data table is that you can pass a crossfilter group to the `dimension`, as
10
6
* long as you specify the {@link dc.dataTable#order order} as `d3.descending`, since the data
11
7
* table will use `dimension.top()` to fetch the data in that case, and the method is equally
12
8
* supported on the crossfilter group as the crossfilter dimension.
13
9
*
10
+ * Note: Formerly the data table (and data grid chart) used the {@link dc.dataTable#group group} attribute as a
11
+ * keying function for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data
12
+ * together in sections. This was confusing so it has been renamed to `section`, although `group` still works.
13
+ *
14
14
* Examples:
15
15
* - {@link http://dc-js.github.com/dc.js/ Nasdaq 100 Index}
16
16
* - {@link http://dc-js.github.io/dc.js/examples/table-on-aggregated-data.html dataTable on a crossfilter group}
@@ -29,7 +29,7 @@ dc.dataTable = function (parent, chartGroup) {
29
29
var LABEL_CSS_CLASS = 'dc-table-label' ;
30
30
var ROW_CSS_CLASS = 'dc-table-row' ;
31
31
var COLUMN_CSS_CLASS = 'dc-table-column' ;
32
- var GROUP_CSS_CLASS = 'dc-table-group' ;
32
+ var SECTION_CSS_CLASS = 'dc-table-section dc-table-group' ;
33
33
var HEAD_CSS_CLASS = 'dc-table-head' ;
34
34
35
35
var _chart = dc . baseMixin ( { } ) ;
@@ -42,12 +42,15 @@ dc.dataTable = function (parent, chartGroup) {
42
42
var _order = d3 . ascending ;
43
43
var _beginSlice = 0 ;
44
44
var _endSlice ;
45
- var _showGroups = true ;
45
+ var _showSections = true ;
46
+ var _section = function ( ) { return '' ; } ; // all in one section
47
+
48
+ _chart . _mandatoryAttributes ( [ 'dimension' ] ) ;
46
49
47
50
_chart . _doRender = function ( ) {
48
51
_chart . selectAll ( 'tbody' ) . remove ( ) ;
49
52
50
- renderRows ( renderGroups ( ) ) ;
53
+ renderRows ( renderSections ( ) ) ;
51
54
52
55
return _chart ;
53
56
} ;
@@ -94,7 +97,7 @@ dc.dataTable = function (parent, chartGroup) {
94
97
return s ;
95
98
} ;
96
99
97
- function renderGroups ( ) {
100
+ function renderSections ( ) {
98
101
// The 'original' example uses all 'functions'.
99
102
// If all 'functions' are used, then don't remove/add a header, and leave
100
103
// the html alone. This preserves the functionality of earlier releases.
@@ -134,19 +137,19 @@ dc.dataTable = function (parent, chartGroup) {
134
137
} ) ;
135
138
}
136
139
137
- var groups = _chart . root ( ) . selectAll ( 'tbody' )
140
+ var sections = _chart . root ( ) . selectAll ( 'tbody' )
138
141
. data ( nestEntries ( ) , function ( d ) {
139
142
return _chart . keyAccessor ( ) ( d ) ;
140
143
} ) ;
141
144
142
- var rowGroup = groups
145
+ var rowSection = sections
143
146
. enter ( )
144
147
. append ( 'tbody' ) ;
145
148
146
- if ( _showGroups === true ) {
147
- rowGroup
149
+ if ( _showSections === true ) {
150
+ rowSection
148
151
. append ( 'tr' )
149
- . attr ( 'class' , GROUP_CSS_CLASS )
152
+ . attr ( 'class' , SECTION_CSS_CLASS )
150
153
. append ( 'td' )
151
154
. attr ( 'class' , LABEL_CSS_CLASS )
152
155
. attr ( 'colspan' , _columns . length )
@@ -155,9 +158,9 @@ dc.dataTable = function (parent, chartGroup) {
155
158
} ) ;
156
159
}
157
160
158
- groups . exit ( ) . remove ( ) ;
161
+ sections . exit ( ) . remove ( ) ;
159
162
160
- return rowGroup ;
163
+ return rowSection ;
161
164
}
162
165
163
166
function nestEntries ( ) {
@@ -169,15 +172,15 @@ dc.dataTable = function (parent, chartGroup) {
169
172
}
170
173
171
174
return d3 . nest ( )
172
- . key ( _chart . group ( ) )
175
+ . key ( _chart . section ( ) )
173
176
. sortKeys ( _order )
174
177
. entries ( entries . sort ( function ( a , b ) {
175
178
return _order ( _sortBy ( a ) , _sortBy ( b ) ) ;
176
179
} ) . slice ( _beginSlice , _endSlice ) ) ;
177
180
}
178
181
179
- function renderRows ( groups ) {
180
- var rows = groups . order ( )
182
+ function renderRows ( sections ) {
183
+ var rows = sections . order ( )
181
184
. selectAll ( 'tr.' + ROW_CSS_CLASS )
182
185
. data ( function ( d ) {
183
186
return d . values ;
@@ -205,21 +208,41 @@ dc.dataTable = function (parent, chartGroup) {
205
208
} ;
206
209
207
210
/**
208
- * Get or set the group function for the data table. The group function takes a data row and
211
+ * Get or set the section function for the data table. The section function takes a data row and
209
212
* returns the key to specify to {@link https://github.com/d3/d3-collection/blob/master/README.md#nest d3.nest}
210
- * to split rows into groups .
213
+ * to split rows into sections. By default there will be only one section with no name .
211
214
*
212
- * Do not pass in a crossfilter group as this will not work.
213
- * @method group
215
+ * Set {@link dc.dataTable#showSections showSections} to false to hide the section headers
216
+ *
217
+ * @method section
214
218
* @memberof dc.dataTable
215
219
* @instance
216
220
* @example
217
- * // group rows by the value of their field
221
+ * // section rows by the value of their field
218
222
* chart
219
- * .group(function(d) { return d.field; })
223
+ * .section(function(d) { return d.field; })
224
+ * @param {Function } sectionFunction Function taking a row of data and returning the nest key.
225
+ * @returns {Function|dc.dataTable }
226
+ */
227
+ _chart . section = function ( section ) {
228
+ if ( ! arguments . length ) {
229
+ return _section ;
230
+ }
231
+ _section = section ;
232
+ return _chart ;
233
+ } ;
234
+
235
+ /**
236
+ * Backward-compatible synonym for {@link dc.dataTable#section section}.
237
+ *
238
+ * @method group
239
+ * @memberof dc.dataTable
240
+ * @instance
220
241
* @param {Function } groupFunction Function taking a row of data and returning the nest key.
221
242
* @returns {Function|dc.dataTable }
222
243
*/
244
+ _chart . group = dc . logger . annotate ( _chart . section ,
245
+ 'consider using dataTable.section instead of dataTable.group for clarity' ) ;
223
246
224
247
/**
225
248
* Get or set the table size which determines the number of rows displayed by the widget.
@@ -406,25 +429,33 @@ dc.dataTable = function (parent, chartGroup) {
406
429
} ;
407
430
408
431
/**
409
- * Get or set if group rows will be shown. The dataTable {@link dc.dataTable#group group}
410
- * function must be specified even if groups are not shown.
411
- * @method showGroups
432
+ * Get or set if section header rows will be shown.
433
+ * @method showSections
412
434
* @memberof dc.dataTable
413
435
* @instance
414
436
* @example
415
437
* chart
416
- * .group ([value], [name])
417
- * .showGroups (true|false);
418
- * @param {Boolean } [showGroups =true]
438
+ * .section ([value], [name])
439
+ * .showSections (true|false);
440
+ * @param {Boolean } [showSections =true]
419
441
* @returns {Boolean|dc.dataTable }
420
442
*/
421
- _chart . showGroups = function ( showGroups ) {
443
+ _chart . showSections = _chart . showGroups = function ( showSections ) {
422
444
if ( ! arguments . length ) {
423
- return _showGroups ;
445
+ return _showSections ;
424
446
}
425
- _showGroups = showGroups ;
447
+ _showSections = showSections ;
426
448
return _chart ;
427
449
} ;
428
450
451
+ /**
452
+ * Backward-compatible synonym for {@link dc.dataTable#showSections showSections}.
453
+ * @method showGroups
454
+ * @memberof dc.dataTable
455
+ * @instance
456
+ * @param {Boolean } [showGroups=true]
457
+ * @returns {Boolean|dc.dataTable }
458
+ */
459
+
429
460
return _chart . anchor ( parent , chartGroup ) ;
430
461
} ;
0 commit comments