Tree.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <view class="u-select">
  3. <u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="showPopup" length="auto"
  4. :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
  5. <view class="u-select">
  6. <view class="u-select__header" @touchmove.stop.prevent="">
  7. <view class="u-select__header__cancel u-select__header__btn" :style="{ color: cancelColor }"
  8. hover-class="u-hover-class" :hover-stay-time="150" @tap="close()">
  9. {{cancelText}}
  10. </view>
  11. <view class="u-select__header__title">{{title}}</view>
  12. <view class="u-select__header__confirm u-select__header__btn"
  13. :style="{ color: moving ? cancelColor : confirmColor }" hover-class="u-hover-class"
  14. :hover-stay-time="150" @touchmove.stop="" @tap.stop="handleConfirm()">
  15. {{confirmText}}
  16. </view>
  17. </view>
  18. <view class="search-box_sticky" v-if="filterable">
  19. <view class="search-box">
  20. <u-search :placeholder="$t('app.apply.pleaseKeyword')" height="72" :show-action="false"
  21. bg-color="#f0f2f6" shape="square" v-model="filterText">
  22. </u-search>
  23. </view>
  24. </view>
  25. <view class="u-select__body u-select__body__treeSelect">
  26. <view class="tree-box">
  27. <scroll-view :scroll-y="true" style="height: 100%">
  28. <ly-tree ref="tree" :node-key="realProps.value" :tree-data="options" :props="realProps"
  29. :show-node-icon="true" :filter-node-method="filterNode" child-visible-for-filter-node
  30. check-on-click-node :expand-on-click-node="false" default-expand-all
  31. :show-radio="!multiple" :show-checkbox="multiple" />
  32. </scroll-view>
  33. </view>
  34. </view>
  35. </view>
  36. </u-popup>
  37. </view>
  38. </template>
  39. <script>
  40. const defaultProps = {
  41. label: 'fullName',
  42. value: 'id',
  43. icon: 'icon',
  44. children: 'children'
  45. }
  46. import LyTree from '@/components/ly-tree/ly-tree.vue'
  47. export default {
  48. name: "tree-select",
  49. components: {
  50. LyTree
  51. },
  52. props: {
  53. options: {
  54. type: Array,
  55. default () {
  56. return [];
  57. }
  58. },
  59. filterable: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // 是否显示边框
  64. border: {
  65. type: Boolean,
  66. default: true
  67. },
  68. // 通过双向绑定控制组件的弹出与收起
  69. modelValue: {
  70. type: Boolean,
  71. default: false
  72. },
  73. // "取消"按钮的颜色
  74. cancelColor: {
  75. type: String,
  76. default: '#606266'
  77. },
  78. // "确定"按钮的颜色
  79. confirmColor: {
  80. type: String,
  81. default: '#2979ff'
  82. },
  83. // 弹出的z-index值
  84. zIndex: {
  85. type: [String, Number],
  86. default: 0
  87. },
  88. safeAreaInsetBottom: {
  89. type: Boolean,
  90. default: false
  91. },
  92. // 是否允许通过点击遮罩关闭Picker
  93. maskCloseAble: {
  94. type: Boolean,
  95. default: true
  96. },
  97. defaultValue: {
  98. type: Array,
  99. default: () => []
  100. },
  101. props: {
  102. type: Object,
  103. default: () => ({
  104. label: 'fullName',
  105. value: 'id',
  106. icon: 'icon',
  107. children: 'children'
  108. })
  109. },
  110. // 只能选择最后一层的数值
  111. lastLevel: {
  112. type: Boolean,
  113. default: false
  114. },
  115. // 只能选择最后一层的数值时,需要根据lastLevelKey来判断是否最后一层
  116. lastLevelKey: {
  117. type: String,
  118. default: "hasChildren"
  119. },
  120. lastLevelValue: {
  121. default: false
  122. },
  123. multiple: {
  124. type: Boolean,
  125. default: false
  126. },
  127. // 顶部标题
  128. title: {
  129. type: String,
  130. default: ''
  131. },
  132. // 取消按钮的文字
  133. cancelText: {
  134. type: String,
  135. default: '取消'
  136. },
  137. // 确认按钮的文字
  138. confirmText: {
  139. type: String,
  140. default: '确认'
  141. }
  142. },
  143. data() {
  144. return {
  145. filterText: '',
  146. moving: false,
  147. showPopup: false
  148. };
  149. },
  150. watch: {
  151. // 在select弹起的时候,重新初始化所有数据
  152. modelValue: {
  153. immediate: true,
  154. handler(val) {
  155. this.showPopup = val
  156. if (val) setTimeout(() => this.init(), 10);
  157. }
  158. },
  159. filterText(val) {
  160. this.$refs.tree.filter(val);
  161. }
  162. },
  163. computed: {
  164. uZIndex() {
  165. // 如果用户有传递z-index值,优先使用
  166. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  167. },
  168. realProps() {
  169. return {
  170. ...defaultProps,
  171. ...this.props
  172. }
  173. }
  174. },
  175. methods: {
  176. // 标识滑动开始,只有微信小程序才有这样的事件
  177. pickstart() {
  178. // #ifdef MP-WEIXIN
  179. this.moving = true;
  180. // #endif
  181. },
  182. // 标识滑动结束
  183. pickend() {
  184. // #ifdef MP-WEIXIN
  185. this.moving = false;
  186. // #endif
  187. },
  188. filterNode(value, data) {
  189. if (!value) return true;
  190. return data[this.realProps.label].indexOf(value) !== -1;
  191. },
  192. init() {
  193. this.filterText = ''
  194. this.setSelectValue();
  195. },
  196. // 获取默认选中的值
  197. setSelectValue() {
  198. this.$nextTick(() => {
  199. this.$refs.tree.setCheckedKeys(this.defaultValue)
  200. })
  201. },
  202. close() {
  203. this.$emit('close');
  204. },
  205. handleConfirm() {
  206. // #ifdef MP-WEIXIN
  207. if (this.moving) return;
  208. // #endif
  209. let selectValue = this.$refs.tree.getCheckedNodes()
  210. if (this.lastLevel) {
  211. selectValue = selectValue.filter(o => o[this.lastLevelKey] == this.lastLevelValue)
  212. }
  213. if (!selectValue.length) return
  214. this.$emit('confirm', selectValue);
  215. this.close();
  216. }
  217. }
  218. };
  219. </script>
  220. <style scoped lang="scss">
  221. .u-select {
  222. &__action {
  223. position: relative;
  224. line-height: $u-form-item-height;
  225. height: $u-form-item-height;
  226. &__icon {
  227. position: absolute;
  228. right: 20rpx;
  229. top: 50%;
  230. transition: transform .4s;
  231. transform: translateY(-50%);
  232. z-index: 1;
  233. &--reverse {
  234. transform: rotate(-180deg) translateY(50%);
  235. }
  236. }
  237. }
  238. &__hader {
  239. &__title {
  240. color: $u-content-color;
  241. }
  242. }
  243. &--border {
  244. border-radius: 6rpx;
  245. border-radius: 4px;
  246. border: 1px solid $u-form-item-border-color;
  247. }
  248. &__header {
  249. display: flex;
  250. align-items: center;
  251. justify-content: space-between;
  252. height: 80rpx;
  253. padding: 0 40rpx;
  254. }
  255. &__body {
  256. width: 100%;
  257. height: 500rpx;
  258. overflow: hidden;
  259. background-color: #fff;
  260. .tree-box {
  261. height: 100%;
  262. }
  263. &__picker-view {
  264. height: 100%;
  265. box-sizing: border-box;
  266. &__item {
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. font-size: 32rpx;
  271. color: $u-main-color;
  272. padding: 0 8rpx;
  273. }
  274. }
  275. }
  276. }
  277. </style>