Newer
Older
base_front / src / views / system / dept / listDept.vue
StephanieGitHub on 11 Oct 2019 4 KB first commit
<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-form-item class="selectForm-container-item" prop="deptName">
          <el-input v-model.trim="listQuery.deptName" placeholder="组织名称" clearable/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="hasPerm('/dept/add')" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :columns="columns" rowkey="simplename" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="操作" width="110">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/dept/update')" type="warning" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-if="hasPerm('/dept/delete')" type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑资源的对话框-->
    <edit-dept v-show="dialogFormVisible" ref="editdept" @watchChild="fetchData"/>
  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import editDept from '@/views/system/dept/editdept'
import { getDeptList, delDept } from '@/api/dept'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'ListDept',
  components: {
    treeTable,
    editDept
  },
  data() {
    return {
      listQuery: {
        deptName: ''
      },
      columns: [
        {
          text: '组织机构简称',
          value: 'simplename'
        },
        {
          text: '组织机构全称',
          value: 'fullname'
        },
        {
          text: '备注',
          value: 'tips'
        },
        {
          text: '排序',
          value: 'num',
          width: 80
        }
      ],
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列,如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/dept/update') || this.hasPerm('/dept/delete')
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$refs.editdept.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      console.log('row:' + row)
      this.$refs.editdept.initDialog(this.dialogStatus, this.dialogFormVisible, row)
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.simplename + '吗?',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delDept(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
            // deleteItem(this.list, row)// 前端删除指定项
          }
        })
      })
    },
    search() {
      this.fetchData()
    },
    fetchData() {
      console.log('fetchData')
      this.listLoading = true
      getDeptList(this.listQuery).then(response => {
        this.list = toTreeList(response.data.list, '0', false)
        this.listLoading = false
      })
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(des) {
      // deleteItem(this.list, des)
      const del = (list, item) => {
        for (const i in list) {
          if (list[i].id === des.id) {
            list.splice(i, 1)
            return
          } else {
            if (list[i].children && list[i].children.length > 0) {
              del(list[i].children, des)
            }
          }
        }
      }
      del(this.list, des)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .treetable{
    margin-bottom: 50px;
  }
</style>