2
2
* Data grid is a simple widget designed to list the filtered records, providing
3
3
* a simple way to define how the items are displayed.
4
4
*
5
- * Note: Unlike other charts, the data grid chart (and data table) use the {@link dc.dataGrid#group group} attribute as a keying function
6
- * for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data together in groups.
7
- * Do not pass in a crossfilter group as this will not work.
5
+ *
6
+ * Note: Formerly the data grid chart (and data table) used the {@link dc.dataGrid#group group} attribute as a
7
+ * keying function for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data
8
+ * together in sections. This was confusing so it has been renamed to `section`, although `group` still works.
8
9
*
9
10
* Examples:
10
11
* - {@link http://europarl.me/dc.js/web/ep/index.html List of members of the european parliament}
21
22
dc . dataGrid = function ( parent , chartGroup ) {
22
23
var LABEL_CSS_CLASS = 'dc-grid-label' ;
23
24
var ITEM_CSS_CLASS = 'dc-grid-item' ;
24
- var GROUP_CSS_CLASS = 'dc-grid-group' ;
25
+ var SECTION_CSS_CLASS = 'dc-grid-section dc-grid-group' ;
25
26
var GRID_CSS_CLASS = 'dc-grid-top' ;
26
27
27
28
var _chart = dc . baseMixin ( { } ) ;
28
29
30
+ var _section = null ;
29
31
var _size = 999 ; // shouldn't be needed, but you might
30
32
var _html = function ( d ) { return 'you need to provide an html() handling param: ' + JSON . stringify ( d ) ; } ;
31
33
var _sortBy = function ( d ) {
@@ -34,54 +36,56 @@ dc.dataGrid = function (parent, chartGroup) {
34
36
var _order = d3 . ascending ;
35
37
var _beginSlice = 0 , _endSlice ;
36
38
37
- var _htmlGroup = function ( d ) {
38
- return '<div class=\'' + GROUP_CSS_CLASS + '\'><h1 class=\'' + LABEL_CSS_CLASS + '\'>' +
39
+ var _htmlSection = function ( d ) {
40
+ return '<div class=\'' + SECTION_CSS_CLASS + '\'><h1 class=\'' + LABEL_CSS_CLASS + '\'>' +
39
41
_chart . keyAccessor ( ) ( d ) + '</h1></div>' ;
40
42
} ;
41
43
44
+ _chart . _mandatoryAttributes ( [ 'dimension' , 'section' ] ) ;
45
+
42
46
_chart . _doRender = function ( ) {
43
47
_chart . selectAll ( 'div.' + GRID_CSS_CLASS ) . remove ( ) ;
44
48
45
- renderItems ( renderGroups ( ) ) ;
49
+ renderItems ( renderSections ( ) ) ;
46
50
47
51
return _chart ;
48
52
} ;
49
53
50
- function renderGroups ( ) {
51
- var groups = _chart . root ( ) . selectAll ( 'div.' + GRID_CSS_CLASS )
54
+ function renderSections ( ) {
55
+ var sections = _chart . root ( ) . selectAll ( 'div.' + GRID_CSS_CLASS )
52
56
. data ( nestEntries ( ) , function ( d ) {
53
57
return _chart . keyAccessor ( ) ( d ) ;
54
58
} ) ;
55
59
56
- var itemGroup = groups
60
+ var itemSection = sections
57
61
. enter ( )
58
62
. append ( 'div' )
59
63
. attr ( 'class' , GRID_CSS_CLASS ) ;
60
64
61
- if ( _htmlGroup ) {
62
- itemGroup
65
+ if ( _htmlSection ) {
66
+ itemSection
63
67
. html ( function ( d ) {
64
- return _htmlGroup ( d ) ;
68
+ return _htmlSection ( d ) ;
65
69
} ) ;
66
70
}
67
71
68
- groups . exit ( ) . remove ( ) ;
69
- return itemGroup ;
72
+ sections . exit ( ) . remove ( ) ;
73
+ return itemSection ;
70
74
}
71
75
72
76
function nestEntries ( ) {
73
77
var entries = _chart . dimension ( ) . top ( _size ) ;
74
78
75
79
return d3 . nest ( )
76
- . key ( _chart . group ( ) )
80
+ . key ( _chart . section ( ) )
77
81
. sortKeys ( _order )
78
82
. entries ( entries . sort ( function ( a , b ) {
79
83
return _order ( _sortBy ( a ) , _sortBy ( b ) ) ;
80
84
} ) . slice ( _beginSlice , _endSlice ) ) ;
81
85
}
82
86
83
- function renderItems ( groups ) {
84
- var items = groups . order ( )
87
+ function renderItems ( sections ) {
88
+ var items = sections . order ( )
85
89
. selectAll ( 'div.' + ITEM_CSS_CLASS )
86
90
. data ( function ( d ) {
87
91
return d . values ;
@@ -106,21 +110,40 @@ dc.dataGrid = function (parent, chartGroup) {
106
110
} ;
107
111
108
112
/**
109
- * Get or set the group function for the data grid. The group function takes a data row and
113
+ * Get or set the section function for the data grid. The section function takes a data row and
110
114
* returns the key to specify to {@link https://github.com/d3/d3-collection/blob/master/README.md#nest d3.nest}
111
- * to split rows into groups .
115
+ * to split rows into sections .
112
116
*
113
- * Do not pass in a crossfilter group as this will not work.
114
- * @method group
117
+ * Do not pass in a crossfilter section as this will not work.
118
+ * @method section
115
119
* @memberof dc.dataGrid
116
120
* @instance
117
121
* @example
118
- * // group rows by the value of their field
122
+ * // section rows by the value of their field
119
123
* chart
120
- * .group(function(d) { return d.field; })
124
+ * .section(function(d) { return d.field; })
125
+ * @param {Function } section Function taking a row of data and returning the nest key.
126
+ * @returns {Function|dc.dataGrid }
127
+ */
128
+ _chart . section = function ( section ) {
129
+ if ( ! arguments . length ) {
130
+ return _section ;
131
+ }
132
+ _section = section ;
133
+ return _chart ;
134
+ } ;
135
+
136
+ /**
137
+ * Backward-compatible synonym for {@link dc.dataGrid#section section}.
138
+ *
139
+ * @method group
140
+ * @memberof dc.dataGrid
141
+ * @instance
121
142
* @param {Function } groupFunction Function taking a row of data and returning the nest key.
122
- * @returns {Function|dc.dataTable }
143
+ * @returns {Function|dc.dataGrid }
123
144
*/
145
+ _chart . group = dc . logger . annotate ( _chart . section ,
146
+ 'consider using dataGrid.section instead of dataGrid.group for clarity' ) ;
124
147
125
148
/**
126
149
* Get or set the index of the beginning slice which determines which entries get displayed by the widget.
@@ -193,23 +216,34 @@ dc.dataGrid = function (parent, chartGroup) {
193
216
} ;
194
217
195
218
/**
196
- * Get or set the function that formats a group label.
197
- * @method htmlGroup
219
+ * Get or set the function that formats a section label.
220
+ * @method htmlSection
198
221
* @memberof dc.dataGrid
199
222
* @instance
200
223
* @example
201
- * chart.htmlGroup (function (d) { return '<h2>'.d.key . 'with ' . d.values.length .' items</h2>'});
202
- * @param {Function } [htmlGroup ]
224
+ * chart.htmlSection (function (d) { return '<h2>'.d.key . 'with ' . d.values.length .' items</h2>'});
225
+ * @param {Function } [htmlSection ]
203
226
* @returns {Function|dc.dataGrid }
204
227
*/
205
- _chart . htmlGroup = function ( htmlGroup ) {
228
+ _chart . htmlSection = function ( htmlSection ) {
206
229
if ( ! arguments . length ) {
207
- return _htmlGroup ;
230
+ return _htmlSection ;
208
231
}
209
- _htmlGroup = htmlGroup ;
232
+ _htmlSection = htmlSection ;
210
233
return _chart ;
211
234
} ;
212
235
236
+ /**
237
+ * Backward-compatible synonym for {@link dc.dataGrid#htmlSection htmlSection}.
238
+ * @method htmlGroup
239
+ * @memberof dc.dataGrid
240
+ * @instance
241
+ * @param {Function } [htmlGroup]
242
+ * @returns {Function|dc.dataGrid }
243
+ */
244
+ _chart . htmlGroup = dc . logger . annotate ( _chart . htmlSection ,
245
+ 'consider using dataGrid.htmlSection instead of dataGrid.htmlGroup for clarity' ) ;
246
+
213
247
/**
214
248
* Get or set sort-by function. This function works as a value accessor at the item
215
249
* level and returns a particular field to be sorted.
0 commit comments