demo.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="demo">
  3. <nut-cell-group :title="translate('basic')">
  4. <nut-cell>
  5. <ul class="infiniteUl" id="scroll">
  6. <nut-infiniteloading container-id="scroll" :use-window="false" :has-more="hasMore" @load-more="loadMore">
  7. <li class="infiniteLi" v-for="(item, index) in defultList" :key="index">{{ item }}</li>
  8. </nut-infiniteloading>
  9. </ul>
  10. </nut-cell>
  11. </nut-cell-group>
  12. <nut-cell-group :title="translate('pullRefresh')">
  13. <nut-cell>
  14. <ul class="infiniteUl" id="refreshScroll">
  15. <nut-infiniteloading
  16. pull-icon="JD"
  17. container-id="refreshScroll"
  18. :use-window="false"
  19. :is-open-refresh="true"
  20. :has-more="refreshHasMore"
  21. @load-more="refreshLoadMore"
  22. @refresh="refresh"
  23. >
  24. <li class="infiniteLi" v-for="(item, index) in refreshList" :key="index">{{ item }}</li>
  25. </nut-infiniteloading>
  26. </ul>
  27. </nut-cell>
  28. </nut-cell-group>
  29. <nut-cell-group :title="translate('customTxt')">
  30. <nut-cell>
  31. <ul class="infiniteUl" id="customScroll">
  32. <nut-infiniteloading
  33. load-txt="loading"
  34. :load-more-txt="translate('none')"
  35. container-id="customScroll"
  36. :use-window="false"
  37. :has-more="customHasMore"
  38. @load-more="customLoadMore"
  39. >
  40. <li class="infiniteLi" v-for="(item, index) in customList" :key="index">{{ item }}</li>
  41. </nut-infiniteloading>
  42. </ul>
  43. </nut-cell>
  44. </nut-cell-group>
  45. </div>
  46. </template>
  47. <script lang="ts">
  48. import { onMounted, ref, reactive, toRefs, getCurrentInstance } from 'vue';
  49. import { createComponent } from '@/packages/utils/create';
  50. const { createDemo, translate } = createComponent('infiniteloading');
  51. import { useTranslate } from '@/sites/assets/util/useTranslate';
  52. useTranslate({
  53. 'zh-CN': {
  54. basic: '基础用法',
  55. pullRefresh: '下拉刷新',
  56. customTxt: '自定义加载文案',
  57. none: '没有啦~',
  58. success: '刷新成功'
  59. },
  60. 'en-US': {
  61. basic: 'Basic Usage',
  62. pullRefresh: 'Pull to refresh',
  63. customTxt: 'Custom loading copywriting',
  64. none: 'No more',
  65. success: 'Refresh success'
  66. }
  67. });
  68. export default createDemo({
  69. props: {},
  70. setup() {
  71. let { proxy } = getCurrentInstance() as any;
  72. const hasMore = ref(true);
  73. const customHasMore = ref(true);
  74. const refreshHasMore = ref(true);
  75. const data = reactive({
  76. defultList: [],
  77. customList: [],
  78. refreshList: []
  79. });
  80. const loadMore = (done) => {
  81. setTimeout(() => {
  82. const curLen = data.defultList.length;
  83. for (let i = curLen; i < curLen + 10; i++) {
  84. data.defultList.push(`${i}`);
  85. }
  86. if (data.defultList.length > 30) hasMore.value = false;
  87. done();
  88. }, 500);
  89. };
  90. const customLoadMore = (done) => {
  91. setTimeout(() => {
  92. const curLen = data.customList.length;
  93. for (let i = curLen; i < curLen + 10; i++) {
  94. data.customList.push(`${i}`);
  95. }
  96. if (data.customList.length > 30) customHasMore.value = false;
  97. done();
  98. }, 500);
  99. };
  100. const refreshLoadMore = (done) => {
  101. setTimeout(() => {
  102. const curLen = data.refreshList.length;
  103. for (let i = curLen; i < curLen + 10; i++) {
  104. data.refreshList.push(`${i}`);
  105. }
  106. if (data.refreshList.length > 30) refreshHasMore.value = false;
  107. done();
  108. }, 500);
  109. };
  110. const refresh = (done) => {
  111. setTimeout(() => {
  112. proxy.$toast.text(translate('success'));
  113. data.refreshList = data.refreshList.slice(0, 10);
  114. refreshHasMore.value = true;
  115. done();
  116. }, 1000);
  117. };
  118. const init = () => {
  119. for (let i = 0; i < 10; i++) {
  120. data.defultList.push(`${i}`);
  121. data.customList.push(`${i}`);
  122. data.refreshList.push(`${i}`);
  123. }
  124. };
  125. onMounted(() => {
  126. init();
  127. });
  128. return {
  129. loadMore,
  130. hasMore,
  131. customHasMore,
  132. customLoadMore,
  133. refreshHasMore,
  134. refreshLoadMore,
  135. refresh,
  136. translate,
  137. ...toRefs(data)
  138. };
  139. }
  140. });
  141. </script>
  142. <style lang="scss" scoped>
  143. .infiniteUl {
  144. height: 300px;
  145. width: 100%;
  146. padding: 0;
  147. margin: 0;
  148. overflow-y: auto;
  149. overflow-x: hidden;
  150. }
  151. .infiniteLi {
  152. margin-top: 10px;
  153. font-size: 14px;
  154. color: rgba(100, 100, 100, 1);
  155. text-align: center;
  156. }
  157. .loading {
  158. display: block;
  159. width: 100%;
  160. text-align: center;
  161. }
  162. </style>