diff --git a/src/main/webapp/resources/js/base/dataset.js b/src/main/webapp/resources/js/base/dataset.js index 6369fc0f..76324a3b 100644 --- a/src/main/webapp/resources/js/base/dataset.js +++ b/src/main/webapp/resources/js/base/dataset.js @@ -450,6 +450,10 @@ class Dataset { this._byKeys = {}; this._current = null; + let data = this._getDataItems(obj); + this._items = data.items; + this._byKeys = data.byKeys; + /* obj = obj || {}; let array = Array.isArray(obj) ? obj : this.conf.dataGetter(obj) || []; if (!Array.isArray(array)) @@ -460,12 +464,62 @@ class Dataset { let key = "key-" + this.getKey(item.data); this._byKeys[key] = item; }); + */ + this.onDatasetChange(obj); + this.setState(!Array.isArray(obj) ? obj.state : state); + this.onDirtiesChange(this.dirty); + + return this; + } + + _getDataItems(obj) { + obj = obj || {}; + let array = Array.isArray(obj) ? obj : this.conf.dataGetter(obj) || []; + if (!Array.isArray(array)) + throw new Error("The data must be an array"); + + let _items = array.map(e => new DataItem(e, this._formats)), + _byKeys = {}; + _items.forEach(item => { + let key = "key-" + this.getKey(item.data); + _byKeys[key] = item; + }); + + return { + items: _items, + byKeys: _byKeys + }; + } + + /**Adds user data to the Dataset. + * To get user data from an object, the dataGetter configured is called. + * After user data is set, the methods + * + * are called back. + * @param {array|object} obj user data or an object that has user data + * @returns {Dataset} the Dataset + */ + addData(obj) { + if (this.empty) + return this.setData(obj); + + let data = this._getDataItems(obj); + this._items = this._items.concat(data.items); + this._byKeys = { + ...this._byKeys, + ...data.byKeys + }; this.onDatasetChange(obj); this.setState(!Array.isArray(obj) ? obj.state : state); this.onDirtiesChange(this.dirty); return this; + } /**Clears the Dataset's user data. @@ -1140,6 +1194,7 @@ class DatasetControl { this.prefix = conf.prefix; this.prefixName = conf.prefixName; this.infoSize = conf.infoSize; + this.appendData = conf.appendData; this.query = {}; @@ -1184,13 +1239,23 @@ class DatasetControl { ajax.get({ url:this.urls.load, data:this.query, - success:resp => this.setData(resp) + success:resp => { + if (!this.appendData || this.query.pageNum == 1) + this.setData(resp); + else { + this.addData(resp); + } + } }); } setData(obj) { this.dataset.setData(obj); } + + addData(obj) { + this.dataset.addData(obj); + } onDatasetChange(obj) { debug("onDatasetChange", obj);