projectDropDown.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="project-drop-down">
  3. <uv-drop-down ref="dropDown" sign="dropDown" text-active-color="#08979c" text-active-size="28rpx"
  4. text-color="#1f1f1f" text-size="28rpx" :extra-icon="{name:'arrow-down-fill',color:'#1f1f1f',size:'26rpx'}"
  5. :extra-active-icon="{name:'arrow-up-fill',color:'#08979c',size:'26rpx'}" :defaultValue="defaultValue"
  6. :custom-style="{padding: '0 30rpx'}" :is-sticky="false" @click="selectMenu" v-if="dropItem('item').label">
  7. <uv-drop-down-item name="item" type="2" :label="dropItem('item').label" :value="dropItem('item').value">
  8. </uv-drop-down-item>
  9. <uv-drop-down-item name="target" type="2" :label="dropItem('target').label"
  10. :value="dropItem('target').value">
  11. </uv-drop-down-item>
  12. <uv-drop-down-item name="room" type="2" :label="dropItem('room').label" :value="dropItem('room').value">
  13. </uv-drop-down-item>
  14. </uv-drop-down>
  15. <uv-drop-down-popup sign="dropDown" :click-overlay-on-close="true" :currentDropItem="currentDropItem"
  16. @clickItem="clickItem" v-if="currentDropItem.value">
  17. </uv-drop-down-popup>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. onPageScroll() {
  23. // 滚动后及时更新位置
  24. this.$refs.dropDown.init();
  25. },
  26. computed: {
  27. dropItem(name) {
  28. return (name) => {
  29. const result = {};
  30. const find = this.result.find(item => item.name === name);
  31. if (find) {
  32. result.label = find.label;
  33. result.value = find.value;
  34. } else {
  35. result.label = this[name].label;
  36. result.value = this[name].value;
  37. }
  38. return result;
  39. }
  40. },
  41. // 获取当前下拉筛选项
  42. currentDropItem() {
  43. return this[this.activeName];
  44. }
  45. },
  46. data() {
  47. return {
  48. // 表示value等于这些值,就属于默认值
  49. defaultValue: [0, 'all', '0'],
  50. // 筛选结果
  51. result: [],
  52. activeName: 'item',
  53. item: {
  54. label: '建筑楼宇',
  55. value: 'all',
  56. activeIndex: 0,
  57. color: '#1f1f1f',
  58. activeColor: '#08979c',
  59. size: '28rpx',
  60. activeSize: '28rpx',
  61. child: [{
  62. label: '全部楼宇',
  63. value: 'all'
  64. }, {
  65. label: '1号楼',
  66. value: '1'
  67. }]
  68. },
  69. target: {
  70. label: '楼层位置',
  71. value: 'all',
  72. activeIndex: 0,
  73. color: '#1f1f1f',
  74. activeColor: '#08979c',
  75. size: '28rpx',
  76. activeSize: '28rpx',
  77. child: [{
  78. label: '全部楼层',
  79. value: 'all'
  80. }, {
  81. label: '1层',
  82. value: '1'
  83. }]
  84. },
  85. room: {
  86. label: '区域位置',
  87. value: 'all',
  88. activeIndex: 0,
  89. color: '#1f1f1f',
  90. activeColor: '#08979c',
  91. size: '28rpx',
  92. activeSize: '28rpx',
  93. child: [{
  94. label: '全部区域',
  95. value: 'all'
  96. }, {
  97. label: '办公室101',
  98. value: '1'
  99. }, {
  100. label: '办公室102',
  101. value: '2'
  102. }, {
  103. label: '办公室103',
  104. value: '3'
  105. }]
  106. },
  107. }
  108. },
  109. methods: {
  110. /**
  111. * 点击每个筛选项回调
  112. * @param {Object} e { name, active, type } = e
  113. */
  114. selectMenu(e) {
  115. const {
  116. name,
  117. active,
  118. type
  119. } = e;
  120. this.activeName = name;
  121. const find = this.result.find(item => item.name == this.activeName);
  122. if (find) {
  123. const findIndex = this[this.activeName].child.findIndex(item => item.label == find.label && item
  124. .value == find.value);
  125. this[this.activeName].activeIndex = findIndex;
  126. } else {
  127. this[this.activeName].activeIndex = 0;
  128. }
  129. },
  130. /**
  131. * 点击菜单回调处理
  132. * @param {Object} item 选中项 { label,value } = e
  133. */
  134. clickItem(e) {
  135. // 下面有重新赋值,所以用let
  136. let {
  137. label,
  138. value
  139. } = e;
  140. const findIndex = this.result.findIndex(item => item.name == this.activeName);
  141. if (this.defaultValue.indexOf(value) > -1 && this[this.activeName].label) {
  142. label = this[this.activeName].label;
  143. }
  144. // 已经存在筛选项
  145. if (findIndex > -1) {
  146. this.$set(this.result, findIndex, {
  147. name: this.activeName,
  148. label,
  149. value
  150. })
  151. } else {
  152. this.result.push({
  153. name: this.activeName,
  154. label,
  155. value
  156. });
  157. }
  158. this.result = this.result.filter(item => this.defaultValue.indexOf(item.value) == -1);
  159. this.$emit('change', {
  160. name: this.activeName,
  161. label,
  162. value
  163. });
  164. }
  165. }
  166. }
  167. </script>
  168. <style lang="scss">
  169. .project-drop-down {
  170. position: fixed;
  171. top: 0;
  172. left: 0;
  173. right: 0;
  174. z-index: 999;
  175. }
  176. </style>