index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view>
  3. <view class="u-dropdown-select" :class="{ 'active': showOptions }" @touchstart="touchStart">
  4. <view class="u-dropdown-label">{{ label || placeholder }} </view>
  5. <van-icon name="arrow-down" />
  6. </view>
  7. <view v-if="showOptions" class="u-dropdown-select-mask" :style="{ 'top': offsetTop +'px' }" @click="showOptions = false"></view>
  8. <view v-if="showOptions" class="u-dropdown-panel" :style="{ 'top': offsetTop +'px' }">
  9. <view class="u-dropdown-tabs">
  10. <u-tabs :active.sync="tabActive" :tabs="tabs" tab-style="tag" @change="tabChange"/>
  11. </view>
  12. <view class="u-dropdown-options">
  13. <template v-if="list.length">
  14. <template v-for="(item, index) in list">
  15. <view class="u-dropdown-option" :class="{ 'active': item.active }" @click="clickOption(item)">
  16. <view>{{ item.label }}</view>
  17. <van-icon class="van-icon" name="arrow" />
  18. </view>
  19. </template>
  20. </template>
  21. <van-empty v-else class="van-empty" description="没有数据"/>
  22. </view>
  23. <view class="u-dropdown-action">
  24. <van-button type="info" class="button-info" block @click="reset">重置</van-button>
  25. <van-button type="primary" class="button-primary" block @click="confirm">确定</van-button>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import UTabs from '@/components/common/u-tabs/index.vue'
  32. export default {
  33. options: {
  34. styleIsolation: 'shared'
  35. },
  36. components: {
  37. UTabs
  38. },
  39. props: {
  40. placeholder: String,
  41. options: Array,
  42. dataApi: Function,
  43. dataParams: Object,
  44. value: [String, Number]
  45. },
  46. watch: {
  47. options() {
  48. this.initComponent()
  49. }
  50. },
  51. data() {
  52. return {
  53. showOptions: false,
  54. offsetTop: 0,
  55. tabActive: 3,
  56. tabs: [],
  57. list: [],
  58. label: ''
  59. }
  60. },
  61. created() {
  62. this.initComponent()
  63. console.log('value:' + this.value)
  64. },
  65. mounted() {
  66. },
  67. methods: {
  68. initComponent() {
  69. if (this.options) {
  70. this.setOptions(this.options)
  71. }
  72. if (this.value) {
  73. this.setLabel(this.value)
  74. }
  75. if (this.dataApi) {
  76. this.dataApi(this.dataParams || {}).then(res => {
  77. this.setOptions(res.data)
  78. })
  79. }
  80. },
  81. tabChange(e) {
  82. this.list = this.tabs[e.detail.index].children
  83. },
  84. setOptions(options) {
  85. if (options) {
  86. if (!options.length) {
  87. this.tabs = []
  88. this.list = []
  89. } else if (!options[0].children || options[0].children.length === 0) {
  90. this.tabs = []
  91. this.list = options
  92. } else {
  93. this.tabs = options.map(v => {
  94. return {
  95. label: v.label,
  96. value: v.value,
  97. children: v.children || []
  98. }
  99. })
  100. this.list = this.tabs[0].children
  101. this.tabActive = this.tabs[0].value
  102. }
  103. }
  104. },
  105. setLabel(value) {
  106. const option = this.getSelectOption(value)
  107. if (option) {
  108. if (option.tab) {
  109. this.label = option.tab.label + ' / ' + option.item.label
  110. } else {
  111. this.label = option.item.label
  112. }
  113. } else {
  114. this.label = ''
  115. }
  116. },
  117. reset() {
  118. this.cleanListActive()
  119. this.label = ''
  120. this.showOptions = false
  121. this.$emit('input', null)
  122. this.$emit('change', {
  123. detail: {
  124. value: null
  125. }
  126. })
  127. },
  128. confirm() {
  129. this.showOptions = false
  130. let val = null
  131. const option = this.getSelectOption()
  132. if (option) {
  133. this.setLabel()
  134. if (option.tab) {
  135. val = option.item.value
  136. } else {
  137. val = option.value
  138. }
  139. this.$emit('input', val)
  140. }
  141. this.$emit('change', {
  142. detail: {
  143. value: val
  144. }
  145. })
  146. },
  147. getSelectOption(value) {
  148. if (this.tabs.length) {
  149. for (const tab of this.tabs) {
  150. for (const item of tab.children) {
  151. if (item.active || item.value === value) {
  152. return {
  153. tab: tab,
  154. item: item
  155. }
  156. }
  157. }
  158. }
  159. } else {
  160. for (const item of this.list) {
  161. if (item.active || item.value === value) {
  162. return item
  163. }
  164. }
  165. }
  166. },
  167. cleanListActive() {
  168. this.list.forEach(item => {
  169. this.$set(item, 'active', false)
  170. })
  171. this.$emit('change', {
  172. detail: {
  173. value: null
  174. }
  175. })
  176. this.$emit('input', undefined)
  177. },
  178. clickOption(item) {
  179. this.cleanListActive()
  180. this.$set(item, 'active', true)
  181. },
  182. touchStart() {
  183. this.showOptions = !this.showOptions
  184. const query = uni.createSelectorQuery().in(this)
  185. query.select('.u-dropdown-select').boundingClientRect(data => {
  186. if (data) {
  187. this.offsetTop = data.top + data.height
  188. // data.top 就是元素的offsetTop
  189. console.log('offsetTop:', data.top);
  190. }
  191. }).exec()
  192. console.log(this.showOptions)
  193. if (this.showOptions) {
  194. this.$emit('dropdown')
  195. }
  196. },
  197. showDropdown() {
  198. this.showOptions = true
  199. },
  200. hideDropdown() {
  201. this.showOptions = false
  202. }
  203. }
  204. }
  205. </script>
  206. <style lang="scss">
  207. .u-dropdown-tabs{
  208. padding: 10rpx 0;
  209. margin-top: 20rpx;
  210. }
  211. .van-empty{
  212. --image-width: 160rpx;
  213. --image-height: 160rpx;
  214. }
  215. .u-dropdown-select{
  216. @include display-flex-between;
  217. min-width: 211rpx;
  218. height: 50rpx;
  219. padding: 0 10rpx;
  220. border: $borderLight;
  221. border-radius: 8rpx;
  222. background-color: #FFFFFF;
  223. border-radius: 8rpx;
  224. border: 1rpx solid #D9D9D9;
  225. &.active{
  226. background-color: $buttonPrimaryColor;
  227. border-color: $buttonPrimaryColor;
  228. color: white;
  229. }
  230. .van-icon{
  231. font-size: 15rpx;
  232. }
  233. }
  234. .u-dropdown-label{
  235. max-width: 100%;
  236. @include text-ellipsis;
  237. font-size: $fontSize2;
  238. }
  239. .u-dropdown-tabs{
  240. --tabs-line-height: 44px;
  241. --tabs-card-height: 30px;
  242. --tabs-bottom-bar-height: 0rpx;
  243. --tab-active-text-color: #E57519;
  244. }
  245. .u-dropdown-panel{
  246. position: fixed;
  247. left: 0;
  248. top: 0;
  249. display: block;
  250. width: 100%;
  251. z-index: 9999;
  252. background-color: white;
  253. }
  254. .u-dropdown-options{
  255. min-height: 100rpx;
  256. max-height: 400rpx;
  257. padding: 20rpx;
  258. overflow-y: auto;
  259. }
  260. .u-dropdown-option{
  261. line-height: 50rpx;
  262. @include display-flex-between;
  263. font-size: $fontSize3;
  264. &.active{
  265. color: $buttonPrimaryColor;
  266. }
  267. }
  268. .u-dropdown-action{
  269. display: grid;
  270. grid-template-columns: 1fr 1fr;
  271. .van-button{
  272. border-radius: 0rpx;
  273. height: 80rpx;
  274. font-size: $fontSize3;
  275. }
  276. }
  277. .van-empty__description{
  278. font-size: $fontSize3;
  279. margin-top: 0rpx;
  280. }
  281. .button-info{
  282. --button-info-background-color: black;
  283. --button-info-border-color: black;
  284. --button-info-color: white;
  285. }
  286. .u-dropdown-select-mask{
  287. position: fixed;
  288. display: block;
  289. left: 0;
  290. right: 0;
  291. bottom: 0;
  292. z-index: 9998;
  293. background-color: rgba(0, 0, 0, 0.5);
  294. }
  295. </style>