index.vue 7.0 KB

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