<template> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body width="40%"> <el-form ref="dataForm" :model="userReplyForm" :rules="rules" label-width="100px"> <el-form-item label="常用语" prop="content"> <el-input v-model="userReplyForm.content" type="textarea" clearable/> </el-form-item> <el-form-item label="排序" prop="num"> <el-input v-model.number="userReplyForm.num" clearable/> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="saveData">保存</el-button> <el-button @click="cancel">取消</el-button> </div> </el-dialog> </template> <script> import { add, update } from '@/api/userReply/userReply' export default { name: 'EditUserReply', data() { return { dialogFormVisible: false, dialogStatus: '', userReplyForm: { id: '', content: '', num: '' }, textMap: { update: '编辑', create: '新增' }, userList: [], rules: { content: [{ required: true, message: '常用语内容不能为空', trigger: ['blur', 'change'] }], num: [{ required: true, message: '排序不能为空', trigger: ['blur', 'change'] }, { type: 'number', min: 0, message: '必须为数字值', trigger: ['blur', 'change'] }] } } }, methods: { initDialog(dialogFormVisible, dialogStatus, row = null) { this.dialogFormVisible = dialogFormVisible this.dialogStatus = dialogStatus if (this.dialogStatus === 'create') { // 新增时,清空表格和校验 this.resetForm() this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) } else if (this.dialogStatus === 'update') { // 更新时,填充数据 this.userReplyForm.id = row.id this.userReplyForm.content = row.content this.userReplyForm.num = row.num } }, resetForm() { this.userReplyForm = { id: '', content: '', num: '' } }, cancel() { this.dialogFormVisible = false this.$emit('watchChild') }, saveData() { if (this.dialogStatus === 'create') { this.createData() } else if (this.dialogStatus === 'update') { this.updateData() } }, createData() { this.$refs['dataForm'].validate(valid => { if (valid) { add(this.userReplyForm).then(response => { if (response.code === 200) { this.$message.success('添加成功') this.$emit('watchChild') this.dialogFormVisible = false } else { this.$message.error('添加失败') } }) } else { console.log('create valid error') } }) }, updateData() { this.$refs['dataForm'].validate(valid => { if (valid) { console.log('update valid success') console.log(this.userReplyForm) update(this.userReplyForm).then(response => { if (response.code === 200) { this.$message.success('修改成功') this.$emit('watchChild') this.dialogFormVisible = false } else { this.$message.error('修改失败') } }) } else { console.log('update valid error') } }) } } } </script> <style scoped> .el-form-item { margin-bottom: 22px; margin-left: 20px; margin-right: 50px; } </style>