🌐 AI搜索 & 代理 主页
Skip to content
This repository was archived by the owner on Oct 21, 2023. It is now read-only.

Commit 53f21c4

Browse files
specify title and type, use them in example, and document them
1 parent b41b5ff commit 53f21c4

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,35 @@
33
The intent of this project is to integrate the powerful UI, styling, and plugins of
44
[DataTables](https://datatables.net/) with dc.js filtered data.
55

6-
[Demo](https://dc-js.github.io/dc.datatables.js/) using the dc.js stock example, but with a
7-
DataTable to display the data in a flexible way.
6+
[Demo](https://dc-js.github.io/dc.datatables.js/) using the dc.js stock example, but displaying the
7+
data with a DataTable.
88

9-
For now the implementation is quite simple:
9+
Instead of generating an HTML table using `dc.dataTable` and then converting the DOM elements to a
10+
DataTable, use `dc.datatables.js` to import the data programmatically. This should be faster and
11+
less error-prone.
12+
13+
Implementation:
1014

1115
* `.render()` creates the DataTable object and maps
1216
[columns](https://dc-js.github.io/dc.js/docs/html/dc.dataTable.html#columns__anchor) defined
1317
similar to those in `dc.dataTable`
1418
* `.redraw()` pulls all the data (`.top(Infinity`) from the dimension and puts it into the
15-
DataTable.
19+
DataTable using [rows.add()](https://datatables.net/reference/api/rows.add())
1620
* `.dt()` fetches the underlying DataTable object for further customization.
1721

18-
This method is much faster and more reliable than the old method of generating an HTML table using
19-
`dc.dataTable` and then converting the DOM elements to a DataTable.
22+
For control over column formatting and behavior, use the object form of columns
23+
24+
* [columns.type](https://datatables.net/reference/option/columns.type), used for sorting, is 'num'
25+
by default; use `type` to override this
26+
* [columns.name](https://datatables.net/reference/option/columns.name), used as a unique id, is read
27+
from the `label`
28+
* [columns.title](https://datatables.net/reference/option/columns.title), used to provide heading
29+
text, is read from the `label` and capitalized
30+
* [columns.render](https://datatables.net/reference/option/columns.render), used to fetch and format
31+
the data, uses the function `format`
32+
33+
In limited cases, you can also use the string form of columns. This will read the field with that
34+
name, use the default numeric ordering
2035

2136
See something missing? [File an issue](https://github.com/dc-js/dc.datatables.js/issues) on this
2237
repo, or even better, fork this project and file a pull request!

src/datatable.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ dc_datatables.datatable = function(selector, chartGroup) {
3737
.merge(table);
3838
_dt = $(table.node()).DataTable({
3939
columns: _table.columns().map(function(c) {
40-
return {
40+
var col = {
4141
name: typeof c === 'string' ? c : c.label,
42+
type: typeof c === 'object' ? c.type : 'num',
4243
render: columnRenderer(c)
4344
};
45+
col.title = col.name.charAt(0).toUpperCase() + col.name.slice(1);
46+
return col;
4447
})
4548
});
4649
return _table.redraw();

web/stock.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,15 @@ d3.csv('ndx.csv').then(function (data) {
533533
// There are several ways to specify the columns; see the data-table documentation.
534534
// This code demonstrates generating the column header automatically based on the columns.
535535
.columns([
536-
// Use the `d.date` field; capitalized automatically
537-
'date',
538-
// Use `d.open`, `d.close`
536+
// Use the `d.date` field; capitalized automatically; specify sorting order
537+
{
538+
label: 'date',
539+
type: 'date',
540+
format: function(d) {
541+
return d.date;
542+
}
543+
},
544+
// Use `d.open`, `d.close`; default sorting order is numeric
539545
'open',
540546
'close',
541547
{

0 commit comments

Comments
 (0)