Commit 6e2a9f0f by Moxun1639

自定义数据id字段名

parent 23105943
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<p>{{ msg }}</p> <p>{{ msg }}</p>
<div style="text-align: right; margin: 0"> <div style="text-align: right; margin: 0">
<el-button size="mini" type="text" @click="doCancel">取消</el-button> <el-button size="mini" type="text" @click="doCancel">取消</el-button>
<el-button :loading="crud.dataStatus[data.id].delete === 2" type="primary" size="mini" @click="crud.doDelete(data)">确定</el-button> <el-button :loading="crud.dataStatus[crud.getDataId(data)].delete === 2" type="primary" size="mini" @click="crud.doDelete(data)">确定</el-button>
</div> </div>
<el-button slot="reference" :disabled="disabledDle" type="danger" icon="el-icon-delete" size="mini" @click="toDelete" /> <el-button slot="reference" :disabled="disabledDle" type="danger" icon="el-icon-delete" size="mini" @click="toDelete" />
</el-popover> </el-popover>
......
...@@ -13,6 +13,8 @@ import Vue from 'vue' ...@@ -13,6 +13,8 @@ import Vue from 'vue'
function CRUD(options) { function CRUD(options) {
const defaultOptions = { const defaultOptions = {
tag: 'default', tag: 'default',
// id字段名
idField: 'id',
// 标题 // 标题
title: '', title: '',
// 请求数据的url // 请求数据的url
...@@ -165,7 +167,7 @@ function CRUD(options) { ...@@ -165,7 +167,7 @@ function CRUD(options) {
return return
} }
crud.status.edit = CRUD.STATUS.PREPARED crud.status.edit = CRUD.STATUS.PREPARED
crud.getDataStatus(data.id).edit = CRUD.STATUS.PREPARED crud.getDataStatus(crud.getDataId(data)).edit = CRUD.STATUS.PREPARED
callVmHook(crud, CRUD.HOOK.afterToEdit, crud.form) callVmHook(crud, CRUD.HOOK.afterToEdit, crud.form)
callVmHook(crud, CRUD.HOOK.afterToCU, crud.form) callVmHook(crud, CRUD.HOOK.afterToCU, crud.form)
}, },
...@@ -174,7 +176,7 @@ function CRUD(options) { ...@@ -174,7 +176,7 @@ function CRUD(options) {
* @param {*} data 数据项 * @param {*} data 数据项
*/ */
toDelete(data) { toDelete(data) {
crud.getDataStatus(data.id).delete = CRUD.STATUS.PREPARED crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.PREPARED
}, },
/** /**
* 取消删除 * 取消删除
...@@ -184,7 +186,7 @@ function CRUD(options) { ...@@ -184,7 +186,7 @@ function CRUD(options) {
if (!callVmHook(crud, CRUD.HOOK.beforeDeleteCancel, data)) { if (!callVmHook(crud, CRUD.HOOK.beforeDeleteCancel, data)) {
return return
} }
crud.getDataStatus(data.id).delete = CRUD.STATUS.NORMAL crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.NORMAL
callVmHook(crud, CRUD.HOOK.afterDeleteCancel, data) callVmHook(crud, CRUD.HOOK.afterDeleteCancel, data)
}, },
/** /**
...@@ -204,7 +206,7 @@ function CRUD(options) { ...@@ -204,7 +206,7 @@ function CRUD(options) {
return return
} }
crud.status.edit = CRUD.STATUS.NORMAL crud.status.edit = CRUD.STATUS.NORMAL
crud.getDataStatus(crud.form.id).edit = CRUD.STATUS.NORMAL crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL
} }
crud.resetForm() crud.resetForm()
if (addStatus === CRUD.STATUS.PREPARED) { if (addStatus === CRUD.STATUS.PREPARED) {
...@@ -268,7 +270,7 @@ function CRUD(options) { ...@@ -268,7 +270,7 @@ function CRUD(options) {
crud.status.edit = CRUD.STATUS.PROCESSING crud.status.edit = CRUD.STATUS.PROCESSING
crud.crudMethod.edit(crud.form).then(() => { crud.crudMethod.edit(crud.form).then(() => {
crud.status.edit = CRUD.STATUS.NORMAL crud.status.edit = CRUD.STATUS.NORMAL
crud.getDataStatus(crud.form.id).edit = CRUD.STATUS.NORMAL crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL
crud.editSuccessNotify() crud.editSuccessNotify()
crud.resetForm() crud.resetForm()
callVmHook(crud, CRUD.HOOK.afterSubmit) callVmHook(crud, CRUD.HOOK.afterSubmit)
...@@ -289,11 +291,11 @@ function CRUD(options) { ...@@ -289,11 +291,11 @@ function CRUD(options) {
if (data instanceof Array) { if (data instanceof Array) {
delAll = true delAll = true
data.forEach(val => { data.forEach(val => {
ids.push(val.id) ids.push(this.getDataId(val))
}) })
} else { } else {
ids.push(data.id) ids.push(this.getDataId(data))
dataStatus = crud.getDataStatus(data.id) dataStatus = crud.getDataStatus(this.getDataId(data))
} }
if (!callVmHook(crud, CRUD.HOOK.beforeDelete, data)) { if (!callVmHook(crud, CRUD.HOOK.beforeDelete, data)) {
return return
...@@ -403,7 +405,7 @@ function CRUD(options) { ...@@ -403,7 +405,7 @@ function CRUD(options) {
const dataStatus = {} const dataStatus = {}
function resetStatus(datas) { function resetStatus(datas) {
datas.forEach(e => { datas.forEach(e => {
dataStatus[e.id] = { dataStatus[crud.getDataId(e)] = {
delete: 0, delete: 0,
edit: 0 edit: 0
} }
...@@ -443,7 +445,7 @@ function CRUD(options) { ...@@ -443,7 +445,7 @@ function CRUD(options) {
*/ */
selectChange(selection, row) { selectChange(selection, row) {
// 如果selection中存在row代表是选中,否则是取消选中 // 如果selection中存在row代表是选中,否则是取消选中
if (selection.find(val => { return val.id === row.id })) { if (selection.find(val => { return this.getDataId(val) === this.getDataId(row) })) {
if (row.children) { if (row.children) {
row.children.forEach(val => { row.children.forEach(val => {
crud.findVM('presenter').$refs['table'].toggleRowSelection(val, true) crud.findVM('presenter').$refs['table'].toggleRowSelection(val, true)
...@@ -484,6 +486,12 @@ function CRUD(options) { ...@@ -484,6 +486,12 @@ function CRUD(options) {
}, },
updateProp(name, value) { updateProp(name, value) {
Vue.set(crud.props, name, value) Vue.set(crud.props, name, value)
},
getDataId(data) {
if (!data.hasOwnProperty(this.idField)) {
console.error('[CRUD error]: no property [%s] in %o', this.idField, data)
}
return data[this.idField]
} }
} }
const crud = Object.assign({}, data) const crud = Object.assign({}, data)
...@@ -592,6 +600,7 @@ function presenter(crud) { ...@@ -592,6 +600,7 @@ function presenter(crud) {
console.error('[CRUD error]: ' + 'crud with tag [' + crud.tag + ' is already exist') console.error('[CRUD error]: ' + 'crud with tag [' + crud.tag + ' is already exist')
} }
this.$crud[crud.tag] = crud this.$crud[crud.tag] = crud
this.crud = crud
crud.registerVM('presenter', this, 0) crud.registerVM('presenter', this, 0)
}, },
data() { data() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment