Skip to content
Open
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ let orgchart = new OrgChart({
'depth': 2,
'nodeContent': 'title'
});

// sample code for post ajax request
let orgchart = new OrgChart({
'chartContainer': '#chart-container',
'data' : {
ajax:true,
type:'POST',
url:'/orgchart/initdata',
data:{}, // data will be sent with post request
headers:{} // to set request headers
},
'depth': 2,
'nodeContent': 'title'
});
```
![ajax datasource](http://dabeng.github.io/OrgChart.js/ajax-datasource/recorder.gif)

Expand Down Expand Up @@ -195,6 +209,15 @@ orgchart = new OrgChart({
'nodeContent': 'title',
'nodeId': 'id'
});

// data sample for post ajax request
'data' : {
ajax:true,
type:'POST',
url:'/orgchart/initdata',
data:{}, // data will be sent with post request
headers:{} // to set request headers
},
```
![on-demand loading data](http://dabeng.github.io/OrgChart.js/ondemand-loading-data/recorder.gif)

Expand Down
18 changes: 11 additions & 7 deletions src/orgchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class OrgChart {
chart.dataset.options = JSON.stringify(opts);
chart.setAttribute('class', 'orgchart' + (opts.chartClass !== '' ? ' ' + opts.chartClass : '') +
(opts.direction !== 't2b' ? ' ' + opts.direction : ''));
if (typeof data === 'object') { // local json datasource
if (typeof data === 'object' && !data.ajax) { // local json datasource
this.buildHierarchy(chart, opts.ajaxURL ? data : this._attachRel(data, '00'), 0);
} else if (typeof data === 'string' && data.startsWith('#')) { // ul datasource
this.buildHierarchy(chart, this._buildJsonDS(document.querySelector(data).children[0]), 0);
Expand Down Expand Up @@ -180,7 +180,7 @@ export default class OrgChart {
ancestors.forEach((el) => results.push(...el.querySelectorAll(selector)));
return results;
}
_getJSON(url) {
_getJSON(options) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();

Expand All @@ -189,17 +189,21 @@ export default class OrgChart {
return;
}
if (this.status === 200) {
resolve(JSON.parse(this.response));
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
}
xhr.open('GET', url);
xhr.open((options.type || 'GET'), options.url);
xhr.onreadystatechange = handler;
xhr.responseType = 'json';
xhr.responseType = options.responseType || 'json';
let data = options.data || {};
// xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();
for(let i in options.headers){
xhr.setRequestHeader(i, options.headers[i]);
}
xhr.send(JSON.stringify(data));
});
}
_buildJsonDS(li) {
Expand Down Expand Up @@ -1762,4 +1766,4 @@ export default class OrgChart {
}
}
}
}
}