zouyu
2023-09-07 275d9a3c158173842449782201b9be2c243dd58b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<template>
  <div class="main-div">
    <div class="side_div" style="overflow: scroll;">
      <el-input placeholder="输入关键字进行过滤" v-model="filterText">
      </el-input>
      <el-tree
        style="overflow-x:auto;width:400px"
        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 lang="scss">
.main-div {
  width: 99.2% !important;
  height: 94vh !important;
  margin: 5px 5px 5px 5px;
}
.side_div {
  overflow-x: auto;
  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>