Newer
Older
toilet / src / views / rule / components / editStation.vue
yangqianqian on 8 Mar 2021 4 KB 自测修改
<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" append-to-body @close="cancel">
      <el-form ref="dataForm" :rules="rules" :model="stationForm" label-well-code="right" label-width="100px">
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="场站名称" prop="stationName">
              <el-input v-model.trim="stationForm.stationName" type="text" placeholder="必填"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="所属机构" prop="deptId">
              <dept-select v-model="stationForm.deptId" :dept-show="true" :clearable="false" placeholder="请选择" />
            </el-form-item>
          </el-col>
        </el-row>
      </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>
  </div>
</template>

<script>
import { addStation, editStation, delStation } from '@/api/station'
import DeptSelect from '@/components/DeptSelect'

export default {
  name: 'EditStation',
  components: { DeptSelect },
  data() {
    return {
      listLoading: true,
      dialogFormVisible: false, // 对话框是否显示
      stationForm: {
        stationId: '',
        stationName: '',
        deptId: ''
      }, // 表单
      title: '', // 表头显示标题
      rules: {
        stationName: [{ required: true, message: '场站名称不能为空', trigger: ['change', 'blur'] }],
        deptId: [{ required: true, message: '所属机构必选', trigger: ['change'] }]
      } // 前端校验规则
    }
  },
  mounted() {
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogFormVisible, stationInfo = null) {
      this.dialogFormVisible = dialogFormVisible
      this.resetForm()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      this.title = '编辑场站'
      if(stationInfo === null){
        this.title = '新增场站'
      } else {
        this.stationForm.stationId = stationInfo.stationId
        this.stationForm.stationName = stationInfo.stationName
        this.stationForm.deptId = stationInfo.deptId
      }
    },
    // 重置表单
    resetForm() {
      this.stationForm = {
        id: '',
        stationName: '',
        deptId: ''
      }
    },

    // 保存数据
    saveData() {
      // 表单校验
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if(this.title === '编辑场站'){
            editStation(this.stationForm).then(response => {
              if (response.code === 200) {
                this.$message.success('修改成功')
                this.$emit('watchChild')
                this.dialogFormVisible = false
              }
            })
          } else if(this.title === '新增场站'){
            addStation(this.stationForm).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
                })
              }
            })
          }
        }
      })
    },
    cancel() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
  .dialog-footer{
    text-align: right;
    margin-top: 10px;
  }
</style>