引入

  1. {
  2. "usingComponents": {
  3. "x-list":"waft-ui/assembly/list/list"
  4. }
  5. }

代码示例

  1. <x-list load="loadMore" loadingText="正在加载中">
  2. <view>
  3. <x-cell a:for="{{list}}" title="{{item.name}}"></x-cell>
  4. </view>
  5. </x-list>

组件演示

list.png

axml

  1. <view class="list">
  2. <x-nav-bar showArrow="{{true}}" title="测试 list 组件"></x-nav-bar>
  3. <view class="list-box">
  4. <x-list load="loadMore" loadingText="正在加载中">
  5. <view>
  6. <x-cell a:for="{{list}}" title="{{item.name}}" border="{{ true }}"></x-cell>
  7. </view>
  8. </x-list>
  9. </view>
  10. </view>

css

  1. .list {
  2. width: 100vw;
  3. height: 100vh;
  4. background-color: #F0F2F7;
  5. display: flex;
  6. flex-direction: column;
  7. justify-content: flex-start;
  8. align-items: flex-start;
  9. }
  10. .list-box {
  11. width: 100%;
  12. /*height: 520rpx;*/
  13. flex: 1;
  14. overflow: hidden;
  15. }
  16. .scroller {
  17. width: 100%;
  18. height: 200rpx;
  19. }
  20. .row {
  21. display: flex;
  22. flex-direction: row;
  23. justify-content: flex-start;
  24. align-items: flex-start;
  25. }

json

  1. {
  2. "usingComponents": {
  3. "x-nav-bar":"../assembly/nav-bar/nav-bar",
  4. "x-cell":"../assembly/cell/cell",
  5. "x-list": "../assembly/list/list"
  6. },
  7. "state": {
  8. "random": "为了数据能够变化",
  9. "list": [
  10. { "name": "name1" },
  11. { "name": "name2" },
  12. { "name": "name3" },
  13. { "name": "name4" },
  14. { "name": "name5" },
  15. { "name": "name6" },
  16. { "name": "name7" },
  17. { "name": "name8" },
  18. { "name": "name9" },
  19. { "name": "name10" }
  20. ]
  21. }
  22. }

ts

  1. import {console, Page, Props, Target, Event} from 'waft';
  2. import {JSONArray, JSONObject} from 'waft-json';
  3. import { lodash, CallbackEvent } from "waft-ui-common";
  4. export class TestList extends Page {
  5. start: i32 = 10;
  6. constructor(props: Props) {
  7. super(props);
  8. // 添加更多
  9. this.addEventListener('loadMore', (event, target) => {
  10. console.log('====### 进入到监听的 loadMore');
  11. const _this = target as TestList;
  12. _this.loadMore(event, target);
  13. });
  14. }
  15. /**
  16. * 加载更多
  17. * @param event
  18. * @param target
  19. */
  20. loadMore(event: Event, target: Target | null = null): void {
  21. console.log('====### 进入 loadMore 外部');
  22. lodash.request({
  23. url: 'https://getman.cn/mock/waft/testList',
  24. method: 'GET',
  25. data: new JSONObject(),
  26. target: target,
  27. event: event,
  28. success: (data: JSONObject, target: Target | null, event) => {
  29. console.log('====### request data is: ' + data.toString());
  30. const _this = target as TestList;
  31. const option = event as CallbackEvent;
  32. // 回调给 list
  33. const callbackData = new JSONObject();
  34. callbackData.set('loading', false); // 也可以不写, 默认为 false
  35. callbackData.set('finished', false); // 是否没有更多数据了, 可不传, 默认为 false
  36. callbackData.set('error', false); // 是否发生错误, 可不传, 默认 false
  37. // callbackData.set('errorText', ''); // 自定义错误的文本, 可不传, 默认为 '出错了呢', 注意: 传 '' 会不显示
  38. option.callback(option, callbackData);
  39. // 拼接并设置数据
  40. const list = lodash.get(_this.state, 'list', new JSONArray());
  41. const newList = list.arrayValue().concat(lodash.get(data, 'list', new JSONArray()).arrayValue());
  42. const state = new JSONObject();
  43. state.set('list', newList);
  44. console.log('====### 最终数据' + state.toString());
  45. _this.setState(state);
  46. }
  47. });
  48. }
  49. }

API

  • props | 参数 | 说明 | 类型 | 默认值 | | —- | —- | —- | —- | | offset | 距离底部多少触发 load 事件 | number | 40 | | loadingText | 加载中的文字 | string | 加载中 | | finishedText | 加载完的文字 | string | 没有更多内容了呢 | | errorText | 加载出错的文字 | string | 出错了呢 | | immediate | 是否在初始化时立即调用 onLoad | boolean | true | | direction | 滚动方向, 默认向下, 支持 column | row | string | column |

  • Events | 事件名 | 说明 | 参数 | | —- | —- | —- | | load | 加载的回调函数, (callback) => callback(data: JSONObject, event: CallbackEvent) | data = { loading: false, finished: false, error: false, errorText: ‘优先级大于 props’ } |

  • slot | 名称 | 说明 | | —- | —- | | default | 列表的内容 |