From af09daa58a6e76ec61ab3936d093fe4066bbff27 Mon Sep 17 00:00:00 2001 From: leebeomjun Date: Thu, 15 Jun 2023 16:29:43 +0900 Subject: [PATCH] =?UTF-8?q?dataset.js=20=EB=B3=80=EA=B2=BD=EB=90=9C=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80(addData=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/webapp/resources/js/base/dataset.js | 67 +++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) 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);