Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataTable showGroups getter/setter #863

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions spec/data-table-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ describe('dc.dataTable', function() {
var dateFixture;
var dimension, group;
var countryDimension;
var valueGroup;

beforeEach(function() {
dateFixture = loadDateFixture();
Expand All @@ -13,14 +14,15 @@ describe('dc.dataTable', function() {
countryDimension = data.dimension(function(d) {
return d.countrycode;
});
valueGroup = function() {
return "Data Table";
};

id = "data-table";
appendChartID(id);
chart = dc.dataTable("#" + id)
.dimension(dimension)
.group(function(d) {
return "Data Table";
})
.group(valueGroup)
.transitionDuration(0)
.size(3)
.sortBy(function(d){return d.id;})
Expand Down Expand Up @@ -51,6 +53,12 @@ describe('dc.dataTable', function() {
it('sets order', function() {
expect(chart.order()).toBe(d3.descending);
});
it('group should be set', function() {
expect(chart.group()).toEqual(valueGroup);
});
it('group tr should not be undefined', function() {
expect(typeof(chart.selectAll("tr.dc-table-group")[0][0])).not.toBe('undefined');
});
it('sets column span set on group tr', function() {
expect(chart.selectAll("tr.dc-table-group td")[0][0].getAttribute("colspan")).toEqual("2");
});
Expand Down Expand Up @@ -168,6 +176,16 @@ describe('dc.dataTable', function() {
});
});

describe('specifying showGroups as false', function () {
beforeEach(function() {
chart.showGroups(false);
chart.render();
});
it('group tr should be undefined', function() {
expect(typeof(chart.selectAll("tr.dc-table-group")[0][0])).toBe('undefined');
});
});

afterEach(function() {
dimension.filterAll();
countryDimension.filterAll();
Expand Down
41 changes: 32 additions & 9 deletions src/data-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dc.dataTable = function (parent, chartGroup) {
return d;
};
var _order = d3.ascending;
var _showGroups = true;

_chart._doRender = function () {
_chart.selectAll('tbody').remove();
Expand Down Expand Up @@ -127,15 +128,17 @@ dc.dataTable = function (parent, chartGroup) {
.enter()
.append('tbody');

rowGroup
.append('tr')
.attr('class', GROUP_CSS_CLASS)
.append('td')
.attr('class', LABEL_CSS_CLASS)
.attr('colspan', _columns.length)
.html(function (d) {
return _chart.keyAccessor()(d);
});
if (_showGroups === true) {
rowGroup
.append('tr')
.attr('class', GROUP_CSS_CLASS)
.append('td')
.attr('class', LABEL_CSS_CLASS)
.attr('colspan', _columns.length)
.html(function (d) {
return _chart.keyAccessor()(d);
});
}

groups.exit().remove();

Expand Down Expand Up @@ -329,5 +332,25 @@ dc.dataTable = function (parent, chartGroup) {
return _chart;
};

/**
### .showGroups(true|false)
Get or set if group rows will be shown. Default value ``` true ```
The .group() getter-setter must be provided in either case.

```js
chart
.group([value], [name])
.showGroups(true|false);
```

**/
_chart.showGroups = function (_) {
if (!arguments.length) {
return true;
}
_showGroups = _;
return _chart;
};

return _chart.anchor(parent, chartGroup);
};