Tree.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <u-popup class="jnpf-tree-select-popup" mode="right" v-model="showPopup" width="100%" @close="close">
  3. <view class="jnpf-tree-select-body">
  4. <view class="jnpf-tree-select-title">
  5. <text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
  6. <view class="title">组织选择</view>
  7. </view>
  8. <view class="jnpf-tree-select-search">
  9. <u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
  10. :show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
  11. </u-search>
  12. </view>
  13. <view class="jnpf-tree-selected">
  14. <view class="jnpf-tree-selected-head">
  15. <view>{{$t('component.jnpf.common.selected')}}</view>
  16. <view v-if="multiple" class="clear-btn" @click="setCheckAll">
  17. {{$t('component.jnpf.common.clearAll')}}
  18. </view>
  19. </view>
  20. <view class="jnpf-tree-selected-box">
  21. <scroll-view scroll-y="true" class="select-list">
  22. <u-tag closeable @close="delSelect(index)" v-for="(list,index) in selectList" :key="index"
  23. :text="list" class="u-selectTag" />
  24. </scroll-view>
  25. </view>
  26. </view>
  27. <view class="jnpf-tree-select-tree">
  28. <scroll-view :scroll-y="true" style="height: 100%">
  29. <ly-tree v-if="selectType !== 'all'" :tree-data="options" :node-key="realProps.value"
  30. default-expand-all :props="realProps" :filter-node-method="filterNode"
  31. child-visible-for-filter-node @node-click="handleNodeClick" ref="tree">
  32. </ly-tree>
  33. <ly-tree ref="tree" v-if="selectType === 'all'" :props="realProps" :node-key="realProps.value"
  34. :load="loadNode" lazy :tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
  35. @node-click="handleNodeClick" />
  36. </scroll-view>
  37. </view>
  38. <!-- 底部按钮 -->
  39. <view class="jnpf-tree-select-actions">
  40. <u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
  41. <u-button class="buttom-btn" type="primary"
  42. @click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
  43. </view>
  44. </view>
  45. </u-popup>
  46. </template>
  47. <script>
  48. const defaultProps = {
  49. label: 'fullName',
  50. value: 'id',
  51. icon: 'icon',
  52. children: 'children'
  53. }
  54. import {
  55. getOrgByOrganizeCondition,
  56. selectAsyncList
  57. } from '@/api/common.js'
  58. export default {
  59. props: {
  60. options: {
  61. type: Array,
  62. default () {
  63. return [];
  64. }
  65. },
  66. selectedData: {
  67. type: Array,
  68. default () {
  69. return [];
  70. }
  71. },
  72. selectType: {
  73. type: String,
  74. default: 'all'
  75. },
  76. ids: {
  77. type: [Array, String],
  78. default () {
  79. return [];
  80. }
  81. },
  82. // 通过双向绑定控制组件的弹出与收起
  83. modelValue: {
  84. type: Boolean,
  85. default: false
  86. },
  87. // 弹出的z-index值
  88. zIndex: {
  89. type: [String, Number],
  90. default: 0
  91. },
  92. props: {
  93. type: Object,
  94. default: () => ({
  95. label: 'fullName',
  96. value: 'id',
  97. icon: 'icon',
  98. children: 'children',
  99. isLeaf: 'isLeaf'
  100. })
  101. },
  102. multiple: {
  103. type: Boolean,
  104. default: false
  105. }
  106. },
  107. data() {
  108. return {
  109. moving: false,
  110. selectList: [],
  111. selectListId: [],
  112. keyword: '',
  113. showPopup: false,
  114. lazyOptions: [],
  115. level: 0
  116. };
  117. },
  118. watch: {
  119. // 在select弹起的时候,重新初始化所有数据
  120. modelValue: {
  121. handler(val) {
  122. this.showPopup = val
  123. if (val) setTimeout(() => this.init(), 10);
  124. },
  125. immediate: true
  126. }
  127. },
  128. created() {
  129. this.init()
  130. },
  131. computed: {
  132. uZIndex() {
  133. // 如果用户有传递z-index值,优先使用
  134. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  135. },
  136. realProps() {
  137. return {
  138. ...defaultProps,
  139. ...this.props
  140. }
  141. }
  142. },
  143. methods: {
  144. init() {
  145. this.keyword = ""
  146. this.selectListId = Array.isArray(this.ids) ? JSON.parse(JSON.stringify(this.ids)) : []
  147. this.selectList = JSON.parse(JSON.stringify(this.selectedData))
  148. },
  149. filterNode(value, data) {
  150. if (!value) return true;
  151. return data[this.realProps.label].indexOf(value) !== -1;
  152. },
  153. loadNode(node, resolve) {
  154. let id = node.key === null ? 0 : node.key
  155. this.level = node.level
  156. let data = {
  157. keyword: '',
  158. currOrgId: 0
  159. }
  160. selectAsyncList(data, id).then(res => {
  161. resolve(res.data?.list)
  162. })
  163. },
  164. selectAsyncList() {
  165. let data = {
  166. keyword: this.keyword,
  167. currOrgId: 0
  168. }
  169. this.lazyOptions = []
  170. selectAsyncList(data, 0).then(res => {
  171. this.lazyOptions = res.data.list || []
  172. })
  173. },
  174. handleNodeClick(obj) {
  175. let selectList;
  176. if (this.multiple) {
  177. this.selectList.push(obj.data.organize)
  178. this.selectList = [...new Set(this.selectList)];
  179. if (obj.data.organizeIds.length) this.selectListId.push(obj.data.organizeIds)
  180. this.selectListId = [...new Set(this.selectListId)];
  181. this.selectListId = this.selectListId.filter(o => Array.isArray(o))
  182. } else {
  183. this.selectList = []
  184. this.selectList.push(obj.data.organize)
  185. this.selectList = [...new Set(this.selectList)];
  186. this.selectListId = obj.data.organizeIds
  187. }
  188. },
  189. delSelect(index) {
  190. this.selectList.splice(index, 1);
  191. this.selectListId.splice(index, 1);
  192. },
  193. setCheckAll() {
  194. this.selectListId = []
  195. this.selectList = [];
  196. },
  197. handleSearch(val) {
  198. this.keyword = val
  199. if (this.selectType === 'all') return this.$u.debounce(this.selectAsyncList, 600)
  200. this.$refs.tree && this.$refs.tree.filter(val)
  201. },
  202. handleConfirm() {
  203. this.$emit('confirm', this.selectList, this.selectListId);
  204. this.close();
  205. },
  206. close() {
  207. this.$emit('close', false);
  208. }
  209. }
  210. };
  211. </script>