export default { data () { return { show: false, currObj: {} } }, computed: { isAdd () { return !this.currObj.id }, dialogTitle () { return this.isAdd ? `新增${this.type}` : `编辑${this.type}` } }, created () { this.getList() }, methods: { getDefaults () { return { name: '', remark: '' } }, transform (data) { return data }, toAdd () { this.currObj = this.getDefaults() this.show = true }, beforeEdit ({ id, name, remark }) { return { id, name, remark } }, toEdit (item) { this.currObj = this.beforeEdit(item) this.$currObj = { ...this.currObj } this.show = true }, close () { this.$currObj = null this.show = false }, check () { return false }, search () { const options = this.options options.list = [] options.totalCount = 0 options.params.pageNum = 1 this.getList() }, getList () { const options = this.options options.error = false options.loading = true this.proxy.list(options.params).then(({ data, totalCount }) => { options.list = this.transform(data) options.totalCount = totalCount }, () => { options.error = true options.list = [] }).finally(() => { options.loading = false }) }, afterAdd () { }, save () { if (this.check(this.currObj)) { if (!this.isAdd && Object.keys(this.$currObj).every(key => this.currObj[key] === this.$currObj[key])) { this.close() return } this.proxy[this.isAdd ? 'add' : 'update'](this.currObj).then(() => { if (this.isAdd) { this.afterAdd() this.search() } else { this.getList() } this.close() }) } }, toDel (item) { const options = this.options || this this.proxy.del(item).then(() => { if (options.list.length === 1 && options.params.pageNum > 1) { options.params.pageNum -= 1 } this.getList() }) } } }