index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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, 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) {
  193. this.cleanListActive()
  194. this.$set(item, 'active', true)
  195. },
  196. touchStart() {
  197. this.showOptions = !this.showOptions
  198. const query = uni.createSelectorQuery().in(this)
  199. query.select('.u-dropdown-select').boundingClientRect(data => {
  200. if (data) {
  201. this.offsetTop = data.top + data.height
  202. // data.top 就是元素的offsetTop
  203. console.log('offsetTop:', data.top);
  204. }
  205. }).exec()
  206. console.log(this.showOptions)
  207. if (this.showOptions) {
  208. this.$emit('dropdown')
  209. }
  210. },
  211. showDropdown() {
  212. this.showOptions = true
  213. },
  214. hideDropdown() {
  215. this.showOptions = false
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss">
  221. .u-dropdown-tabs{
  222. padding: 10rpx 0;
  223. margin-top: 20rpx;
  224. }
  225. .van-empty{
  226. --image-width: 160rpx;
  227. --image-height: 160rpx;
  228. }
  229. .u-dropdown-select{
  230. @include display-flex-between;
  231. min-width: 211rpx;
  232. height: 50rpx;
  233. padding: 0 10rpx;
  234. border: $borderLight;
  235. border-radius: 8rpx;
  236. background-color: #FFFFFF;
  237. border-radius: 8rpx;
  238. border: 1rpx solid #D9D9D9;
  239. &.active{
  240. background-color: $buttonPrimaryColor;
  241. border-color: $buttonPrimaryColor;
  242. color: white;
  243. }
  244. .van-icon{
  245. font-size: 15rpx;
  246. }
  247. }
  248. .u-dropdown-label{
  249. max-width: 100%;
  250. @include text-ellipsis;
  251. font-size: $fontSize2;
  252. }
  253. .u-dropdown-tabs{
  254. --tabs-line-height: 44px;
  255. --tabs-card-height: 30px;
  256. --tabs-bottom-bar-height: 0rpx;
  257. --tab-active-text-color: #E57519;
  258. }
  259. .u-dropdown-panel{
  260. position: fixed;
  261. left: 0;
  262. top: 0;
  263. display: block;
  264. width: 100%;
  265. z-index: 9999;
  266. background-color: white;
  267. }
  268. .u-dropdown-options{
  269. min-height: 100rpx;
  270. max-height: 400rpx;
  271. padding: 20rpx;
  272. overflow-y: auto;
  273. }
  274. .u-dropdown-option{
  275. line-height: 50rpx;
  276. @include display-flex-between;
  277. font-size: $fontSize3;
  278. &.active{
  279. color: $buttonPrimaryColor;
  280. }
  281. }
  282. .u-dropdown-action{
  283. display: grid;
  284. grid-template-columns: 1fr 1fr;
  285. .van-button{
  286. border-radius: 0rpx;
  287. height: 80rpx;
  288. font-size: $fontSize3;
  289. }
  290. }
  291. .van-empty__description{
  292. font-size: $fontSize3;
  293. margin-top: 0rpx;
  294. }
  295. .button-info{
  296. --button-info-background-color: black;
  297. --button-info-border-color: black;
  298. --button-info-color: white;
  299. }
  300. .u-dropdown-select-mask{
  301. position: fixed;
  302. display: block;
  303. left: 0;
  304. right: 0;
  305. bottom: 0;
  306. z-index: 9998;
  307. background-color: rgba(0, 0, 0, 0.5);
  308. }
  309. </style>