From 4bebbfd3faea9b2d06514ee2234354c3c0299857 Mon Sep 17 00:00:00 2001 From: Mohammed Aabid Date: Wed, 15 Nov 2017 19:46:19 +0530 Subject: [PATCH 1/2] Added ajax post request functionality --- src/orgchart.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/orgchart.js b/src/orgchart.js index 5711a78..9022b17 100644 --- a/src/orgchart.js +++ b/src/orgchart.js @@ -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); @@ -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(); @@ -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) { @@ -1762,4 +1766,4 @@ export default class OrgChart { } } } -} +} \ No newline at end of file From af6001fcafb4414b20ffcfe970056d3c2f5e61dd Mon Sep 17 00:00:00 2001 From: Mohammed Aabid Date: Wed, 15 Nov 2017 19:57:41 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 231188a..84e7cc0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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)