Fixiaobai
2023-09-07 efcf450e8e7e375ef4ffe9f421ec0d34c5378180
src/views/laboratory/organizational/index.vue
@@ -1,11 +1,339 @@
<template>
  <div>131</div>
  <div class="main-div">
    <div class="side_div">
      <el-input placeholder="输入关键字进行过滤" v-model="filterText">
      </el-input>
      <el-tree
        class="filter-tree"
        :data="data"
        node-key="id"
        :props="defaultProps"
        default-expand-all
        :filter-node-method="filterNode"
        ref="tree"
        @node-click="handleNodeClick"
            :expand-on-click-node="false"
      >
      </el-tree>
    </div>
    <div class="table_div">
      <div class="table_top_div">
        <span class="top_span" v-if="msg !== ''">{{ msg }}</span>
        <div style="text-align: right; float: right">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="dialogVisible = true"
            >新增</el-button
          >
          <el-button
            size="small"
            icon="el-icon-delete-solid"
            @click="listDeleteClick"
            >删除</el-button
          >
        </div>
      </div>
      <div class="table-main-div">
        <el-table
          ref="multipleTable"
          :data="tableData"
          border
          height="100%"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            label="序号"
            type="index"
            width="70"
          ></el-table-column>
          <el-table-column prop="department" label="部门"> </el-table-column>
          <el-table-column label="操作">
            <template scope="scope">
              <el-button type="text" size="mini" @click="updateClick(scope)"
                >编辑</el-button
              >
              <el-button type="text" size="mini" @click="deleteClick(scope)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog
      :title="isUpdate ? '更新部门名称' : '新增部门'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="ruleForm"
        class="elFormClass"
      >
        <el-form-item label="上级部门:" prop="name" style="padding-left: 10px">
          <el-input
            v-model="treeNodeData.department"
            :disabled="true"
            style="width: 81.3%"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="部门名称:"
          prop="department"
          style="padding-top: 20px"
        >
          <el-input
            placeholder="请输入部门名称"
            v-model="formData.department"
            style="width: 80%"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveForm">{{
          isUpdate ? "更 新" : "新 增"
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getOrganizationalApi,
  getTableInitializationApi,
  organizationalAddApi,
  organizationalUpdateApi,
  organizationalDeleteApi,
} from "@/api/laboratory/organizational";
export default {
  name: "Organizational",
  data() {
    return {
      filterText: "",
      dialogVisible: false,
      msg: "",
      isUpdate: false,
      data: [],
      // tree树默认值配置
      defaultProps: {
        children: "children",
        label: "department",
        id: "id",
      },
      formData: {
        fatherId: "",
        department: "",
      },
      // 新增按钮里面禁止输入框数据
      disabledInputShow: "",
      // 保存点击节点数据
      treeNodeData: {},
      // 多选删除
      listDelete: [],
      // 表格数据
      tableData: [],
      rules: {
        department: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 1, max: 25, message: "长度在 1 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 树上方搜索框过滤器
    filterNode(value, data) {
      if (!value) return true;
      return data.department.indexOf(value) !== -1;
    },
    // 初始化获取树数据
    treeInitialization() {
      getOrganizationalApi().then((res) => {
        this.data = res.data;
        // 初始化默认msg提示路径为第一节点名称
        this.msg = res.data[0].department;
        // 初始化调用表格接口
        this.tableInitialization(res.data[0].id);
        // 初始化保存第一节点数据
        this.treeNodeData.department = res.data[0].department;
        this.treeNodeData.id = res.data[0].id;
      });
    },
    // 获取树路径
    getParentData(node, department) {
      if (node !== null) {
        if (node.data.department !== undefined) {
          this.msg = node.data.department + " > " + department;
        }
        this.getParentData(node.parent, this.msg);
      }
    },
    // 点击树节点
    handleNodeClick(data, node, element) {
      this.getParentData(node.parent, node.data.department);
      // 由于点击第一节点无法触发getParentData里面的判断,只能额外判断
      if (node.data.id === 0) {
        this.msg = node.data.department;
      }
      // 点击节点数据存储下来
      this.treeNodeData = node.data;
      // 点击触发该函数,更新表格数据
      this.tableInitialization(node.data.id);
    },
    // 改变多选框状态
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    // 点击多选框以后的操作
    handleSelectionChange(val) {
      this.listDelete = []
      val.forEach((v) => {
        this.listDelete.push(v.id);
        });
    },
    // 初始化表格数据
    tableInitialization(departmentId) {
      getTableInitializationApi(departmentId).then((res) => {
        this.tableData = res.data;
      });
    },
    // 更新与新增表单
    saveForm() {
      this.formData.fatherId = this.treeNodeData.id;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (!this.isUpdate) {
            organizationalAddApi(this.formData).then((res) => {
              this.$message({
                message: res.message,
                type: "success",
              });
              this.treeInitialization();
              this.dialogVisible = false;
            });
          } else {
            organizationalUpdateApi(this.formData).then((res) => {
              this.$message({
                message: res.message,
                type: "success",
              });
              this.treeInitialization();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
    // 点击编辑触发
    updateClick(scope) {
      this.dialogVisible = true;
      this.isUpdate = true;
      this.formData.id = scope.row.id;
      this.formData.fatherId = this.treeNodeData.id;
      this.formData.department = scope.row.department;
    },
    // 表格中的删除按钮
    deleteClick(scope) {
      organizationalDeleteApi(scope.row.id).then((res) => {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.treeInitialization();
      });
    },
    // 头部多选删除
    listDeleteClick() {
      organizationalDeleteApi(this.listDelete).then((res) => {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.treeInitialization();
      });
    },
  },
  mounted() {
    this.treeInitialization();
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    dialogVisible: {
      handler(newVal, oldVal) {
        if (newVal == false) {
          this.isUpdate = false;
          this.formData = {
            fatherId: "",
            department: "",
          };
          this.$refs.ruleForm.resetFields();
        }
      },
    },
  },
};
</script>
<style></style>
<style lang="scss">
.main-div {
  width: 99.2% !important;
  height: 94vh !important;
  margin: 5px 5px 5px 5px;
}
.side_div {
  float: left;
  width: 300px;
  height: 95%;
  margin-bottom: 5px;
  background-color: #ffffff;
  padding: 15px;
}
.table_div {
  margin-left: 6px;
  float: left;
  width: calc(100% - 306px);
  height: 95%;
  background-color: #ffffff;
}
.filter-tree {
  margin-top: 6px;
}
.table_top_div {
  height: 80px;
  width: 100%;
  padding-top: 23px;
  padding-right: 50px;
}
.table-main-div {
  width: 100%;
  height: 90.3%;
  padding: 10px;
}
.top_span {
  margin-left: 12px;
  font-size: 16px;
  font-weight: 500;
  color: #999999;
}
.elFormClass .el-form-item__error {
  padding-left: 90px;
}
</style>