UNPKG

sohelp-ele

Version:

SohelpEle Library

172 lines (171 loc) 4.87 kB
"use strict"; var TableDatasourceMixin = { data() { return { defaultValue: {}, insertedId: 1, datasource: [], originDataSource: [], modifyDataSource: { deleted: [], updated: [], inserted: [] } }; }, methods: { add(record) { this.insert(record, 1); }, insert(record, index) { let data = { native_record_status: "inserted" }; if (this.defaultValues) { Object.assign(data, this.defaultValues); } if (record) { Object.assign(data, record); } this.modifyDataSource.inserted.push(data); data[this.tableConfiguration.primaryKey] = "*" + this.insertedId; this.insertedId++; let arr = []; if (this.datasource.length === 0) { arr.push(data); } for (let i = 0; i < this.datasource.length; i++) { if (index - 1 === i) { arr.push(data); arr.push(this.datasource[i]); } else { arr.push(this.datasource[i]); } } this.datasource = arr; }, remove(records) { let arr = []; let sum = []; if (!this.selection.length) { this.$message.error("\u8BF7\u9009\u62E9\u8981\u5220\u9664\u7684\u6570\u636E"); } for (let i = 0; i < this.selection.length; i++) { let data = {}; data[this.tableConfiguration.primaryKey] = this.selection[i][this.tableConfiguration.primaryKey]; this.modifyDataSource.deleted.push(data); sum.push(this.selection[i][this.tableConfiguration.primaryKey]); } this.datasource.forEach((rec) => { if (!sum.includes(rec[this.tableConfiguration.primaryKey])) { arr.push(rec); } }); this.datasource = arr; this.selection = []; }, async isDone() { return new Promise((resolve) => { if (this.done) { resolve(); } else { let timer = setInterval(() => { if (this.done) { clearInterval(timer); resolve(); } }, 17); } }); }, async load(params) { await this.isDone(); this.params = {}; if (params) { Object.assign(this.params, params); } let filterParams = this.getFilterParams(); if (filterParams) { Object.assign(this.params, filterParams); } if (this.baseParams) { Object.assign(this.params, this.baseParams); } let url = this.tableConfiguration.url; if (this.tableConfiguration.namespace) { url = url + "?namespace=" + this.tableConfiguration.namespace; } this.loading = true; return SohelpHttp.get(url, this.params).then((res) => { var _a; this.loading = false; if (res.meta.success) { let data = ((_a = res.data) == null ? void 0 : _a.results) || res.data; this.datasource = data; this.total = typeof res.data.total == "string" ? Number(res.data.total) : res.data.total; this.originDataSource = data; this.modifyDataSource = { deleted: [], updated: [], inserted: [] }; this.$nextTick(() => { var _a2; (_a2 = this.$refs.table) == null ? void 0 : _a2.doLayout(); }); } else { this.$message.error(res.meta.message); } }).catch((e) => { this.$message.error(e); }); }, refresh() { return this.reload({}); }, reset() { this.datasource = this.originDataSource; this.modifyDataSource = { deleted: [], updated: [], inserted: [] }; }, reload(params) { if (this.baseParams) { this.params = Object.assign(this.params, this.baseParams); } if (params) { this.params = Object.assign(this.params, params); } return this.load(this.params); }, update(record) { this.datasource.forEach((e) => { if (e.id === record.rowKey) { Object.assign(e, record); } }); }, clearModifyDataSource() { this.$set(this.modifyDataSource, "delected", []); this.$set(this.modifyDataSource, "updated", []); this.$set(this.modifyDataSource, "inserted", []); }, save() { let data = this.getRecordUpdater(); return SohelpHttp.post("/engine/service/crud/save", { data }).then((res) => { if (res.meta.success) { Promise.resolve(res.data); this.$message.success(res.meta.message); this.clearModifyDataSource(); this.refresh(); } else { this.$message.error(res.meta.message); Promise.reject(res.meta.message); } }); } } }; module.exports = TableDatasourceMixin;