index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div class="tree-choice">
  3. <el-input placeholder="输入名称搜索" v-model="filterText" style="margin-bottom: 10px"></el-input>
  4. <el-tree
  5. :data="dataList"
  6. node-key="id"
  7. default-expand-all
  8. :highlight-current="true"
  9. :expand-on-click-node="false"
  10. :filter-node-method="filterNode"
  11. ref="tree"
  12. @node-click="nodeClick"
  13. show-checkbox
  14. :default-checked-keys="checkeds"
  15. :props="{ disabled: () => true }"
  16. >
  17. <span class="custom-tree-node" slot-scope="{ data }">
  18. <span>{{ data.label }}</span>
  19. </span>
  20. </el-tree>
  21. </div>
  22. </template>
  23. <script>
  24. import { treeselect } from '@/api/system/dept.js'
  25. export default {
  26. model: {
  27. prop: 'modelValue',
  28. event: 'dataChange'
  29. },
  30. props: {
  31. modelValue: {
  32. type: Array,
  33. required: true
  34. }
  35. },
  36. data() {
  37. return {
  38. //搜索字段
  39. filterText: '',
  40. //拥有权限的id数组
  41. checkeds: [],
  42. //部门数据
  43. dataList: [],
  44. //选中的部门信息
  45. deptInfo: {}
  46. }
  47. },
  48. computed: {
  49. deptIds: {
  50. get() {
  51. return this.modelValue
  52. },
  53. set(val) {
  54. this.$emit('dataChange', val)
  55. this.$emit('click')
  56. }
  57. }
  58. },
  59. watch: {
  60. filterText(val) {
  61. this.$refs.tree.filter(val)
  62. }
  63. },
  64. created() {
  65. this.getDeptList()
  66. },
  67. methods: {
  68. //获取部门数据
  69. getDeptList() {
  70. treeselect().then((res) => {
  71. if (res.code === 200) {
  72. this.dataList = res.data
  73. this.checkeds = this.getDeptIds()
  74. }
  75. })
  76. },
  77. //搜索过滤节点
  78. filterNode(value, data) {
  79. if (!value) return true
  80. return data.label.indexOf(value) !== -1
  81. },
  82. //获取拥有的数据权限
  83. getDeptIds() {
  84. let ids = this.$store.state.user.dataPermissions
  85. if (ids.includes('*')) {
  86. return this.getAllIds(this.dataList)
  87. } else {
  88. return ids
  89. }
  90. },
  91. //节点点击
  92. nodeClick(data, node) {
  93. let ids = data.children && data.children.length ? this.getAllIds(data.children) : []
  94. ids.push(data.id)
  95. ids = ids.filter((item) => this.checkeds.includes(item))
  96. if (!ids.length) {
  97. return
  98. }
  99. this.deptIds = ids
  100. let deptNames = data.label
  101. let parent = node.parent
  102. while (parent.data.constructor === Object) {
  103. deptNames = parent.data.label + '\\' + deptNames
  104. parent = parent.parent
  105. }
  106. this.$emit('getDeptInfo', {
  107. deptId: data.id,
  108. deptName: data.label,
  109. deptNames
  110. })
  111. },
  112. //递归获取所有部门id
  113. getAllIds(parents) {
  114. if (!parents) return []
  115. let arrs = []
  116. parents.forEach((item) => {
  117. arrs.push(item.id)
  118. if (item.children && item.children.length) {
  119. arrs = [...arrs, ...this.getAllIds(item.children)]
  120. }
  121. })
  122. return arrs
  123. }
  124. }
  125. }
  126. </script>
  127. <style scoped lang="scss">
  128. .tree-choice {
  129. background-color: #fff;
  130. padding: 10px 10px;
  131. width: 280px;
  132. margin-right: 7px;
  133. flex-shrink: 0;
  134. height: 100%;
  135. overflow-y: auto;
  136. }
  137. </style>