audit-archives-list-tree.vue 10 KB

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