当前位置:网站首页>小程序实现搜索功能续
小程序实现搜索功能续
2022-08-10 00:00:00 【低代码布道师】
我们上一篇利用自定义连接器实现了搜索功能,但测试发现使用数据列表无法做到数据更新。我们还是按照数据绑定的思路重新开发一下。
自定义连接器方法
module.exports = async function (params, context) {
const db = context.database
const _ = db.command
let result = {
data:[]}
let total = 0
if(params.keyword ==""){
result =await context.database.collection('lcap-data-26caupXt1-product_vfxwrnn-preview').
skip(params.pageSize*(params.pageNo-1)).limit(params.pageSize)
.get();
total = await context.database.collection('lcap-data-26caupXt1-product_vfxwrnn-preview').count();
}else{
result =await context.database.collection('lcap-data-26caupXt1-product_vfxwrnn-preview').
skip(params.pageSize*(params.pageNo-1)).limit(params.pageSize)
.where(_.or([
{
name:db.RegExp({
regexp: params.keyword,
options: 'i',
})},
{
desc:db.RegExp({
regexp: params.keyword,
options: 'i'})}
]))
.get();
total = await context.database.collection('lcap-data-26caupXt1-product_vfxwrnn-preview').where(_.or([
{
name:db.RegExp({
regexp: params.keyword,
options: 'i',
})},
{
desc:db.RegExp({
regexp: params.keyword,
options: 'i'})}
])).count();
// 在这里返回这个方法的结果,需要与出参定义的结构映射
}
return {
pageNo:params.pageNo,
pageSize:params.pageSize,
total:total.total,
records: result.data
};
};
变量定义
自定义连接器做好之后,在变量里需要定义三个变量,分别是pageNo、keyword和productList


组件搭建
变量定义好后,我们往页面中添加一个滚动容器
先往滚动容器里添加一个单行输入组件,修改字段标题为关键字
给单行输入组件设置行为,值改变时赋值给keyword变量
在单行输入组件下,我们增加个普通容器,里边放置一个文本组件
在普通容器上绑定循环展示,绑定我们的productList变量
文本组件的文本内容从循环变量里绑定
目前没有查询出数据来,是因为pageNo初始为0,我们修改一下,设定初始值为1
为了好看一点,我们给文本组件设置高度,并且设置一个边框

因为需要实现一下分页的效果,我们多录入一些数据,将每页显示的条数更改为5

实现分页
组件配置好之后,我们需要考虑如何实现分页的效果。首先是怎么触发分页,因为是在手机上浏览,我们一般是需要触底的时候实现分页的效果。因此需要在滚动容器上增加触底的事件,我们触底时先将pageNo+1

页码+1之后,我们就需要用新的页码再去调用自定义连接器,获取最新的数据
获取数据成功之后,我们再给列表数据productList重新赋值一下,将新获取到的数据追加进来就实现了分页的效果
{
...$page.dataset.state.productList,
records:$page.dataset.state.prodcutList?.records?.concat(event.detail?.records)
}

测试时候发现触底没法触发事件,原来需要给滚动容器设置一个默认的高度才可以
设置高度后,如果往下拉,分页的数据会追加到原来的集合中
总结
如果使用数据列表容器,设置比较简单,无需编写过多的代码。但是如果使用自定义连接器就不太好使,就得我们自己使用滚动容器实现一下分页的效果,方法比较多,需要多多实践才可以。
边栏推荐
- CAS:183896-00-6 (Biotin-PEG3-C3-NH2) PEG derivative
- Kubernetes 60个为什么
- Kubernetes服务接入Istio
- C语言--数据的存储(上)
- CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
- R语言使用glm函数构建logistic回归模型,使用forestmodel包的forest_model函数可视化逻辑回归模型对应的森林图
- LeetCode常见题型——链表
- Stanford CS143 Speed Pass PA1 Tutorial
- E - Sugoroku 3(期望dp)
- 【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
猜你喜欢

C语言--数据的存储(上)

CST Studio Suite 2021 software installation package and installation tutorial
![MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]](/img/10/55cc34a929768d89d9ffde8a5b3419.png)
MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]

Docker 面试题2则--取数据库连接数和docker-compose

CAS:851113-28-5 (生物素-ahx-ahx-酪胺)

使用C语言实现静态链表

程序员从佩洛西窜访事件中可以学到什么?

微服务【同步和异步通讯详解】第6章

Characteristics of the (CAS:1527486-16-3TAMRA-azide-PEG3-Biotin) reaction in biotin azide!

dlopen failed: library "libtaml.so" not found
随机推荐
ETCD快速入门-02 ETCD安装
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
程序员从佩洛西窜访事件中可以学到什么?
手把手教你编写性能测试用例
聚焦热点 | ISC 2022软件供应链安全治理与运营论坛圆满落幕
Mysql database ALTER basic operations
数据的存储——C语言
删除表空间数据文件
c语言结构体、函数以及指针练习(简单通讯录)
Characteristics of the (CAS:1527486-16-3TAMRA-azide-PEG3-Biotin) reaction in biotin azide!
-象棋比赛-
Redis 非关系型数据库学习(一) ---- Redis 的安装
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
GBJ1510-ASEMI机器人电源整流桥GBJ1510
基于ABP的AppUser对象扩展
Kubernetes 60个为什么
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
c语言文件基本操作总结
What should I do if there is no sound after reinstalling the system in win10?
02| operator