Skip to content

Commit 6ff0d0e

Browse files
introduce dataGrid.section to replace dataGrid.group
similar to #855 it's still required and has no default new htmlGroup also suggests htmlSection
1 parent da67017 commit 6ff0d0e

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

src/data-grid.js

+66-32
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* Data grid is a simple widget designed to list the filtered records, providing
33
* a simple way to define how the items are displayed.
44
*
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.
89
*
910
* Examples:
1011
* - {@link http://europarl.me/dc.js/web/ep/index.html List of members of the european parliament}
@@ -21,11 +22,12 @@
2122
dc.dataGrid = function (parent, chartGroup) {
2223
var LABEL_CSS_CLASS = 'dc-grid-label';
2324
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';
2526
var GRID_CSS_CLASS = 'dc-grid-top';
2627

2728
var _chart = dc.baseMixin({});
2829

30+
var _section = null;
2931
var _size = 999; // shouldn't be needed, but you might
3032
var _html = function (d) { return 'you need to provide an html() handling param: ' + JSON.stringify(d); };
3133
var _sortBy = function (d) {
@@ -34,54 +36,56 @@ dc.dataGrid = function (parent, chartGroup) {
3436
var _order = d3.ascending;
3537
var _beginSlice = 0, _endSlice;
3638

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 + '\'>' +
3941
_chart.keyAccessor()(d) + '</h1></div>';
4042
};
4143

44+
_chart._mandatoryAttributes(['dimension', 'section']);
45+
4246
_chart._doRender = function () {
4347
_chart.selectAll('div.' + GRID_CSS_CLASS).remove();
4448

45-
renderItems(renderGroups());
49+
renderItems(renderSections());
4650

4751
return _chart;
4852
};
4953

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)
5256
.data(nestEntries(), function (d) {
5357
return _chart.keyAccessor()(d);
5458
});
5559

56-
var itemGroup = groups
60+
var itemSection = sections
5761
.enter()
5862
.append('div')
5963
.attr('class', GRID_CSS_CLASS);
6064

61-
if (_htmlGroup) {
62-
itemGroup
65+
if (_htmlSection) {
66+
itemSection
6367
.html(function (d) {
64-
return _htmlGroup(d);
68+
return _htmlSection(d);
6569
});
6670
}
6771

68-
groups.exit().remove();
69-
return itemGroup;
72+
sections.exit().remove();
73+
return itemSection;
7074
}
7175

7276
function nestEntries () {
7377
var entries = _chart.dimension().top(_size);
7478

7579
return d3.nest()
76-
.key(_chart.group())
80+
.key(_chart.section())
7781
.sortKeys(_order)
7882
.entries(entries.sort(function (a, b) {
7983
return _order(_sortBy(a), _sortBy(b));
8084
}).slice(_beginSlice, _endSlice));
8185
}
8286

83-
function renderItems (groups) {
84-
var items = groups.order()
87+
function renderItems (sections) {
88+
var items = sections.order()
8589
.selectAll('div.' + ITEM_CSS_CLASS)
8690
.data(function (d) {
8791
return d.values;
@@ -106,21 +110,40 @@ dc.dataGrid = function (parent, chartGroup) {
106110
};
107111

108112
/**
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
110114
* 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.
112116
*
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
115119
* @memberof dc.dataGrid
116120
* @instance
117121
* @example
118-
* // group rows by the value of their field
122+
* // section rows by the value of their field
119123
* 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
121142
* @param {Function} groupFunction Function taking a row of data and returning the nest key.
122-
* @returns {Function|dc.dataTable}
143+
* @returns {Function|dc.dataGrid}
123144
*/
145+
_chart.group = dc.logger.annotate(_chart.section,
146+
'consider using dataGrid.section instead of dataGrid.group for clarity');
124147

125148
/**
126149
* 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) {
193216
};
194217

195218
/**
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
198221
* @memberof dc.dataGrid
199222
* @instance
200223
* @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]
203226
* @returns {Function|dc.dataGrid}
204227
*/
205-
_chart.htmlGroup = function (htmlGroup) {
228+
_chart.htmlSection = function (htmlSection) {
206229
if (!arguments.length) {
207-
return _htmlGroup;
230+
return _htmlSection;
208231
}
209-
_htmlGroup = htmlGroup;
232+
_htmlSection = htmlSection;
210233
return _chart;
211234
};
212235

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+
213247
/**
214248
* Get or set sort-by function. This function works as a value accessor at the item
215249
* level and returns a particular field to be sorted.

web/ep/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148

149149
dc.dataGrid(".dc-data-grid")
150150
.dimension(country)
151-
.group(function (d) {
151+
.section(function (d) {
152152
return d.country;
153153
})
154154
.size(1000)

0 commit comments

Comments
 (0)