当前位置:网站首页>小程序实现搜索功能续
小程序实现搜索功能续
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)
}

测试时候发现触底没法触发事件,原来需要给滚动容器设置一个默认的高度才可以
设置高度后,如果往下拉,分页的数据会追加到原来的集合中
总结
如果使用数据列表容器,设置比较简单,无需编写过多的代码。但是如果使用自定义连接器就不太好使,就得我们自己使用滚动容器实现一下分页的效果,方法比较多,需要多多实践才可以。
边栏推荐
- What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
- Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
- 无源晶振负载电容值CL匹配方法及说明
- 2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
- Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
- 《痞子衡嵌入式半月刊》 第 60 期
- Moonbeam网络维护模式(Maintenance Mode)解读
- 02|运算符
- Redis 非关系型数据库学习(一) ---- Redis 的安装
- c语言指针练习题
猜你喜欢

What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?

3.1 - 程序设计语言 3.2 - 高级语言的特点及引用 3.3 - 静态/动态类型语言

win10重装系统后没声音怎么办?

How to turn off system protection in Win11?How to turn off the system protection restore function?

deepstream学习笔记(三):deepstream-imagedata-multistream解析与接入适配yolov5模型测试

How to add control panel to right click menu in win7

《痞子衡嵌入式半月刊》 第 60 期

Docker interview question 2--get the number of database connections and docker-compose

The older tester has just passed the "hurdle" of being 35 years old, and I want to tell you something from my heart

将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
随机推荐
生物素叠氮化物中的(CAS:1527486-16-3TAMRA-azide-PEG3-Biotin)反应的特点!
【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
02| operator
Docker interview question 2--get the number of database connections and docker-compose
openEuler 知:abi 检测
走出迷宫的最少步数2
Leetcode79. 单词搜索
渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)
使用 apxs 构建和安装 Apache 扩展共享对象模块
Solidity 智能合约入门
关于HBuilder X鼠标变为_ 替换字符的问题
c语言文件基本操作总结
GBJ1510-ASEMI机器人电源整流桥GBJ1510
XSS高级 svg 复现一个循环问题以及两个循环问题
[obs] obsqsv11 hard coding and comparison with metartc codec
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
Mysql数据库 ALTER 基本操作
Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid
分形网络(FractalNet)----学习笔记