<template> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body> <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="200px"> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="物业服务编号" prop="code"> <el-input v-model.trim="form.code" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="物业服务名称" prop="name"> <el-input v-model.trim="form.name" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="物业计费(元/平方米*月)" prop="money"> <el-input v-model.trim="form.money" :disabled="isEditMode" type="text" placeholder="必填" /> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="20" :offset="2"> <el-form-item label="备注"> <el-input v-model.trim="form.remarks" :disabled="isEditMode" :rows="3" type="textarea" placeholder="备注" /> </el-form-item> </el-col> </el-row> </el-form> <div slot="footer" class="dialog-footer" v-if="!isEditMode"> <el-button :loading="btnLoading" type="primary" @click="saveData"> 保存 </el-button> <el-button @click="cancel"> 取消 </el-button> </div> </el-dialog> </template> <script> import { addCaseLevel, updateCaseLevel } from '@/api/case' import { validateFloatPlus } from '@/utils/validate' export default { name: 'EditCaseLevel', data() { const validateNum = (rule, value, callback) => { if (value !== '') { if (validateFloatPlus(value,0,1000) === true) { callback() } else { callback(new Error('请输入正数')) } } else { callback(new Error('物业计费不能为空')) } } return { dialogFormVisible: false, // 对话框是否显示 dialogStatus: '', // 对话框类型:create,update isEditMode: true, form: { id: '', code: '', name: '', money:'', remarks:'' }, // 表单 textMap: { update: '编辑物业服务', create: '新增物业服务', detail: '物业服务详情' }, // 表头显示标题 btnLoading: false, // 保存按钮的加载中状态 rules: { code: [{ required: true, message: '编号不能为空', trigger: ['blur', 'change'] }], name: [{ required: true, message: '物业服务名称不能为空', trigger: ['blur', 'change'] }], money: [{ required: true, validator: validateNum, trigger: ['blur', 'change'] }] } // 前端校验规则 } }, computed: { }, created() { }, methods: { // 初始化对话框 initDialog: function(dialogStatus, row = null) { this.dialogStatus = dialogStatus this.dialogFormVisible = true this.btnLoading = false if (dialogStatus === 'create') { // 如果是新增,清除验证 this.resetForm() this.isEditMode = false this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中 this.form = { id: row.id, code: row.code, name: row.name, money:row.money, remarks:row.remarks } this.isEditMode = false }else if (dialogStatus === 'detail') { this.form = { id: row.id, code: row.code, name: row.name, money:row.money, remarks:row.remarks } this.isEditMode = true } }, // 重置表单 resetForm() { this.form = { id: '', code: '', name: '', money:'', remarks:'' } }, // 保存数据 saveData: function() { if (this.dialogStatus === 'update') { this.updateData() } else if (this.dialogStatus === 'create') { this.createData() } }, // 新增数据 createData: function() { this.$refs['dataForm'].validate((valid) => { console.log(this.form) if (valid) { this.btnLoading = true addCaseLevel(this.form).then(response => { if (response.code === 200) { this.$confirm('新增成功,是否继续新增?', '提示', { confirmButtonText: '是', cancelButtonText: '否', type: 'info' }).then(() => { // 选是 this.resetForm() this.btnLoading = false this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) }).catch(() => { // 选否,关闭窗口,并刷新页面 this.$emit('watchChild') this.dialogFormVisible = false }) } }).catch(_ => { // 异常情况,loading置为false this.btnLoading = false }) } }) }, // 修改数据 updateData: function() { this.$refs['dataForm'].validate((valid) => { if (valid) { this.btnLoading = true updateCaseLevel(this.form).then(response => { if (response.code === 200) { this.$message.success('修改成功') this.$refs['dataForm'].clearValidate() this.$emit('watchChild') this.dialogFormVisible = false } }).catch(_ => { // 异常情况,loading置为false this.btnLoading = false }) } }) }, cancel: function() { this.dialogFormVisible = false // this.$emit('watchChild') } } } </script> <style rel="stylesheet/scss" lang="scss" scoped> .el-dialog{ width:700px } .el-select{ width: 100%; } </style>