sticky-section.uvue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <page-head title="sticky-section"></page-head>
  3. <list-view id="list-view" ref="list-view" show-scrollbar=false class="page" :scroll-into-view="scrollIntoView"
  4. @scroll="onScroll" @scrollend="onScrollEnd" bounces="false" refresher-enabled="true" :refresher-triggered="refresherTriggered" @refresherrefresh="onRefresherrefresh">
  5. <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
  6. <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
  7. </list-item>
  8. <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
  9. <button @click="appendSectionItem(0)" size="mini">第一组 section 新增5条内容</button>
  10. </list-item>
  11. <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
  12. <button @click="deleteSection()" size="mini">删除第一组 section</button>
  13. </list-item>
  14. <sticky-section v-for="(section) in sectionArray" :key="section.name" :padding="sectionPadding"
  15. :push-pinned-header="true">
  16. <sticky-header :id="section.name">
  17. <text class="sticky-header-text">{{section.name}}</text>
  18. </sticky-header>
  19. <list-item v-for="(list) in section.list" :key="list.text" :name="list.text" class="content-item" :type=10>
  20. <text class="text">{{list.text}}</text>
  21. </list-item>
  22. </sticky-section>
  23. <list-item v-if="sectionArray.length > 0" style="padding: 10px; margin: 5px 0;align-items: center;" :type=30>
  24. <!-- <text style="color: #aaa">到底了</text> -->
  25. <button @click="toTop" size="mini">回到顶部</button>
  26. </list-item>
  27. </list-view>
  28. </template>
  29. <script>
  30. export type sectionData = {
  31. name : string,
  32. list : sectionListItem[]
  33. }
  34. export type sectionListItem = {
  35. text : string
  36. }
  37. export default {
  38. data() {
  39. return {
  40. data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
  41. sectionPadding: [0, 10, 0, 10] as Array<number>,
  42. scrollIntoView: "",
  43. scrolling: false,
  44. sectionArray: [] as sectionData[],
  45. appendId: 0,
  46. refresherTriggered: false,
  47. isReady: false//自动化测试使用
  48. }
  49. },
  50. onLoad() {
  51. },
  52. onReady() {
  53. //延迟onReady再加载数据 校验issues:14705 bug
  54. this.initSectionArray()
  55. this.isReady = true
  56. },
  57. methods: {
  58. initSectionArray() {
  59. this.sectionArray = [] as sectionData[]
  60. console.log("initSectionArray start", this.sectionArray.length)
  61. this.data.forEach(key => {
  62. const list = [] as sectionListItem[]
  63. for (let i = 1; i < 11; i++) {
  64. const item = { text: key + "--item--content----" + i } as sectionListItem
  65. list.push(item)
  66. }
  67. const data = { name: key, list: list } as sectionData
  68. this.sectionArray.push(data)
  69. }
  70. )
  71. console.log("initSectionArray end", this.sectionArray[0].name)
  72. },
  73. toTop() {
  74. this.scrollIntoView = ""
  75. uni.getElementById("list-view")!.scrollTop = 0
  76. },
  77. //用于自动化测试
  78. listViewScrollByY(y : number) {
  79. const listview = this.$refs["list-view"] as UniElement
  80. // listview.scrollBy(0, y)
  81. listview.scrollTop = y
  82. },
  83. gotoStickyHeader(id : string) {
  84. // #ifdef APP
  85. this.scrollIntoView = id
  86. // #endif
  87. // #ifdef WEB
  88. console.log("web端不支持该功能")
  89. // #endif
  90. },
  91. onScroll() {
  92. this.scrolling = true
  93. },
  94. onScrollEnd() {
  95. this.scrolling = false
  96. //滚动后重置scrollIntoView = ""
  97. if (this.scrollIntoView != "") {
  98. this.scrollIntoView = ""
  99. }
  100. },
  101. appendSectionItem(index : number) {
  102. const data = this.sectionArray[index]
  103. this.appendId++
  104. const list = [
  105. { text: data.name + "--item--content----new1--" + this.appendId } as sectionListItem,
  106. { text: data.name + "--item--content----new2--" + this.appendId } as sectionListItem,
  107. { text: data.name + "--item--content----new3--" + this.appendId } as sectionListItem,
  108. { text: data.name + "--item--content----new4--" + this.appendId } as sectionListItem,
  109. { text: data.name + "--item--content----new5--" + this.appendId } as sectionListItem
  110. ] as sectionListItem[]
  111. data.list.unshift(...list)
  112. },
  113. deleteSection() {
  114. this.sectionArray.shift()
  115. },
  116. onRefresherrefresh(_ : UniRefresherEvent) {
  117. this.refresherTriggered = true;
  118. setTimeout(() => {
  119. this.initSectionArray()
  120. this.refresherTriggered = false;
  121. }, 1000)
  122. },
  123. }
  124. }
  125. </script>
  126. <style>
  127. .page {
  128. flex: 1;
  129. background-color: #f5f5f5;
  130. }
  131. .sticky-header-text {
  132. font-size: 16px;
  133. padding: 8px;
  134. color: #959595;
  135. background-color: #f5f5f5;
  136. }
  137. .content-item {
  138. padding: 15px;
  139. margin-bottom: 10px;
  140. background-color: #fff;
  141. }
  142. </style>