ADD 模板 CRUD列表

This commit is contained in:
sakuya 2022-05-21 19:02:39 +08:00
parent 780f713856
commit e21fecbc80
4 changed files with 347 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<template>
<sc-page-header :title="id?'编辑':'新增'" description="可用于非常复杂的表单提交如一些较为简单的表单提交应使用dialog或者drawer更合适" icon="el-icon-burger"></sc-page-header>
<el-main>
<el-alert title="注意: 因为keep-alive只接受组件name,导致多路由共用组件时,关闭或刷新一个标签导致其他同一组件的页面缓存失效,后续还在寻找完美的解决方案.建议在列表页使用dialog或者drawer形式" type="error" style="margin-bottom: 15px;"></el-alert>
<el-card shadow="never">
<el-form ref="form" label-width="100px">
<el-form-item label="id">
<el-input v-model="id"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary">保存</el-button>
</el-form-item>
</el-form>
</el-card>
</el-main>
</template>
<script>
export default {
name: 'listCrud-detail',
data() {
return {
id: this.$route.query.id,
input: ""
}
},
created() {
},
mounted() {
//tab
this.$store.commit("updateViewTagsTitle", this.id?`CURD编辑ID:${this.id}`:"CURD新增")
},
methods: {
}
}
</script>
<style>
</style>

View File

@ -0,0 +1,158 @@
<template>
<el-container>
<el-header>
<div class="left-panel">
<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
<el-button type="primary" icon="el-icon-plus" @click="addPage">页面新增</el-button>
<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
</div>
</el-header>
<el-main class="nopadding">
<scTable ref="table" :apiObj="list.apiObj" row-key="id" @selection-change="selectionChange" stripe>
<el-table-column type="selection" width="50"></el-table-column>
<el-table-column label="姓名" prop="name" width="180"></el-table-column>
<el-table-column label="性别" prop="sex" width="150"></el-table-column>
<el-table-column label="邮箱" prop="email" width="250"></el-table-column>
<el-table-column label="状态" prop="boolean" width="60">
<template #default="scope">
<sc-status-indicator v-if="scope.row.boolean" type="success"></sc-status-indicator>
<sc-status-indicator v-if="!scope.row.boolean" pulse type="danger"></sc-status-indicator>
</template>
</el-table-column>
<el-table-column label="评分" prop="num" width="150"></el-table-column>
<el-table-column label="操作" fixed="right" align="right" width="300">
<template #default="scope">
<el-button plain size="small" @click="table_show(scope.row)">查看</el-button>
<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
<el-button type="primary" plain size="small" @click="table_edit_page(scope.row)">页面编辑</el-button>
<el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row, scope.$index)">
<template #reference>
<el-button plain type="danger" size="small">删除</el-button>
</template>
</el-popconfirm>
</template>
</el-table-column>
</scTable>
</el-main>
</el-container>
<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
<el-drawer v-model="dialog.info" :size="800" title="详细" custom-class="drawerBG" direction="rtl" destroy-on-close>
<info ref="infoDialog"></info>
</el-drawer>
</template>
<script>
import saveDialog from './save'
import info from './info'
export default {
name: 'listCrud',
components: {
saveDialog,
info
},
data() {
return {
dialog:{
save: false,
info: false
},
list: {
apiObj: this.$API.demo.list
},
selection: []
}
},
mounted() {
},
methods: {
//
add(){
this.dialog.save = true
this.$nextTick(() => {
this.$refs.saveDialog.open()
})
},
//
table_edit(row){
this.dialog.save = true
this.$nextTick(() => {
this.$refs.saveDialog.open('edit').setData(row)
})
},
//
addPage(){
this.$router.push({
path: '/template/list/crud/detail',
})
},
//
table_edit_page(row){
this.$router.push({
path: '/template/list/crud/detail',
query: {
id: row.id
}
})
},
//
table_show(row){
this.dialog.info = true
this.$nextTick(() => {
this.$refs.infoDialog.setData(row)
})
},
//
async table_del(row, index){
var reqData = {id: row.id}
var res = await this.$API.demo.post.post(reqData);
if(res.code == 200){
this.$refs.table.removeIndex(index)
this.$message.success("删除成功")
}else{
this.$alert(res.message, "提示", {type: 'error'})
}
},
//
async batch_del(){
var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗?`, '提示', {
type: 'warning',
confirmButtonText: '删除',
confirmButtonClass: 'el-button--danger'
}).catch(() => {})
if(!confirmRes){
return false
}
var ids = this.selection.map(v => v.id)
this.$refs.table.removeKeys(ids)
this.$message.success("操作成功")
},
//
selectionChange(selection){
this.selection = selection
},
//
handleSaveSuccess(data, mode){
//
if(mode=='add'){
this.$refs.table.unshiftRow(data)
}else if(mode=='edit'){
this.$refs.table.updateKey(data)
}
//
// this.$refs.table.refresh()
}
}
}
</script>
<style>
</style>

View File

@ -0,0 +1,33 @@
<template>
<el-main style="padding:0 20px;">
<el-card shadow="never">
<el-descriptions title="Table row data" :column="2" border>
<el-descriptions-item v-for=" (val, key) in data" :key="key" :label="key" width="150px">{{val}}</el-descriptions-item>
</el-descriptions>
</el-card>
</el-main>
</template>
<script>
export default {
data() {
return {
data: {
id: ""
}
}
},
mounted() {
},
methods: {
//
setData(data){
this.data = data
}
}
}
</script>
<style>
</style>

View File

@ -0,0 +1,114 @@
<template>
<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name" placeholder="请输入姓名" clearable></el-input>
</el-form-item>
<el-form-item label="性别" prop="sex">
<el-radio-group v-model="form.sex">
<el-radio label="男"></el-radio>
<el-radio label="女"></el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
</el-form-item>
<el-form-item label="评分" prop="num">
<el-input-number v-model="form.num" :min="0" style="width: 100%;"></el-input-number>
</el-form-item>
<el-form-item label="状态" prop="boolean">
<el-switch v-model="form.boolean"></el-switch>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="visible=false" > </el-button>
<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()"> </el-button>
</template>
</el-dialog>
</template>
<script>
export default {
emits: ['success', 'closed'],
data() {
return {
mode: "add",
titleMap: {
add: '新增',
edit: '编辑',
show: '查看'
},
visible: false,
isSaveing: false,
//
form: {
id:"",
name: "",
sex: "男",
email: "",
num: 0,
boolean: true
},
//
rules: {
name: [
{required: true, message: '请输入姓名'}
]
},
//
groups: [],
groupsProps: {
value: "id",
emitPath: false,
checkStrictly: true
}
}
},
mounted() {
},
methods: {
//
open(mode='add'){
this.mode = mode;
this.visible = true;
return this
},
//
async submit(){
var valid = await this.$refs.dialogForm.validate()
if(!valid){
return false
}
this.isSaveing = true;
var res = await this.$API.demo.post.post(this.form);
this.isSaveing = false;
if(res.code == 200){
if(this.mode=='add'){
this.form.id = res.data
}
this.$emit('success', this.form, this.mode)
this.visible = false;
this.$message.success("操作成功")
}else{
this.$alert(res.message, "提示", {type: 'error'})
}
},
//
setData(data){
this.form.id = data.id
this.form.name = data.name
this.form.sex = data.sex
this.form.email = data.email
this.form.num = data.num
this.form.boolean = data.boolean
//
//Object.assign(this.form, data)
}
}
}
</script>
<style>
</style>