audit-find-cat-tree.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <a-card
  3. :class="[
  4. $style.treewrap,
  5. $style.ywlxtree,
  6. { [$style.collapse]: fold, [$style.single]: single },
  7. ]"
  8. >
  9. <!-- 选择部门 -->
  10. <!-- <a-tree-select
  11. v-if="isSelectDep"
  12. v-model="depvalue"
  13. :allow-clear="true"
  14. :tree-data="depOptions"
  15. @change="changedep"
  16. >
  17. </a-tree-select> -->
  18. <!-- 搜索框 -->
  19. <!-- <a-input-search
  20. v-if="isSearch"
  21. v-model="searchStr"
  22. placeholder=""
  23. allow-clear
  24. @change="onSearchChange"
  25. /> -->
  26. <a-spin :spinning="spinning" :class="$style.spin" />
  27. <a-empty v-if="empty" />
  28. <a-tree
  29. v-else
  30. :key="refreshKey"
  31. ref="auditTree"
  32. v-model="checkedKeys"
  33. :block-node="false"
  34. :show-icon="true"
  35. :show-line="showLine"
  36. :check-strictly="true"
  37. :checkable="checkable"
  38. :draggable="draggable"
  39. :tree-data="treeData"
  40. :replace-fields="replaceFields"
  41. :selected-keys="defaultSelectedKeys"
  42. :expanded-keys="expandedKeys"
  43. :default-expand-all="defaultExpandAll"
  44. :default-expanded-keys="defaultTreeExpandedKeys"
  45. :load-data="onLoadData"
  46. @select="treeSelect"
  47. @check="treeCheck"
  48. @expand="onExpand"
  49. @dragenter="onDragEnter"
  50. @drop="onDrop"
  51. >
  52. <template v-slot:title="{ text, props }">
  53. <span v-if="text.indexOf(searchValue) > -1">
  54. {{ text.substr(0, text.indexOf(searchValue))
  55. }}<span :class="$style.active">{{ searchValue }}</span
  56. >{{ text.substr(text.indexOf(searchValue) + searchValue.length) }}</span
  57. ><span v-else>{{ text }}</span>
  58. <!-- 后台返回数量,如果有数量的话就显示 -->
  59. <span v-if="props.count > 0">({{ props.count }})</span>
  60. </template>
  61. </a-tree>
  62. <!-- <a-button v-if="showPrimary" type="primary" :class="$style.fold" @click="foldClick">
  63. <a-icon :type="icontype" />
  64. </a-button> -->
  65. </a-card>
  66. </template>
  67. <script>
  68. import AuditFindCatTreeMixins from './audit-find-cat-tree-mixins'
  69. import auditFindCatTreeService from './audit-find-cat-tree-service'
  70. import components from './_import-components/audit-find-cat-tree-import'
  71. export default {
  72. name: 'AuditFindCatTree',
  73. metaInfo: {
  74. title: 'AuditFindCatTree',
  75. },
  76. components,
  77. mixins: [AuditFindCatTreeMixins],
  78. props: {
  79. // 默认选中节点
  80. // selectedKeys: {
  81. // type: Array,
  82. // default: function() {
  83. // return []
  84. // },
  85. // },
  86. // 是否开启搜索
  87. isSearch: {
  88. type: Boolean,
  89. default: false,
  90. },
  91. // 是否显示复选框
  92. checkable: {
  93. type: Boolean,
  94. default: false,
  95. },
  96. // 是否可以拖拽
  97. draggable: {
  98. type: Boolean,
  99. default: false,
  100. },
  101. // 是否显示连线
  102. showLine: {
  103. type: Boolean,
  104. default: false,
  105. },
  106. // 根节点名称
  107. topNodeText: {
  108. type: String,
  109. default: 'XX科技1',
  110. },
  111. // 地址树接口数据源
  112. treeparams: {
  113. type: Object,
  114. default: () => {
  115. return {}
  116. },
  117. },
  118. // 默认展开节点id
  119. defaultExpandedKeys: {
  120. type: Array,
  121. default: () => {
  122. return ['0']
  123. },
  124. },
  125. // 是否显示部门选择下拉框
  126. isSelectDep: {
  127. type: Boolean,
  128. default: false,
  129. },
  130. // 部门下拉框列表值
  131. depOptions: {
  132. type: Array,
  133. default: () => {
  134. return []
  135. },
  136. },
  137. // 部门下拉框默认值
  138. defaultDepValue: {
  139. type: Array,
  140. default: () => {
  141. return []
  142. },
  143. },
  144. // 是否显示隐藏按钮
  145. showPrimary: {
  146. type: Boolean,
  147. default: true,
  148. },
  149. // 地址树是否单选
  150. single: {
  151. type: Boolean,
  152. default: false,
  153. },
  154. refreshKey: {
  155. type: Number,
  156. default: 0,
  157. },
  158. value: {
  159. type: Object,
  160. default: null,
  161. },
  162. defaultExpandAll: {
  163. type: Boolean,
  164. default: false,
  165. },
  166. },
  167. data() {
  168. return {
  169. defaultTreeExpandedKeys: ['1'],
  170. defaultSelectedKeys: ['1'],
  171. selectedKeys: ['1'],
  172. depvalue: [], // 选择的组织数据
  173. checkedKeys: [], // 选中的节点数据
  174. treeData: [],
  175. dataList: [], // 数组dataList,搜索要用
  176. spinning: true,
  177. empty: false,
  178. icontype: 'left',
  179. fold: false,
  180. replaceFields: {
  181. title: 'text',
  182. key: 'id',
  183. },
  184. expandedKeys: ['1'],
  185. backupsExpandedKeys: [],
  186. autoExpandParent: false,
  187. searchValue: '',
  188. searchStr: '',
  189. defaultTopNodeText: 'XX科技',
  190. defaultTopNodeId: '1',
  191. key: 1,
  192. selectedData: [],
  193. }
  194. },
  195. created() {
  196. if (this.value !== null) {
  197. if (this.value.checked && this.value.checked.length > 0) {
  198. this.checkedKeys = this.value
  199. }
  200. // 已选中的树节点数据赋值 add zhangtt 2023-2-23
  201. if (this.value.selectedData && this.value.selectedData.length > 0) {
  202. this.selectedData = this.value.selectedData
  203. }
  204. }
  205. },
  206. methods: {
  207. getSelectedNode() {
  208. return this.selectedData
  209. },
  210. treeSelect(keys, evt) {
  211. debugger
  212. const vNode = evt.node.dataRef
  213. if (this.checkable) {
  214. const index = this.checkedKeys.checked.indexOf(keys[0])
  215. if (index === -1) {
  216. this.checkedKeys.checked.push(keys[0])
  217. this.selectedData.push({
  218. id: vNode.id,
  219. text: vNode.text,
  220. isLeaf: vNode.isLeaf,
  221. })
  222. } else {
  223. this.checkedKeys.checked.splice(index, 1)
  224. this.selectedData.splice(index, 1)
  225. }
  226. } else {
  227. this.defaultSelectedKeys = keys
  228. this.selectedData = [
  229. {
  230. id: vNode.id,
  231. text: vNode.text,
  232. isLeaf: vNode.isLeaf,
  233. },
  234. ]
  235. }
  236. this.$emit('treeSelect', this.defaultSelectedKeys, evt)
  237. },
  238. treeCheck(keys, evt) {
  239. this.selectedData = evt.checkedNodes.map((item) => {
  240. const vNode = item.data.props.dataRef
  241. return {
  242. id: vNode.id,
  243. text: vNode.text,
  244. isLeaf: vNode.isLeaf,
  245. }
  246. })
  247. },
  248. onDragEnter(info) {
  249. // expandedKeys 需要受控时设置
  250. this.expandedKeys = info.expandedKeys
  251. },
  252. // 拖拽节点方法
  253. onDrop(info) {
  254. // 被插入节点信息
  255. const dropKey = info.node.eventKey
  256. // 拖拽节点信息
  257. const dragKey = info.dragNode.eventKey
  258. const dropPos = info.node.pos.split('-')
  259. // -1表示之前 0表示合并 1表示之后
  260. const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1])
  261. const loop = (data, key, callback) => {
  262. data.forEach((item, index, arr) => {
  263. if (item.id === key) {
  264. return callback(item, index, arr)
  265. }
  266. if (item.children) {
  267. return loop(item.children, key, callback)
  268. }
  269. })
  270. }
  271. const data = this.treeData
  272. // Find dragObject
  273. let dragObj
  274. loop(data, dragKey, (item, index, arr) => {
  275. arr.splice(index, 1)
  276. dragObj = item
  277. })
  278. if (!info.dropToGap) {
  279. // Drop on the content
  280. loop(data, dropKey, (item) => {
  281. item.children = item.children || []
  282. // where to insert 示例添加到尾部,可以是随意位置
  283. item.children.push(dragObj)
  284. })
  285. } else if (
  286. (info.node.children || []).length > 0 && // Has children
  287. info.node.expanded && // Is expanded
  288. dropPosition === 1 // On the bottom gap
  289. ) {
  290. loop(data, dropKey, (item) => {
  291. item.children = item.children || []
  292. // where to insert 示例添加到尾部,可以是随意位置
  293. item.children.unshift(dragObj)
  294. })
  295. } else {
  296. let ar
  297. let i
  298. loop(data, dropKey, (item, index, arr) => {
  299. ar = arr
  300. i = index
  301. })
  302. if (dropPosition === -1) {
  303. ar.splice(i, 0, dragObj)
  304. } else {
  305. ar.splice(i + 1, 0, dragObj)
  306. }
  307. }
  308. this.treeData = data
  309. // 拖拽完成,调用接口
  310. const dragParams = {
  311. configId: this.treeparams.configId,
  312. id: dragKey,
  313. targetId: dropKey,
  314. position: dropPosition,
  315. }
  316. auditFindCatTreeService.dragNode(dragParams).then((res) => {
  317. console.log(res)
  318. })
  319. },
  320. // 点击地址树展开时调用
  321. onLoadData(treeNode) {
  322. return auditFindCatTreeService.getCategoryTree(treeNode.dataRef.id).then((res) => {
  323. treeNode.dataRef.children = res.data
  324. this.treeData = this.transformData([...this.treeData])
  325. this.generateList(this.treeData)
  326. })
  327. },
  328. // 初始化地址树
  329. initTreeData(depId) {
  330. let topDepId = null
  331. auditFindCatTreeService.getCategoryTree('PROBLEM_CATEGORY_FUNCTION').then((res) => {
  332. this.spinning = false
  333. topDepId = !depId ? res.data[0].id : depId
  334. const treeNode = res.data
  335. res.data.forEach((item) => {
  336. this.defaultSelectedKeys.push(item.id)
  337. this.selectedKeys.push(item.id)
  338. this.defaultTreeExpandedKeys.push(item.id)
  339. this.treeData = this.transformData(treeNode)
  340. this.expandedKeys = this.defaultTreeExpandedKeys
  341. this.generateList(this.treeData)
  342. this.empty = false
  343. this.$emit('treeSelect', this.defaultSelectedKeys, item)
  344. })
  345. // this.defaultSelectedKeys = [res.data[0].id]
  346. // this.selectedKeys = [res.data[0].id]
  347. // this.defaultTreeExpandedKeys = [res.data[0].id]
  348. // // auditFindCatTreeService.getCategoryTree(topDepId).then((res1) => {
  349. // // console.log(treeNode)
  350. // this.treeData = this.transformData(treeNode)
  351. // this.expandedKeys = this.defaultTreeExpandedKeys
  352. // this.generateList(this.treeData)
  353. // this.empty = false
  354. // this.$emit('treeSelect', this.defaultSelectedKeys, res.data[0])
  355. // // })
  356. })
  357. },
  358. },
  359. }
  360. </script>
  361. <style module lang="scss">
  362. @use '@/common/design' as *;
  363. .spin {
  364. width: 100%;
  365. // line-height: 30;
  366. }
  367. .ywlxtree {
  368. :global(.ant-tree-title) {
  369. display: inline-block;
  370. width: 100%;
  371. overflow: hidden;
  372. text-overflow: ellipsis;
  373. white-space: nowrap;
  374. }
  375. :global(.ant-input-search) {
  376. margin: 8px 0;
  377. overflow: hidden;
  378. }
  379. .active {
  380. color: $primary-color;
  381. }
  382. :global(.ant-select) {
  383. width: 100%;
  384. overflow: hidden;
  385. }
  386. }
  387. .treewrap {
  388. // position: relative;
  389. // display: flex;
  390. // flex-direction: column;
  391. // width: 100%;
  392. height: 500px;
  393. // margin-right: $padding-lg;
  394. // transition: width 0.2s;
  395. .fold {
  396. position: absolute;
  397. top: calc(50% - 30px);
  398. right: -15px;
  399. z-index: 2;
  400. width: 15px;
  401. height: 75px;
  402. padding: 0;
  403. border-radius: 0 10px 10px 0;
  404. }
  405. :global(.ant-tree) {
  406. overflow: hidden;
  407. text-overflow: ellipsis;
  408. white-space: nowrap;
  409. }
  410. :global(.ant-card-body) {
  411. background: $white;
  412. }
  413. }
  414. .collapse {
  415. width: 0;
  416. :global(.ant-card-body) {
  417. background: transparent;
  418. :global(.ant-empty) {
  419. display: none;
  420. }
  421. }
  422. }
  423. // 单选样式
  424. .single {
  425. :global .ant-tree-checkbox {
  426. .ant-tree-checkbox-inner {
  427. border-radius: 100px;
  428. &::after {
  429. position: absolute;
  430. top: 3px;
  431. left: 3px;
  432. display: table;
  433. width: 8px;
  434. height: 8px;
  435. content: ' ';
  436. background-color: $primary-color;
  437. border: 0;
  438. border-radius: 8px;
  439. opacity: 0;
  440. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  441. transform: scale(0);
  442. }
  443. }
  444. &.ant-tree-checkbox-checked {
  445. &::after {
  446. position: absolute;
  447. top: 16.67%;
  448. left: 0;
  449. width: 100%;
  450. height: 66.67%;
  451. content: '';
  452. border: 1px solid #1890ff;
  453. border-radius: 50%;
  454. animation: antRadioEffect 0.36s ease-in-out;
  455. animation-fill-mode: both;
  456. }
  457. .ant-tree-checkbox-inner {
  458. background-color: $white;
  459. &::after {
  460. opacity: 1;
  461. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  462. transform: scale(1);
  463. }
  464. }
  465. }
  466. }
  467. }
  468. </style>