uni-indexed-list.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="uni-indexed-list" ref="list" id="list">
  3. <!-- #ifdef APP-NVUE -->
  4. <list class="uni-indexed-list__scroll" scrollable="true" show-scrollbar="false">
  5. <cell v-for="(list, idx) in lists" :key="idx" :ref="'uni-indexed-list-' + idx">
  6. <!-- #endif -->
  7. <!-- #ifndef APP-NVUE -->
  8. <scroll-view :scroll-into-view="scrollViewId" class="uni-indexed-list__scroll" scroll-y @scrolltolower="getMore">
  9. <view v-for="(list, idx) in lists" :key="idx" :id="'uni-indexed-list-' + idx">
  10. <!-- #endif -->
  11. <uni-indexed-list-item :list="list" :loaded="loaded" :idx="idx" :showSelect="showSelect" @itemClick="onClick"></uni-indexed-list-item>
  12. <!-- #ifndef APP-NVUE -->
  13. </view>
  14. </scroll-view>
  15. <!-- #endif -->
  16. <!-- #ifdef APP-NVUE -->
  17. </cell>
  18. </list>
  19. <!-- #endif -->
  20. <view class="uni-indexed-list__menu">
  21. <view v-for="(list, key) in lists" :key="key" class="uni-indexed-list__menu-item" @click="goInitial(key)">
  22. <text class="uni-indexed-list__menu-text" :class="touchmoveIndex == key ? 'uni-indexed-list__menu-text--active' : ''">{{ list.key }}</text>
  23. </view>
  24. </view>
  25. <view v-if="touchmove" class="uni-indexed-list__alert-wrapper">
  26. <text class="uni-indexed-list__alert">{{ lists[touchmoveIndex].key }}</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import uniIcons from '../uni-icons/uni-icons.vue'
  32. import uniIndexedListItem from './uni-indexed-list-item.vue'
  33. // #ifdef APP-NVUE
  34. const dom = weex.requireModule('dom');
  35. // #endif
  36. // #ifdef APP-PLUS
  37. function throttle(func, delay) {
  38. var prev = Date.now();
  39. return function() {
  40. var context = this;
  41. var args = arguments;
  42. var now = Date.now();
  43. if (now - prev >= delay) {
  44. func.apply(context, args);
  45. prev = Date.now();
  46. }
  47. }
  48. }
  49. function touchMove(e) {
  50. let pageY = e.touches[0].pageY
  51. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  52. if (this.touchmoveIndex === index) {
  53. return false
  54. }
  55. let item = this.lists[index]
  56. if (item) {
  57. // #ifndef APP-NVUE
  58. this.scrollViewId = 'uni-indexed-list-' + index
  59. this.touchmoveIndex = index
  60. // #endif
  61. // #ifdef APP-NVUE
  62. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  63. animated: false
  64. })
  65. this.touchmoveIndex = index
  66. // #endif
  67. }
  68. }
  69. const throttleTouchMove = throttle(touchMove, 40)
  70. // #endif
  71. /**
  72. * IndexedList 索引列表
  73. * @description 用于展示索引列表
  74. * @tutorial https://ext.dcloud.net.cn/plugin?id=375
  75. * @property {Boolean} showSelect = [true|false] 展示模式
  76. * @value true 展示模式
  77. * @value false 选择模式
  78. * @property {Object} options 索引列表需要的数据对象
  79. * @event {Function} click 点击列表事件 ,返回当前选择项的事件对象
  80. * @example <uni-indexed-list options="" showSelect="false" @click=""></uni-indexed-list>
  81. */
  82. export default {
  83. name: 'UniIndexedList',
  84. components: {
  85. uniIcons,
  86. uniIndexedListItem
  87. },
  88. props: {
  89. options: {
  90. type: Array,
  91. default () {
  92. return []
  93. }
  94. },
  95. showSelect: {
  96. type: Boolean,
  97. default: false
  98. },
  99. hasMore:{
  100. type:Boolean,
  101. default:false
  102. }
  103. },
  104. data() {
  105. return {
  106. lists: [],
  107. winHeight: 0,
  108. itemHeight: 0,
  109. winOffsetY: 0,
  110. touchmove: false,
  111. touchmoveIndex: -1,
  112. scrollViewId: '',
  113. touchmoveTimeout: '',
  114. loaded: false
  115. }
  116. },
  117. watch: {
  118. options: {
  119. handler: function() {
  120. this.setList()
  121. },
  122. deep: true
  123. }
  124. },
  125. mounted() {
  126. setTimeout(() => {
  127. this.setList()
  128. }, 50)
  129. setTimeout(() => {
  130. this.loaded = true
  131. }, 300);
  132. },
  133. methods: {
  134. setList() {
  135. let index = 0;
  136. this.lists = [];
  137. this.options.forEach((value, index) => {
  138. if (value.goodsBrandInfoList.length === 0) {
  139. return
  140. }
  141. let indexBefore = index
  142. let items = value.goodsBrandInfoList.map(item => {
  143. let obj = {}
  144. obj['key'] = value.brandInitial;
  145. obj['name'] = item.brandName;
  146. obj['image'] = item.imageUrl;
  147. obj['brandDesc'] = item.brandDesc;
  148. obj['brandId'] = item.brandId;
  149. obj['itemIndex'] = index
  150. index++
  151. obj.checked = item.checked ? item.checked : false
  152. return obj
  153. })
  154. this.lists.push({
  155. title: value.brandInitial,
  156. key: value.brandInitial,
  157. items: items,
  158. itemIndex: indexBefore
  159. })
  160. })
  161. // #ifndef APP-NVUE
  162. uni.createSelectorQuery()
  163. .in(this)
  164. .select('#list')
  165. .boundingClientRect()
  166. .exec(ret => {
  167. this.winOffsetY = ret[0].top
  168. this.winHeight = ret[0].height
  169. this.itemHeight = this.winHeight / this.lists.length
  170. })
  171. // #endif
  172. // #ifdef APP-NVUE
  173. dom.getComponentRect(this.$refs['list'], (res) => {
  174. this.winOffsetY = res.size.top
  175. this.winHeight = res.size.height
  176. this.itemHeight = this.winHeight / this.lists.length
  177. })
  178. // #endif
  179. },
  180. //右侧字母导航点击事件,点击字母,列表自动滚动到对应的部分
  181. goInitial(index) {
  182. let item = this.lists[index]
  183. if (item) {
  184. this.scrollViewId = 'uni-indexed-list-' + index
  185. this.touchmoveIndex = index
  186. // #ifdef APP-NVUE
  187. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  188. animated: false
  189. })
  190. // #endif
  191. }
  192. },
  193. onClick(e) {
  194. let {
  195. item
  196. } = e
  197. //根据品牌id跳转到商品列表
  198. uni.navigateTo({
  199. url: `/pages/product/list?brandId=${item.brandId}`
  200. })
  201. },
  202. getMore(){
  203. if(this.hasMore){
  204. this.$emit("getData")
  205. }
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .uni-indexed-list {
  212. position: absolute;
  213. left: 0;
  214. top: 0;
  215. right: 0;
  216. bottom: 0;
  217. /* #ifndef APP-NVUE */
  218. display: flex;
  219. /* #endif */
  220. flex-direction: row;
  221. width: 750rpx;
  222. margin: 0 auto;
  223. }
  224. .uni-indexed-list__scroll {
  225. flex: 1;
  226. }
  227. .uni-indexed-list__menu {
  228. width: 80rpx;
  229. background-color: transparent;
  230. /* #ifndef APP-NVUE */
  231. display: flex;
  232. /* #endif */
  233. flex-direction: column;
  234. position: absolute;
  235. right: 0;
  236. z-index: 2;
  237. top: 15vh;
  238. }
  239. .uni-indexed-list__menu-item {
  240. /* #ifndef APP-NVUE */
  241. display: flex;
  242. /* #endif */
  243. flex: 1;
  244. align-items: center;
  245. justify-content: center;
  246. }
  247. .uni-indexed-list__menu-text {
  248. line-height: 32rpx;
  249. font-size: 24rpx;
  250. text-align: center;
  251. color: #2D2D2D;
  252. }
  253. .uni-indexed-list__menu--active {}
  254. .uni-indexed-list__menu-text--active {
  255. color: $main-color;
  256. }
  257. .uni-indexed-list__alert-wrapper {
  258. position: absolute;
  259. left: 0;
  260. top: 0;
  261. right: 0;
  262. bottom: 0;
  263. /* #ifndef APP-NVUE */
  264. display: flex;
  265. /* #endif */
  266. flex-direction: row;
  267. align-items: center;
  268. justify-content: center;
  269. }
  270. .uni-indexed-list__alert {
  271. width: 80px;
  272. height: 80px;
  273. border-radius: 80px;
  274. text-align: center;
  275. line-height: 80px;
  276. font-size: 35px;
  277. color: #fff;
  278. background-color: rgba(0, 0, 0, 0.5);
  279. }
  280. </style>