HarmonyOS 关于LazyForEach懒加载实现列表页面后,搜索功能何如做?

可以用LazyForEach实现首次加载,想做搜索功能,搜索后还在当前页面显示,搜索后数据更新,页面数据应该怎样操作。

HarmonyOS
2024-10-29 10:40:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

​LazyForEach数据的修改一般都在类里定义方法进行处理。鉴于的场景其实可以考虑直接用普通的ForEach循环

1、内层数据的类新增searchKeyData方法进行搜索关键词过滤操作​。

// 内层数据  
class MyDataSource1 extends BasicDataSource {  
  private dataArray: string[] = [];  
  
  public totalCount(): number {  
    console.log('总长', this.dataArray.length)  
    return this.dataArray.length;  
  }  
  
  public getData(index: number): string {  
    return this.dataArray[index];  
  }  
  
  public addData(index: number, data: string): void {  
    this.dataArray.splice(index, 0, data);  
    this.notifyDataAdd(index);  
  }  
  
  public pushData(data: string): void {  
    this.dataArray.push(data);  
    this.notifyDataAdd(this.dataArray.length - 1);  
  }  
  
  public deleteData(index: number): void {  
    this.dataArray.splice(index, 1);  
    this.notifyDataDelete(index);  
  }  
  
  public searchKeyData(keyword: string): void {  
    let i = 0;  
    while (i < this.totalCount()) {  
      if (!this.dataArray[i].includes(keyword)) {  
        // 不匹配  
        this.deleteData(i);  
      } else {  
        // 内容匹配  
        i++;  
      }  
    }  
    console.log('12222', this.totalCount())  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

2、利用DataSource类里新增的方法进行数据修改。

Row(){  
  Text('查询内容:')  
  Text(this.keyWord)  
  Button('搜索')  
    .onClick(()=>{  
      console.log('333', this.list)  
      const total = this.list.totalCount();  
      for (let i = 0; i < total; i++) {  
        const listItemData = this.list.getData(i);  
        // 关键代码在MyDataSource1类中新增searchKeyData方法去进行过滤操作  
        listItemData.orderList.searchKeyData(this.keyWord);  
      }  
    })  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

​lazyforeach使用可以参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5#非首次渲染

分享
微博
QQ
微信
回复
2024-10-29 17:43:56


相关问题
使用LazyForEach加载列表相关问题
1726浏览 • 1回复 待解决
HarmonyOS LazyForEach 不会加载原因
733浏览 • 1回复 待解决
如何实现Fraction加载功能
7989浏览 • 1回复 待解决
LazyForEach加载的原理是什么
3254浏览 • 1回复 待解决
HarmonyOS 列表展示list加载问题
1282浏览 • 1回复 待解决
在鸿蒙中如何实现页面加载?
741浏览 • 0回复 待解决
HarmonyOS 加载列表更改属性UI不刷新
755浏览 • 1回复 待解决
HarmonyOS 如何实现搜索历史功能
861浏览 • 1回复 待解决
HarmonyOS 加载
586浏览 • 1回复 待解决
界面内容瀑布流加载实现
1808浏览 • 1回复 待解决
HarmonyOS 搜索功能
934浏览 • 1回复 待解决