Newer
Older
base_front / src / views / system / dept / editDept.vue
StephanieGitHub on 11 Oct 2019 7 KB first commit
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="deptForm" label-position="right" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="组织名称" prop="simplename">
            <el-input v-model.trim="deptForm.simplename" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组织全称" prop="fullname">
            <el-input v-model.trim="deptForm.fullname" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="父组织" prop="pid">
            <!--<dept-select v-model="deptForm.pid" :dept-tree-list="deptTreeList" :dept-props="defaultProps" :multi-data="multiData" >-->
            <dept-select v-model="deptForm.pid" placeholder="请选择父级" >
            <!--<select-tree v-model="deptForm.pid" :options="deptTreeList" :props="defaultProps" />-->
          </dept-select></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="num">
            <el-input-number v-model.number="deptForm.num" style="width:100%" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="组织类型" prop="deptType">
            <el-select v-model="deptForm.deptType" placeholder="请选择">
              <el-option
                v-for="item in deptTypList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
            <!--<el-input v-model="resourceForm.resourceType" type="text" placeholder="必填"/>-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label=" 备注" prop="tips">
            <el-input v-model.trim="deptForm.tips" type="text" 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>
</template>

<script>
import { judgeTree, toTreeList } from '@/utils/structure'
import SelectTree from '@/components/SelectTree/singleSelect'
import { getDeptTreeList, addDept, updateDept } from '@/api/dept'
import DeptSelect from '@/components/DeptSelect'

export default {
  name: 'EditDept',
  components: { DeptSelect, SelectTree },
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      deptForm: {
        id: '',
        simplename: '',
        fullname: '',
        pid: '',
        pids: '',
        version: '',
        num: '',
        tips: '',
        deptType: ''
      }, // 表单
      defaultProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      },
      deptTypList: [
        {
          name: '公司/单位',
          value: '03'
        },
        {
          name: '部门/组',
          value: '04'
        }
      ],
      multiData: false, // 组织树是否为多级列表
      deptTreeList: [], // 组织树列表数据
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        simplename: [{ required: true, message: '组织机构名称不能为空', trigger: ['blur', 'change'] }],
        fullname: [{ required: true, message: '组织机构全称不能为空', trigger: ['blur', 'change'] }],
        pid: [{ required: true, message: '父组织机构必选', trigger: ['blur', 'change'] }],
        num: [{ required: true, message: '排序不能为空' }, { type: 'number', message: '必须为数字值' }],
        deptType: [{ required: true, message: '组织类型必选', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  computed: {
    deptTree: function() {
      const tree = toTreeList(this.deptTreeList)
      console.log(tree)
      return tree
    }
  },
  mounted: function() {
    // this.fetchPcode()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      // this.fetchPcode()
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.deptForm = {
          id: row.id,
          simplename: row.simplename,
          fullname: row.fullname,
          pid: row.pid,
          pids: row.pids,
          num: row.num,
          tips: row.tips,
          version: row.version,
          deptType: row.deptType
        }
      }
    },
    // 加载父组织树形下拉菜单
    fetchPcode: function() {
      getDeptTreeList(this.listQuery).then(response => {
        const list = response.data.list
        list.push({ id: '0', name: '顶级', open: true, value: '0' })
        if (list) {
          // 如果有必要转树
          if (judgeTree(list)) {
            this.multiData = true
            this.deptTreeList = toTreeList(response.data.list, '0', true)
          } else {
            this.deptTreeList = list
            this.multiData = false
          }
          console.log(this.deptTreeList)
        }
      })
    },
    // 重置表单
    resetForm() {
      this.deptForm = {
        id: '',
        simplename: '',
        fullname: '',
        pid: '',
        pids: '',
        num: '',
        tips: '',
        version: '',
        deptType: ''
      }
    },
    // 保存数据
    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.deptForm)
        if (valid) {
          addDept(this.deptForm).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
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDept(this.deptForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = 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>