Fixiaobai
2023-09-07 efcf450e8e7e375ef4ffe9f421ec0d34c5378180
src/views/laboratory/organizational/index.vue
@@ -12,6 +12,7 @@
        :filter-node-method="filterNode"
        ref="tree"
        @node-click="handleNodeClick"
            :expand-on-click-node="false"
      >
      </el-tree>
    </div>
@@ -22,10 +23,16 @@
          <el-button
            type="primary"
            size="small"
            icon="el-icon-circle-plus-outline"
            icon="el-icon-plus"
            @click="dialogVisible = true"
            >新增</el-button
          >
          <el-button size="small" icon="el-icon-delete-solid">删除</el-button>
          <el-button
            size="small"
            icon="el-icon-delete-solid"
            @click="listDeleteClick"
            >删除</el-button
          >
        </div>
      </div>
      <div class="table-main-div">
@@ -39,27 +46,83 @@
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column label="日期" width="120">
            <template slot-scope="scope">{{ scope.row.date }}</template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="address" label="地址" show-overflow-tooltip>
          <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 } from "@/api/laboratory/organizational";
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: {
@@ -67,28 +130,24 @@
        label: "department",
        id: "id",
      },
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      formData: {
        fatherId: "",
        department: "",
      },
      // 新增按钮里面禁止输入框数据
      disabledInputShow: "",
      // 保存点击节点数据
      treeNodeData: {},
      // 多选删除
      listDelete: [],
      // 表格数据
      tableData: [],
      rules: {
        department: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 1, max: 25, message: "长度在 1 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
@@ -101,6 +160,13 @@
    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;
      });
    },
    // 获取树路径
@@ -115,10 +181,14 @@
    // 点击树节点
    handleNodeClick(data, node, element) {
      this.getParentData(node.parent, node.data.department);
      console.log(`output->this.msg`, this.msg);
      console.log(`output->data`, data);
      console.log(`output->node`, node);
      console.log(`output->element`, element);
      // 由于点击第一节点无法触发getParentData里面的判断,只能额外判断
      if (node.data.id === 0) {
        this.msg = node.data.department;
      }
      // 点击节点数据存储下来
      this.treeNodeData = node.data;
      // 点击触发该函数,更新表格数据
      this.tableInitialization(node.data.id);
    },
    // 改变多选框状态
    toggleSelection(rows) {
@@ -132,7 +202,71 @@
    },
    // 点击多选框以后的操作
    handleSelectionChange(val) {
      this.multipleSelection = 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() {
@@ -141,6 +275,18 @@
  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();
        }
      },
    },
  },
};
@@ -187,4 +333,7 @@
  font-weight: 500;
  color: #999999;
}
.elFormClass .el-form-item__error {
  padding-left: 90px;
}
</style>