index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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, list, index)">
  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.map(v => {
  92. if (v.value === this.value) {
  93. v.active = true
  94. } else {
  95. v.active = false
  96. }
  97. return v
  98. })
  99. } else {
  100. this.tabs = options.map(v => {
  101. return {
  102. label: v.label,
  103. value: v.value,
  104. children: v.children || []
  105. }
  106. })
  107. this.list = this.tabs[0].children.map(v => {
  108. if (v.value === this.value) {
  109. v.active = true
  110. } else {
  111. v.active = false
  112. }
  113. return v
  114. })
  115. this.tabActive = this.tabs[0].value
  116. }
  117. }
  118. },
  119. setLabel(value) {
  120. const option = this.getSelectOption(value)
  121. if (option) {
  122. if (option.tab) {
  123. this.label = option.tab.label + ' / ' + option.item.label
  124. } else {
  125. this.label = option.item.label
  126. }
  127. } else {
  128. this.label = ''
  129. }
  130. },
  131. reset() {
  132. this.cleanListActive()
  133. this.label = ''
  134. this.showOptions = false
  135. this.$emit('input', null)
  136. this.$emit('change', {
  137. detail: {
  138. value: null
  139. }
  140. })
  141. },
  142. confirm() {
  143. this.showOptions = false
  144. let val = null
  145. const option = this.getSelectOption()
  146. if (option) {
  147. this.setLabel()
  148. if (option.tab) {
  149. val = option.item.value
  150. } else {
  151. val = option.value
  152. }
  153. this.$emit('input', val)
  154. }
  155. this.$emit('change', {
  156. detail: {
  157. value: val
  158. }
  159. })
  160. },
  161. getSelectOption(value) {
  162. if (this.tabs.length) {
  163. for (const tab of this.tabs) {
  164. for (const item of tab.children) {
  165. if (item.active || item.value === value) {
  166. return {
  167. tab: tab,
  168. item: item
  169. }
  170. }
  171. }
  172. }
  173. } else {
  174. for (const item of this.list) {
  175. if (item.active || item.value === value) {
  176. return item
  177. }
  178. }
  179. }
  180. },
  181. cleanListActive() {
  182. this.list.forEach(item => {
  183. this.$set(item, 'active', false)
  184. })
  185. this.$emit('change', {
  186. detail: {
  187. value: null
  188. }
  189. })
  190. this.$emit('input', undefined)
  191. },
  192. clickOption(item, list, index) {
  193. this.cleanListActive()
  194. this.$set(item, 'active', true)
  195. this.$set(list, index, item)
  196. },
  197. touchStart() {
  198. this.showOptions = !this.showOptions
  199. const query = uni.createSelectorQuery().in(this)
  200. query.select('.u-dropdown-select').boundingClientRect(data => {
  201. if (data) {
  202. this.offsetTop = data.top + data.height
  203. // data.top 就是元素的offsetTop
  204. console.log('offsetTop:', data.top);
  205. }
  206. }).exec()
  207. console.log(this.showOptions)
  208. if (this.showOptions) {
  209. this.$emit('dropdown')
  210. }
  211. },
  212. showDropdown() {
  213. this.showOptions = true
  214. },
  215. hideDropdown() {
  216. this.showOptions = false
  217. }
  218. }
  219. }
  220. </script>
  221. <style lang="scss">
  222. .u-dropdown-tabs{
  223. padding: 10rpx 0;
  224. margin-top: 20rpx;
  225. }
  226. .van-empty{
  227. --image-width: 160rpx;
  228. --image-height: 160rpx;
  229. }
  230. .u-dropdown-select{
  231. @include display-flex-between;
  232. min-width: 211rpx;
  233. height: 50rpx;
  234. padding: 0 10rpx;
  235. border: $borderLight;
  236. border-radius: 8rpx;
  237. background-color: #FFFFFF;
  238. border-radius: 8rpx;
  239. border: 1rpx solid #D9D9D9;
  240. &.active{
  241. background-color: $buttonPrimaryColor;
  242. border-color: $buttonPrimaryColor;
  243. color: white;
  244. }
  245. .van-icon{
  246. font-size: 15rpx;
  247. }
  248. }
  249. .u-dropdown-label{
  250. max-width: 100%;
  251. @include text-ellipsis;
  252. font-size: $fontSize2;
  253. }
  254. .u-dropdown-tabs{
  255. --tabs-line-height: 44px;
  256. --tabs-card-height: 30px;
  257. --tabs-bottom-bar-height: 0rpx;
  258. --tab-active-text-color: #E57519;
  259. }
  260. .u-dropdown-panel{
  261. position: fixed;
  262. left: 0;
  263. top: 0;
  264. display: block;
  265. width: 100%;
  266. z-index: 9999;
  267. background-color: white;
  268. }
  269. .u-dropdown-options{
  270. min-height: 100rpx;
  271. max-height: 400rpx;
  272. padding: 20rpx;
  273. overflow-y: auto;
  274. }
  275. .u-dropdown-option{
  276. line-height: 50rpx;
  277. @include display-flex-between;
  278. font-size: $fontSize3;
  279. &.active{
  280. color: $buttonPrimaryColor;
  281. }
  282. }
  283. .u-dropdown-action{
  284. display: grid;
  285. grid-template-columns: 1fr 1fr;
  286. .van-button{
  287. border-radius: 0rpx;
  288. height: 80rpx;
  289. font-size: $fontSize3;
  290. }
  291. }
  292. .van-empty__description{
  293. font-size: $fontSize3;
  294. margin-top: 0rpx;
  295. }
  296. .button-info{
  297. --button-info-background-color: black;
  298. --button-info-border-color: black;
  299. --button-info-color: white;
  300. }
  301. .u-dropdown-select-mask{
  302. position: fixed;
  303. display: block;
  304. left: 0;
  305. right: 0;
  306. bottom: 0;
  307. z-index: 9998;
  308. background-color: rgba(0, 0, 0, 0.5);
  309. }
  310. </style>