按钮级别的新版本,多级别的组织架构
loulijun2021
2023-08-24 68338b19f9dc2ad9725326b7e699a3e191030cb8
src/views/basicSettings/organizationList.vue
@@ -1,298 +1,294 @@
<template>
  <div>
    <div class="body" :style="{height:mainHeight+'px'}">
      <div
        class="elTableDiv"
      >
        <div style="width: 300px;background:#fff">
          <div style="margin: 20px 10px 0 10px;display: flex;justify-content: space-between;">
            <div style="display: flex;">
              <div
                style="width: 5px;height: 100%;border-radius: 5px;"
                :style="{background:$store.state.settings.theme}"
              />
              <div style="margin-left: 8px;">组织名称</div>
            </div>
            <!--            <div style="margin-right:10px">-->
            <!--              <i class="el-icon-plus" style="cursor: pointer;color: #999" />-->
            <!--            </div>-->
            <!--            @click="rolePlusClick"-->
          </div>
          <el-tree
            ref="treeLeftRef"
            style="padding: 10px;overflow: auto"
            :style="{height:(tableHeight-20)+'px'}"
            :data="treeLeft"
            node-key="code"
            highlight-current
            :props="defaultPropsLeft"
            :default-expand-all="true"
            :expand-on-click-node="false"
            draggable
            :allow-drop="treeLeftAllowDrop"
          >
            <!--            @node-click="treeLeftNodeClick"-->
            <span slot-scope="{ node, data }" class="custom-tree-node">
              <span v-if="!data.isEdit">{{ data.name }}</span>
              <input
                v-if="data.isEdit"
                :ref="data.code"
                v-model="data.name"
                type="text"
                @click.stop
                @blur="treeSave(data)"
                @keyup.enter="treeSave(data)"
              >
              <span v-if="!data.isEdit">
                <i
                  class="el-icon-plus"
                  style="margin-right:10px;color: #999"
                  @click.stop="val=>treeAddClick(val,node,data)"
                />
                <i
                  v-if="data.code!=='000'"
                  class="el-icon-edit"
                  style="margin-right:10px;color: #999"
                  @click.stop="val=>treeEditClick(val,node,data)"
                />
                <i
                  v-if="data.code!=='000'"
                  class="el-icon-delete"
                  style="margin-right: 4px;color: #999"
                  @click.stop="val=>treeDeleteClick(val,node,data)"
                />
              </span>
            </span>
          </el-tree>
        </div>
        <div
          style="margin-left: 10px;width:100%;position: relative"
        >
          <el-table
            ref="tableDataRef"
            class="tableFixed"
            :data="tableData"
            :height="(tableHeight-40)+'px'"
            border
            row-class-name="custom-row"
            :style="{width: 100+'%',height:(tableHeight-40)+'px',marginBottom:'10px'}"
            highlight-current-row
            :header-cell-style="this.$headerCellStyle"
            :cell-style="this.$cellStyle"
            @sort-change="sortChange"
          >
            <!--            prop="RowNum"-->
            <el-table-column
              type="index"
              width="50"
              fixed
              label="序号"
            />
            <el-table-column
              prop="code"
              label="用户编码"
              sortable="custom"
            />
            <!--            <el-table-column-->
            <!--              label="操作"-->
            <!--              width="120"-->
            <!--              fixed="right"-->
            <!--            >-->
            <!--              <template slot-scope="{row}">-->
            <!--                <div class="operationClass">-->
            <!--                  <el-tooltip class="item" effect="dark" content="编辑" placement="top">-->
            <!--                    <i class="el-icon-edit-outline" @click="edit('edit',row)" />-->
            <!--                  </el-tooltip>-->
            <!--                  <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">-->
            <!--                    <i class="el-icon-delete" @click="del(row)" />-->
            <!--                  </el-tooltip>-->
            <!--                </div>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
          </el-table>
          <!--分页-->
          <pagination
            :total="total"
            :page.sync="form.page"
            :limit.sync="form.rows"
            align="right"
            layout="total,prev, pager, next,sizes,jumper"
            popper-class="select_bottom"
            @pagination="getOrganizationSearch"
          />
        </div>
      <div class="bodyTopButtonGroup" style="justify-content: space-between">
        <el-button v-waves type="primary" icon="el-icon-circle-plus-outline" @click="add('add')">新增</el-button>
      </div>
      <div class="bodyTopFormGroup">
        <el-form
          ref="form"
          :model="form"
          label-width="100px"
          inline
          style="display: flex;"
        >
          <div class="elForm">
            <el-form-item label="组织编码" style=" display: flex;">
              <el-input v-model="form.storg_code" placeholder="请输入" style="width: 200px" />
            </el-form-item>
            <el-form-item label="组织名称" style=" display: flex;">
              <el-input v-model="form.storg_name" placeholder="请输入" style="width: 200px" />
            </el-form-item>
          </div>
          <div
            class="bodySearchReset"
            :style="{marginLeft:$store.state.app.sidebar.opened? $store.state.settings.menuIsHorizontal?'15%':'3%':'10%'}"
          >
            <el-button v-waves type="primary" icon="el-icon-search" @click="getSTorgData">查询</el-button>
            <el-button v-waves type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
          </div>
        </el-form>
        <div
          class="bodyTopFormExpand"
          style="height:5px"
        >
          <!--          <svg-icon-->
          <!--            v-show="mouseHoverType==='mouseout'"-->
          <!--            style="cursor: pointer"-->
          <!--            :icon-class="!isExpandForm?'doubleDown3':'doubleUp3'"-->
          <!--            @mouseenter="mouseHoverType=$event.type"-->
          <!--          />-->
          <!--          <svg-icon-->
          <!--            v-show="mouseHoverType==='mouseenter'"-->
          <!--            style="cursor: pointer"-->
          <!--            :icon-class="!isExpandForm?'doubleDown':'doubleUp'"-->
          <!--            @click="isExpandForm=!isExpandForm"-->
          <!--            @mouseout="mouseHoverType=$event.type"-->
          <!--          />-->
        </div>
      </div>
      <div class="elTableDiv">
        <el-table
          ref="tableDataRef"
          class="tableFixed"
          :data="tableData"
          :height="tableHeight+'px'"
          border
          row-class-name="custom-row"
          :style="{width: 100+'%',height:tableHeight+'px',}"
          highlight-current-row
          row-key="torg_code"
          default-expand-all
          :header-cell-style="this.$headerCellStyle"
          :cell-style="this.$cellStyle"
          @sort-change="sortChange"
        >
          <!--          <el-table-column-->
          <!--            prop="rowNum"-->
          <!--            width="120"-->
          <!--            fixed-->
          <!--            label="序号"-->
          <!--          />-->
          <el-table-column
            prop="torg_code"
            label="组织编码"
            sortable="custom"
          />
          <el-table-column
            prop="torg_name"
            label="组织名称"
            sortable="custom"
          />
          <el-table-column
            prop="status"
            label="状态"
            sortable="custom"
          >
            <template slot-scope="{row}">
              <el-tag v-if="row.status==='Y'" size="small" type="success">正常</el-tag>
              <el-tag v-if="row.status==='N'" size="small" type="danger">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="username"
            label="创建人员"
            sortable="custom"
          />
          <el-table-column
            prop="lm_date"
            label="创建时间"
            width="160"
            sortable="custom"
          />
          <el-table-column
            label="操作"
            width="120"
            fixed="right"
          >
            <template slot-scope="{row}">
              <div class="operationClass">
                <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                  <i
                    v-if="row.leve!==0"
                    class="el-icon-edit-outline"
                    :style="{color:$store.state.settings.theme}"
                    @click="edit('edit',row)"
                  />
                </el-tooltip>
                <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">
                  <i
                    v-if="row.leve!==0"
                    class="el-icon-delete"
                    :style="{color:$store.state.settings.theme}"
                    @click="del(row)"
                  />
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <pagination
        :total="total"
        :page.sync="form.page"
        :limit.sync="form.rows"
        align="right"
        layout="total,prev, pager, next,sizes,jumper"
        popper-class="select_bottom"
        @pagination="getSTorgData"
      />
    </div>
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClose"
      @close="handleClose"
    >
      <el-form ref="dialogForm" inline :rules="dialogFormRules" :model="dialogForm" label-width="80px">
        <el-form-item label="组织编码" prop="OrganCode">
          <el-input v-model="dialogForm.OrganCode" :disabled="operation!=='add'" style="width: 200px" />
        </el-form-item>
        <el-form-item label="组织名称" prop="OrganName">
          <el-input v-model="dialogForm.OrganName" style="width: 200px" />
        </el-form-item>
        <el-form-item label="所属组织" prop="storgcode">
          <el-cascader
            ref="cascader"
            v-model="dialogForm.storgcode"
            :options="cascaderOptions"
            filterable
            :props="defaultProps"
            :show-all-levels="false"
            @change="cascaderChange"
          />
        </el-form-item>
        <el-form-item required label="组织状态">
          <el-radio-group v-model="dialogForm.status">
            <el-radio label="Y">正常</el-radio>
            <el-radio label="N">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleCancel">取 消</el-button>
          <el-button
            v-waves
            type="primary"
            :loading="$store.state.app.buttonIsDisabled"
            :disabled="$store.state.app.buttonIsDisabled"
            @click="dialogVisibleConfirm"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import {
  AddUpdateOrganization,
  DeleteOrganization,
  STorgData
} from '@/api/basicSettings'
import { validateCode } from '@/utils/global'
import elDragDialog from '@/directive/el-drag-dialog'
import waves from '@/directive/waves'
import arrayToTree from 'array-to-tree'
import { throttle } from '@/utils/debounceAndThrottle'
import Pagination from '@/components/Pagination'
import { PrentOrganization } from '@/api/GeneralBasicData'
export default {
  name: 'RoleList',
  directives: { waves },
  name: 'Zzjg',
  components: {
    Pagination
  },
  directives: { elDragDialog, waves },
  data() {
    return {
      mainHeight: 0,
      tableHeight: 0,
      treeLeft: [
        {
          code: '000',
          name: '大岛电器',
          isEdit: false,
          children: [
            {
              code: '001',
              name: '研发部',
              isEdit: false,
              children: []
            }, {
              code: '002',
              name: '销售部',
              isEdit: false,
              children: []
            }, {
              code: '003',
              name: '生产部',
              isEdit: false,
              children: [
                {
                  code: '004',
                  name: '金工车间一',
                  isEdit: false,
                  children: []
                }, {
                  code: '005',
                  name: '金工车间二',
                  isEdit: false,
                  children: []
                }
              ]
            }
          ]
        }
      ], // 左侧树
      defaultPropsLeft: {
        children: 'children',
        label: 'name'
      },
      tableData: [
        { code: '001' },
        { code: '002' },
        { code: '003' }
      ],
      form: {
        page: 1,
        rows: 20
        storg_code: '', // 组织架构编码
        storg_name: '', // 组织架构名称
        prop: 'lm_date', // 排序字段
        order: 'desc', // 排序字段
        page: 1, // 第几页
        rows: 20 // 每页多少条
      },
      total: 0
      total: 10,
      tableData: [],
      dialogVisible: false,
      dialogForm: {
        OrganCode: '', // 组织编码
        OrganName: '', // 组织名称
        leve: 0, // 层级编码
        storgcode: '', // 父级编码
        parent_id: '', // 原pid
        torg_seq: '', // 排序
        status: 'Y'// 状态
      },
      operation: '',
      dialogFormRules: {
        OrganCode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        OrganName: [
          { required: true, message: '请输入组织名称', trigger: ['blur', 'change'] }
        ],
        storgcode: [
          { required: true, message: '请选择所属组织', trigger: ['blur', 'change'] }
        ]
      },
      defaultProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'torg_code',
        label: 'torg_name'
      },
      Options: [],
      cascaderOptions: []
    }
  },
  watch: {
    // treeLeft: {
    //   handler(newValue, oldValue) {
    //     newValue[0].roles.forEach(i => {
    //       if (i.isEdit) {
    //         this.$nextTick(() => {
    //           this.$refs[i.code].focus()
    //         })
    //       }
    //     })
    //   },
    //   deep: true
    // }
  },
  created() {
    this.getSTorgData()
  },
  mounted() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
  },
  methods: {
    // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 75
        this.tableHeight = this.mainHeight - 50
    // 组织架构大列表查询
    async getSTorgData() {
      const res = await STorgData(this.form)
      this.tableData = arrayToTree(res.data, {
        parentProperty: 'parent_id',
        customID: 'torg_code',
        childrenProperty: 'children'
      })
      this.total = res.count
    },
    // 组织架构级联选择器
    async getPrentOrganization() {
      const { data: res } = await PrentOrganization()
      this.Options = res
      this.cascaderOptions = arrayToTree(res, {
        parentProperty: 'parent_id',
        customID: 'torg_code',
        childrenProperty: 'children'
      })
    },
    // 左侧树形拖动处理
    treeLeftAllowDrop(draggingNode, dropNode, type) {
      // console.log(draggingNode, dropNode, type)
      // 注掉的是同级拖拽
      // if (draggingNode.level === dropNode.level) {
      //   return type === 'prev' || type === 'next'
      // } else {
      //   // 不同级进行处理
      //   return false
      // }
    },
    getTreeAttr(val) {
      console.log(val, 1)
      if (val['children']) {
        val['children'].push({
          code: '',
          name: '',
          isEdit: true,
          children: []
        })
      } else {
        val.children.push(
          { code: '', name: '', isEdit: true }
        )
      }
      this.$nextTick(() => {
        this.$refs[''].focus()
      })
    },
    // 角色名称保存   采用防抖形式
    treeSave: throttle(function(val) {
      val.isEdit = false
      console.log('执行了!')
    }, 1000, true),
    treeEditClick(val, node, data) {
      data.isEdit = true
      this.$nextTick(() => {
        this.$refs[data.code].focus()
      })
    },
    treeDeleteClick(val, node, data) {
    },
    treeAddClick(val, node, data) {
      // console.log(val, node, data, 2)
      // this.treeLeft = data.children.push({ code: '', name: '', isEdit: true })
      this.getTreeAttr(data)
      // console.log(this.treeLeft, 3)
    },
    // 排序改变时
    sortChange({ column, prop, order }) {
      if (order === 'descending') {
        order = 'desc'
@@ -303,30 +299,142 @@
      }
      this.form.order = order
      this.form.prop = prop
      // this.getOrganizationSearch()
      this.getSTorgData()
    },
    getOrganizationSearch() {
    // 重置
    reset() {
      this.form.storg_code = ''
      this.form.storg_name = ''
      this.getSTorgData()
    },
    // 新增按钮
    add(operation) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Add'
      this.getPrentOrganization()
    },
    // 修改按钮
    async edit(operation, row) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Update'
      await this.getPrentOrganization()
      this.cascaderOptions = this.filterChildren(this.cascaderOptions, row.torg_code)
      this.$nextTick(() => {
        this.dialogForm.OrganCode = row.torg_code
        this.dialogForm.OrganName = row.torg_name
        this.dialogForm.leve = parseFloat(row.leve)
        this.dialogForm.torg_seq = row.torg_seq
        this.dialogForm.status = row.status
        this.dialogForm.storgcode = this.findParent([], row.parent_id, this.cascaderOptions).reverse()
        this.dialogForm.parent_id = row.parent_id
      })
    },
    filterChildren(treeData, code) {
      treeData.forEach(item => {
        if (code.split(',').includes(item.torg_code)) {
          item.disabled = true
          if (item.children && item.children.length > 0) {
            this.filterChildren(item.children, item.children.map(it => it.torg_code).join(','))
          }
        } else {
          if (item.children && item.children.length > 0) {
            this.filterChildren(item.children, code)
          }
        }
      })
      return treeData
    },
    // parents:用于返回的数组,childNode:要查询的节点,treeData:json树形数据
    findParent(parents, childNode, treeData) {
      // console.log(parents, childNode, treeData, 2)
      for (let i = 0; i < treeData.length; i++) {
        // 父节点查询条件
        if (treeData[i].torg_code === childNode) {
          // 如果找到结果,保存当前节点
          parents.push(treeData[i].torg_code)
          // 用当前节点再去原数据查找当前节点的父节点
          this.findParent(parents, treeData[i].parent_id, this.cascaderOptions)
          break
        } else {
          if (treeData[i].children instanceof Array) {
            //   没找到,遍历该节点的子节点
            this.findParent(parents, childNode, treeData[i].children)
          }
        }
      }
      return parents
    },
    // 删除按钮
    async del(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteOrganization({ orgcode: row.torg_code }).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功!')
            if (this.form.page > 1 && this.tableData.length === 1) {
              this.form.page--
            }
            this.getSTorgData()
          }
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    // 对话框关闭事件
    handleClose() {
      this.dialogForm.OrganCode = ''
      this.dialogForm.OrganName = ''
      this.dialogForm.storgcode = ''
      this.dialogForm.parent_id = ''
      this.dialogForm.status = 'Y'
      this.dialogForm.leve = 0
      this.$refs.cascader.checkedValue = ''
      this.$refs.dialogForm.clearValidate()
    },
    cascaderChange(val) {
      const temp = this.Options.find(i => i.torg_code === val[val.length - 1])
      this.dialogForm.storgcode = val
      this.dialogForm.leve = parseFloat(temp.leve) + 1
      this.dialogForm.torg_seq = this.Options.filter(i => i.parent_id === val[val.length - 1]).length + 1
    },
    // 对话框取消
    dialogVisibleCancel() {
      this.dialogVisible = false
    },
    // 对话框确认
    dialogVisibleConfirm() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          this.$store.state.app.buttonIsDisabled = true
          this.dialogForm.storgcode = this.dialogForm.storgcode[this.dialogForm.storgcode.length - 1]
          AddUpdateOrganization(this.dialogForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogVisible = false
              this.$store.state.app.buttonIsDisabled = false
              this.getSTorgData()
            } else {
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
              this.$store.state.app.buttonIsDisabled = false
            }
          })
        }
      })
    },
    // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 85
        this.tableHeight = this.mainHeight - 200
        this.$refs.tableDataRef.doLayout()
      })
    }
  }
}
</script>
<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
//::v-deep .el-checkbox__label {
//  width: 55px;
//}
//
.el-icon-plus:hover, .el-icon-edit:hover, .el-icon-delete:hover {
  color: #000 !important;
}
</style>