audit-unit-select.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <div :class="$style.unitSelect">
  3. <a-select
  4. ref="select"
  5. v-model="onKeys"
  6. mode="multiple"
  7. :max-tag-count="2"
  8. @focus="focus"
  9. @change="selectChange"
  10. >
  11. <a-select-option v-for="(item, i) in mockData" :key="i" :value="item.key">{{
  12. item.title
  13. }}</a-select-option>
  14. </a-select>
  15. <a-modal v-model="showTree" title="请选择" @ok="ok" @cancel="cancel">
  16. <!-- 单选 -->
  17. <div :class="$style.tree">
  18. <a-input-search
  19. style="margin-bottom: 8px"
  20. placeholder="搜索"
  21. allow-clear
  22. @change="onChange"
  23. />
  24. <a-radio-group v-if="single" v-model="onGroupKey">
  25. <a-tree
  26. v-model="checkedKeys"
  27. :expanded-keys="expandedKeys"
  28. :auto-expand-parent="autoExpandParent"
  29. :selected-keys="selectedKeys"
  30. :tree-data="treeData"
  31. :load-data="onLoadData"
  32. :default-expanded-keys="['100000']"
  33. :default-expand-parent="true"
  34. multiple
  35. show-icon
  36. @expand="onExpand"
  37. @select="onSelect"
  38. @check="onCheck"
  39. >
  40. <template slot="custom" slot-scope="{ key }">
  41. <a-radio :value="key"></a-radio>
  42. </template>
  43. </a-tree>
  44. </a-radio-group>
  45. <!-- 多选 -->
  46. <a-checkbox-group v-else v-model="onCheckBoxGroupKey">
  47. <a-tree
  48. v-model="checkedKeys"
  49. :expanded-keys="expandedKeys"
  50. :auto-expand-parent="autoExpandParent"
  51. :selected-keys="selectedKeys"
  52. :tree-data="treeData"
  53. :load-data="onLoadData"
  54. :default-expanded-keys="['100000']"
  55. :default-expand-parent="true"
  56. multiple
  57. show-icon
  58. @expand="onExpand"
  59. @select="onSelect"
  60. @check="onCheck"
  61. >
  62. <template slot="custom" slot-scope="{ key }">
  63. <a-checkbox :value="key"></a-checkbox>
  64. </template>
  65. </a-tree>
  66. </a-checkbox-group>
  67. </div>
  68. </a-modal>
  69. </div>
  70. <!-- 分级数据 -->
  71. </template>
  72. <script>
  73. import components from './_import-components/audit-unit-select-import'
  74. import AuditPortraitService from './audit-portrait-service'
  75. export default {
  76. name: 'AuditUnitSelect',
  77. metaInfo: {
  78. title: 'AuditUnitSelect',
  79. },
  80. components,
  81. model: {
  82. prop: 'value',
  83. event: 'change',
  84. },
  85. props: {
  86. value: {
  87. type: Array,
  88. default: () => [],
  89. },
  90. single: {
  91. type: Boolean,
  92. default: true,
  93. },
  94. },
  95. data() {
  96. return {
  97. // 弹窗
  98. showTree: false,
  99. onKeys: [],
  100. mockData: [],
  101. checkedKeys: [],
  102. expandedKeys: ['100000'],
  103. autoExpandParent: true,
  104. selectedKeys: [],
  105. treeData: [],
  106. searchValue: '',
  107. onGroupKey: '',
  108. onCheckBoxGroupKey: [],
  109. }
  110. },
  111. created() {
  112. this.initTreeData()
  113. },
  114. methods: {
  115. selectChange(e) {
  116. this.onKeys = e
  117. const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
  118. const arr = isArr.map((val) => ({
  119. orgCode: val.key + '',
  120. name: val.title,
  121. }))
  122. this.$emit('change', arr)
  123. },
  124. ok() {
  125. this.showTree = false
  126. // 寻找对应的name
  127. this.onKeys = this.single ? [this.onGroupKey] : this.onCheckBoxGroupKey
  128. // 遍历数组看是否有 100000
  129. // 如果没有则插入
  130. const isType = this.mockData.find((val) => val.key === '100000')
  131. if (!isType) {
  132. this.mockData.push({
  133. key: '100000',
  134. title: '国家电力投资集团有限公司',
  135. lev: 1,
  136. isLeaf: false,
  137. parent: 'root',
  138. scopedSlots: { icon: 'custom' },
  139. })
  140. }
  141. const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
  142. const arr = isArr.map((val) => ({
  143. orgCode: val.key + '',
  144. name: val.title,
  145. }))
  146. this.$emit('change', arr)
  147. },
  148. focus() {
  149. this.showTree = true
  150. this.onCheckBoxGroupKey = this.onKeys
  151. this.onGroupKey = this.onKeys[0] || ''
  152. // 展开第一个
  153. this.expandedKeys = ['100000']
  154. // 去除焦点
  155. this.$refs.select.blur()
  156. },
  157. cancel() {
  158. this.showTree = false
  159. // 清空组件内数据
  160. this.checkedKeys = []
  161. this.expandedKeys = []
  162. this.autoExpandParent = true
  163. this.selectedKeys = []
  164. this.onGroupKey = ''
  165. },
  166. initTreeData() {
  167. // 100000
  168. const params = {
  169. mdgOrgCode: '100000',
  170. lev: 1,
  171. }
  172. // 国家电力投资集团有限公司
  173. AuditPortraitService.getGroupsByCode(params).then((res) => {
  174. this.treeData = [
  175. {
  176. key: '100000',
  177. title: '国家电力投资集团有限公司',
  178. lev: 1,
  179. scopedSlots: { icon: 'custom' },
  180. children: res.data.map((res) => {
  181. return {
  182. key: res.code,
  183. title: res.name,
  184. parent: '100000',
  185. isLeaf: false,
  186. lev: 2,
  187. scopedSlots: { icon: 'custom' },
  188. }
  189. }),
  190. },
  191. ]
  192. this.mockData = []
  193. this.mockData.push({
  194. key: '100000',
  195. title: '国家电力投资集团有限公司',
  196. lev: 1,
  197. isLeaf: false,
  198. parent: 'root',
  199. scopedSlots: { icon: 'custom' },
  200. })
  201. this.mockData = res.data.map((res) => {
  202. return {
  203. key: res.code + '',
  204. title: res.name,
  205. parent: '100000',
  206. isLeaf: false,
  207. lev: 2,
  208. scopedSlots: { icon: 'custom' },
  209. }
  210. })
  211. // 判断value是否有值如果有值且mock中没有该值则添加
  212. this.value.forEach((val) => {
  213. if (!this.mockData.find((item) => item.key === val.orgCode)) {
  214. this.mockData.push({
  215. key: val.orgCode + '',
  216. title: val.name,
  217. parent: '',
  218. isLeaf: false,
  219. lev: 2,
  220. scopedSlots: { icon: 'custom' },
  221. })
  222. }
  223. })
  224. this.$nextTick(() => {
  225. if (this.value.length > 0) {
  226. this.onKeys = [this.value[0].orgCode]
  227. const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
  228. const arr = isArr.map((val) => ({
  229. orgCode: val.key + '',
  230. name: val.title,
  231. }))
  232. this.$emit('change', arr)
  233. }
  234. })
  235. })
  236. },
  237. onLoadData(treeNode) {
  238. return new Promise((resolve) => {
  239. if (treeNode.dataRef.children) {
  240. return resolve()
  241. }
  242. const params = {
  243. mdgOrgCode: treeNode.dataRef.key,
  244. lev: treeNode.dataRef.lev,
  245. }
  246. AuditPortraitService.getGroupsByCode(params).then((res) => {
  247. treeNode.dataRef.children = res.data.map((res) => {
  248. return {
  249. key: res.code + '',
  250. title: res.name,
  251. isLeaf: true,
  252. lev: treeNode.dataRef.lev,
  253. parent: treeNode.dataRef.key,
  254. scopedSlots: { icon: 'custom' },
  255. }
  256. })
  257. // 遍历遍历数组如果res.data中有value的值则删除mockData中的该值
  258. this.value.forEach((val) => {
  259. if (res.data.find((item) => item.code === val.orgCode)) {
  260. this.mockData = this.mockData.filter((item) => item.key !== val.orgCode)
  261. }
  262. })
  263. this.mockData = [
  264. ...this.mockData,
  265. ...res.data.map((res) => {
  266. return {
  267. key: res.code + '',
  268. title: res.name,
  269. parent: treeNode.dataRef.key,
  270. isLeaf: true,
  271. lev: treeNode.dataRef.lev,
  272. scopedSlots: { icon: 'custom' },
  273. }
  274. }),
  275. ]
  276. resolve()
  277. })
  278. })
  279. },
  280. onExpand(expandedKeys) {
  281. this.expandedKeys = expandedKeys
  282. this.autoExpandParent = false
  283. },
  284. onSelect(selectedKeys) {
  285. console.log('🚀 ~ onCheck ~ checkedKeys:', selectedKeys)
  286. if (this.single) {
  287. this.onGroupKey = selectedKeys[0]
  288. } else {
  289. if (this.onCheckBoxGroupKey.includes(selectedKeys[0])) {
  290. this.onCheckBoxGroupKey = this.onCheckBoxGroupKey.filter(
  291. (item) => item !== selectedKeys[0]
  292. )
  293. } else {
  294. this.onCheckBoxGroupKey = [...this.onCheckBoxGroupKey, selectedKeys[0]]
  295. }
  296. }
  297. this.selectedKeys = []
  298. // this.onGroupKey = selectedKeys[0]
  299. // 寻找父节点 展开
  300. // this.expandedKeys = [
  301. // '100000',
  302. // ...this.mockData.filter((item) => item.key === selectedKeys[0]).map((item) => item.parent),
  303. // ]
  304. // 只能选择一个节点
  305. },
  306. onCheck(checkedKeys) {
  307. this.checkedKeys = checkedKeys
  308. if (checkedKeys.length > 1 && this.single) {
  309. this.checkedKeys = [this.checkedKeys[checkedKeys.length - 1]]
  310. } else {
  311. this.onCheckBoxGroupKey = checkedKeys
  312. this.checkedKeys = checkedKeys
  313. }
  314. },
  315. onChange(e) {
  316. const { value } = e.target
  317. const selectedKeys = this.mockData
  318. .map((item) => {
  319. if (item.title.indexOf(value) > -1) {
  320. return item
  321. }
  322. return null
  323. })
  324. .filter((item) => item)
  325. console.log('🚀 ~ onChange ~ selectedKeys:', selectedKeys)
  326. const dataTree = [
  327. {
  328. key: '100000',
  329. title: '国家电力投资集团有限公司',
  330. lev: 1,
  331. scopedSlots: { icon: 'custom' },
  332. children: [],
  333. },
  334. ]
  335. selectedKeys.map((val) => {
  336. if (val.lev === 2) {
  337. dataTree[0].children.push(val)
  338. }
  339. })
  340. dataTree[0].children.forEach((val2) => {
  341. selectedKeys.map((val3) => {
  342. if (val2.lev === 3 && val2.key === val3.parent) {
  343. val2.children.push(val3)
  344. }
  345. })
  346. })
  347. console.log('🚀 ~ onChange ~ dataTree:', dataTree)
  348. this.treeData = dataTree
  349. },
  350. },
  351. }
  352. </script>
  353. <style module lang="scss">
  354. @use '@/common/design' as *;
  355. .unit-select {
  356. min-width: 200px;
  357. }
  358. .tree {
  359. height: 500px;
  360. // 滚动
  361. overflow-y: auto;
  362. }
  363. </style>