Skip to content

Commit 54bf032

Browse files
committed
Add working support for datatable configurations
1 parent 6fc0f0d commit 54bf032

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

README.md

+26-23
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,44 @@ With regards to support for dynamic data source, note that the plugin can only i
1212
npm install bcopy/gitbook-plugin-datatables
1313
```
1414

15-
* Add the plugin to your `book.json` :
15+
* Add the plugin to your `book.json`, for example :
1616

1717
```json
1818
{
19-
"plugins": ["datatables"]
19+
"plugins": ["datatables"],
20+
(...)
21+
"pluginsConfig":{
22+
"datatables":{
23+
"config-url" : "/data/dtconfig.json",
24+
"default-config": {"pageLength": 50 }
25+
}
26+
}
2027
}
2128
```
2229

2330
## DataTables Configuration
2431

25-
In order to configure your datatables, you can use [inline HTML 5 data attributes](https://datatables.net/manual/options#HTML-5-data-attributes) for simple cases, or provide a javascript module exposing the configuration associating a CSS selector with the configuration you wish to see applied.
26-
For it to work, your module must export an object constant ```DATATABLES_CONFIG```, as in the example below.
32+
In order to configure your datatables, you can use [inline HTML 5 data attributes](https://datatables.net/manual/options#HTML-5-data-attributes) for simple cases, or provide a JSON object associating a CSS selector with the datatable configuration you wish to see applied to matching DOM elements.
33+
For it to work, your JSON file must be valid, as in the example below.
2734

2835
For instance, to configure an HTML table bearing the ID "accounting", you could use :
2936

30-
```javascript
31-
module.exports = Object.freeze({
32-
DATATABLES_CONFIG : {
33-
"#accounting" : {
34-
ajax: '/data/objects.txt',
35-
columns: [
36-
{ data: 'name' },
37-
{ data: 'position' },
38-
{ data: 'office' },
39-
{ data: 'extn' },
40-
{ data: 'start_date' },
41-
{ data: 'salary' }
42-
],
43-
order: [[2, 'asc']],
44-
rowGroup: {
45-
dataSrc: 'office'
46-
}
47-
}
37+
```json
38+
{
39+
"#accounting":{
40+
"ajax":"/data/objects.txt",
41+
"columns":[
42+
{ "data":"name" },
43+
{ "data":"position" },
44+
{ "data":"office" },
45+
{ "data":"extn" },
46+
{ "data":"start_date" },
47+
{ "data":"salary" }
48+
],
49+
"order":[ [ 2, "asc" ] ],
50+
"rowGroup":{ "dataSrc":"office" }
4851
}
49-
});
52+
}
5053
```
5154

5255
## Features

assets/js/plugin.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@ require(["gitbook", "jQuery"], function(gitbook, $) {
44
var defaultDatatablesConfig = {}
55

66
gitbook.events.bind('start', function(e, bookPluginConfig) {
7-
// simply retain in parent scope (for subsequent use)
87
config = bookPluginConfig['datatables'];
98

10-
if (config.defaultConfig) {
11-
defaultDatatablesConfig = config.defaultConfig;
9+
if (config["default-config"]) {
10+
defaultDatatablesConfig = eval(config["default-config"]);
1211
}
1312

14-
// If configured, keep the datatablesConfig available
15-
if (config.configLocation) {
16-
// require the module and keep a copy of the map
17-
datatablesConfig = require(config.configLocation).DATATABLES_CONFIG;
13+
// If configured and valid JSON, initialize the datatablesConfig
14+
if (config["config-url"]) {
15+
$.ajax({ url: config["config-url"]
16+
, contentType: 'application/json; charset=UTF-8'
17+
, async: false
18+
, success: function( data ){
19+
datatablesConfig = eval(data);
20+
}
21+
, error: function(errorData){
22+
console.log("Datatables Config ajax call failed - invalid JSON or mime type ?", errorData);
23+
}
24+
});
1825
}
19-
20-
console.debug('start event ... config: ', config);
2126
});
2227

2328
// listen for gitbook "page.change" events
@@ -30,16 +35,14 @@ require(["gitbook", "jQuery"], function(gitbook, $) {
3035
var tableElement = $(this);
3136

3237
// Try and match this to any of the CSS selectors in the datables config map
33-
$.each(datatablesConfig,function(index, selector){
38+
$.each(datatablesConfig,function(selector){
3439
if(tableElement.is(selector)){
35-
$.extend(dtConfig, datatablesConfig[selector])
40+
$.extend(dtConfig, datatablesConfig[selector]);
3641
}
3742
});
3843

3944
// Construct the datatable and apply the custom configuration if any
4045
new DataTable(this,dtConfig);
4146
});
42-
43-
4447
});
4548
});

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
"force": {
1919
"type": "boolean",
2020
"required": false,
21-
"title": "Force all generated tables to support datatables"
21+
"title": "Force all generated tables to support datatables (if set to true, all HTML and markdown tables will become datatables)",
22+
"value": false
2223
},
23-
"configLocation": {
24+
"config-url": {
2425
"type": "string",
2526
"required": false,
26-
"title": "Location of a js module exposing DATATABLES_CONFIG"
27+
"title": "URL of a valid json file containing datatables configurations"
28+
},
29+
"default-config": {
30+
"type": "object",
31+
"required": false,
32+
"title": "Default datatables configuration to apply to all datatables"
2733
}
2834
}
2935
},

0 commit comments

Comments
 (0)