You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.4 KiB

2 years ago
<template>
<div>
<div>
<el-button icon="el-icon-plus" type="primary" size="small" @click="add"></el-button>
</div>
<vxe-table
stripe
style="margin-top: 10px;"
ref="table"
:loading="loading"
keep-source
show-overflow
:edit-rules="validRules"
:edit-config="{ trigger: 'manual', mode: 'row', showStatus: true }"
:align="allAlign"
:data="tableData">
<vxe-column type="seq" width="58" align="center"></vxe-column>
<vxe-column field="name" width="160" title="角色" :edit-render="{ name: 'input', attrs: { type: 'text'} }"></vxe-column>
<vxe-column field="sex" title="权限菜单" show-overflow="tooltip">
<template #default="{ row }">
<div>
<el-tag v-for="item in row.permissions" :key="item.id">{{ item.name }}</el-tag>
</div>
</template>
</vxe-column>
<vxe-column field="sortnumber" width="80" title="排序" align="center" :edit-render="{ name: 'input', attrs: { type: 'number' } }"></vxe-column>
<vxe-column field="operate" title="操作">
<template #default="{ row }">
<template v-if="isActiveStatus(row)">
<el-button size="small" type="primary" @click="saveRowEvent(row)"></el-button>
<el-button size="small" type="primary" plain @click="cancelRowEvent(row)"></el-button>
</template>
<template v-else>
<el-button size="small" type="warning" @click="editRowEvent(row)"></el-button>
</template>
<el-button size="small" type="danger" @click="destroyRowEvent(row)"></el-button>
</template>
</vxe-column>
</vxe-table>
<p class="total" type="primary"> {{ total }} 条数据</p>
</div>
</template>
<script>
import { deepCopy } from "@/utils"
import { index, save, destroy } from "@/api/role"
export default {
data() {
return {
loading: false,
total: 0,
allAlign: null,
tableData: [],
validRules: {
},
form: {
id: "",
name: "",
sortnumber: 0
}
}
},
methods: {
editRowEvent (row) {
if (this.$refs['table']) {
this.$refs['table'].setEditRow(row)
}
},
cancelRowEvent (row) {
if (this.$refs['table']) {
this.$refs['table'].clearEdit().then(() => {
// 还原行数据
this.$refs['table'].revertData(row)
})
}
},
async getList () {
this.loading = true;
try {
const res = await index()
this.tableData = res.rows;
this.total = res.total;
console.log(res)
this.loading = false;
} catch (err) {
console.error(err)
this.loading = false;
}
},
add () {
this.tableData.unshift(deepCopy(this.form))
if (this.$refs['table']) {
this.$nextTick(() => {
this.$refs['table'].setEditRow(this.$refs['table'].tableData[0])
})
}
},
async saveRowEvent (row) {
try {
await this.$confirm("确认保存?","提示",{
confirmButtonText: "确认",
cancelButtonText: "取消"
})
let form = deepCopy(this.form)
for (let key in form) {
form[key] = row[key]
}
this.loading = true;
await save(form)
await this.getList();
this.loading = false;
} catch (err) {
this.loading = false;
}
},
async destroyRowEvent (row) {
try {
await this.$confirm("确认删除?","提示",{
confirmButtonText: "确认",
cancelButtonText: "取消"
})
this.loading = true;
if (row.id) {
await destroy({
id: row.id
})
await this.getList();
} else {
console.log(row)
this.tableData.splice(this.tableData.findIndex(i => i._X_ROW_KEY === row._X_ROW_KEY),1)
}
this.loading = false;
} catch (err) {
this.loading = false;
}
},
},
computed: {
isActiveStatus () {
return function (row) {
if (this.$refs['table']) {
return this.$refs['table'].isEditByRow(row)
}
}
}
},
created() {
this.getList()
}
}
</script>
<style scoped lang="scss">
.total {
color: #666;
text-align: right;
line-height: 3;
}
::v-deep .el-tag + .el-tag {
margin-left: 4px;
}
</style>