<template> <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible" append-to-body> <el-form ref="dataForm" :rules="rules" :model="cirForm" size="small" label-well-code="right" label-width="110px"> <el-row :gutter="20"> <el-col :span="12"> <el-form-item label="回路编号" prop="circuitCode"> <el-input v-model.trim="cirForm.circuitCode" :readonly="!isEditMode" :maxlength="15" clearable type="text" placeholder="必填" show-word-limit/> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="回路名称" prop="circuitName"> <el-input v-model.trim="cirForm.circuitName" :readonly="!isEditMode" :maxlength="30" clearable type="text" placeholder="必填" show-word-limit/> </el-form-item> </el-col> </el-row> <el-row :gutter="20"> <el-col :span="12"> <el-form-item label="回路序号" prop="circuitNum"> <el-select v-model="cirForm.circuitNum" :disabled="!isEditMode" placeholder="必填" filterable> <el-option key="1" label="1" value="1"/> <el-option key="2" label="2" value="2"/> <el-option key="3" label="3" value="3"/> <el-option key="4" label="4" value="4"/> </el-select> <!--<el-input v-model.trim="cirForm.circuitNum" :readonly="!isEditMode" placeholder="必填" type="number" max="4" min="0"/>--> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="所属灯箱" prop="lampboxId"> <el-select v-model="cirForm.lampboxId" :disabled="!isEditMode" placeholder="所属灯箱" filterable clearable> <el-option v-for="item in boxList" :key="item.id" :label="item.lampboxName" :value="item.id"/> </el-select> </el-form-item> </el-col> </el-row> </el-form> <div v-show="isEditMode" slot="footer" class="dialog-footer"> <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button> <el-button @click="cancel">取消</el-button> </div> </el-dialog> </template> <script> import DeptSelect from '@/components/DeptSelect' import { addCircuit, editCircuit, getAllLampboxList } from '@/api/system/device' export default { name: 'EditCircuit', components: { DeptSelect }, data() { const typeValidator = (rule, value, callback) => { if (value === 0 || value === '' || typeof (value) === 'undefined') { return callback(new Error('请选择一项')) } else { return callback() } } return { dialogFormVisible: false, // 对话框是否显示 dialogStatus: '', // 对话框类型:create,update,detail cirForm: { id: '', // id circuitNum: '', circuitCode: '', // 回路编号 circuitName: '', // 回路名称 lampboxId: '' }, // 表单 textMap: { update: '编辑回路', create: '新增回路', detail: '详情' }, // 表头显示标题 rules: { circuitName: [{ required: true, message: '回路名称不能为空', trigger: ['blur', 'change'] }], circuitCode: [{ required: true, message: '回路编号不能为空', trigger: ['blur', 'change'] }], lampboxId: [{ required: true, message: '所属灯箱不能为空', trigger: ['blur', 'change'], validator: typeValidator }], circuitNum: [{ required: true, message: '回路序号不能为空', trigger: ['blur', 'change'] }] }, // 前端校验规则 boxList: [], // 灯箱列表 isEditMode: false, btnLoading: true // 保存按钮是否不允许点击 } }, created() { this.fetchLampboxList() // 获取灯箱列表 }, methods: { // 初始化对话框 initDialog: function(dialogStatus, dialogFormVisible, row = null) { this.btnLoading = false this.dialogStatus = dialogStatus this.dialogFormVisible = dialogFormVisible if (dialogStatus === 'create') { // 如果是新增,清除验证 this.resetForm() this.isEditMode = true this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中 this.cirForm = { id: row.id, circuitNum: row.circuitNum, circuitCode: row.circuitCode, circuitName: row.circuitName, lampboxId: row.lampboxId } this.isEditMode = true } }, fetchLampboxList() { getAllLampboxList().then(response => { if (response.code === 200) { this.boxList = response.data } }) }, // 清除数据 resetForm() { this.cirForm = { circuitNum: '', circuitCode: '', circuitName: '', lampboxId: '' } this.btnLoading = false }, // 保存数据 saveData: function() { this.btnLoading = true if (this.dialogStatus === 'update') { this.updateData() } else if (this.dialogStatus === 'create') { this.createData() } }, // 新增数据 createData: function() { this.$refs['dataForm'].validate((valid) => { if (valid) { addCircuit(this.cirForm).then(response => { if (response.code === 200) { this.$confirm('新增成功,是否继续新增?', '提示', { confirmButtonText: '是', cancelButtonText: '否', type: 'info' }).then(() => { this.resetForm() this.$nextTick(() => { this.$refs['dataForm'].clearValidate() }) }).catch(() => { this.$emit('watchChild') this.dialogFormVisible = false }) } else { this.btnLoading = false } }).catch(() => { this.btnLoading = false }) } else { this.btnLoading = false } }) }, // 修改数据 updateData: function() { this.$refs['dataForm'].validate((valid) => { if (valid) { editCircuit(this.cirForm).then(response => { if (response.code === 200) { this.$message.success('修改成功') this.cancel() } else { this.btnLoading = false } }) } else { this.btnLoading = false } }) }, cancel: function() { this.dialogFormVisible = false this.$emit('watchChild') } } } </script> <style rel="stylesheet/scss" lang="scss" scoped> .el-select{ width: 100%; } .el-date-editor{ width: 100%; } .dialog-footer { margin-top: -20px; text-align: center; } </style>