Browse Source

修复搜索功能,实现完全查询

pull/1/head
徐飞洋 3 months ago
parent
commit
367310efdc
  1. 59
      pages/buyer/index.js

59
pages/buyer/index.js

@ -1070,8 +1070,6 @@ Page({
searchKeyword: searchKeyword searchKeyword: searchKeyword
}) })
const { goods } = this.data
// 如果搜索词为空,重置分页并重新加载所有数据 // 如果搜索词为空,重置分页并重新加载所有数据
if (!searchKeyword.trim()) { if (!searchKeyword.trim()) {
console.log('搜索词为空,重置分页状态并重新加载数据'); console.log('搜索词为空,重置分页状态并重新加载数据');
@ -1091,22 +1089,13 @@ Page({
return return
} }
// ✅ 修改:实时过滤当前已加载的商品(前端搜索) // 实时搜索时不进行前端过滤,仅在用户点击搜索按钮时才向服务器请求完整结果
// 注意:这是在前端对已加载数据进行搜索,如果要搜索全部数据需要后端支持 // 这样可以确保搜索结果的完整性和准确性
const filtered = goods.filter(item =>
(item.name && item.name.toLowerCase().includes(searchKeyword.toLowerCase())) ||
(item.spec && item.spec.toLowerCase().includes(searchKeyword.toLowerCase())) ||
(item.yolk && item.yolk.toLowerCase().includes(searchKeyword.toLowerCase()))
)
this.setData({
filteredGoods: filtered
})
}, },
// 搜索商品 // 搜索商品
searchGoods() { searchGoods() {
const { searchKeyword, goods, totalGoods } = this.data const { searchKeyword } = this.data
// 如果搜索词为空,重置分页并重新加载 // 如果搜索词为空,重置分页并重新加载
if (!searchKeyword.trim()) { if (!searchKeyword.trim()) {
@ -1114,7 +1103,8 @@ Page({
this.setData({ this.setData({
page: 1, page: 1,
hasMoreData: true, hasMoreData: true,
selectedCategory: '全部' selectedCategory: '全部',
searchKeyword: ''
}, () => { }, () => {
this.loadGoods().then(() => { this.loadGoods().then(() => {
console.log('搜索重置后数据加载完成'); console.log('搜索重置后数据加载完成');
@ -1126,25 +1116,30 @@ Page({
return return
} }
// ✅ 添加:搜索范围提示 // 显示正在搜索的提示
if (goods.length < totalGoods) { wx.showLoading({
wx.showToast({ title: '正在搜索中...',
title: `正在${goods.length}个已加载商品中搜索`, mask: true
icon: 'none', });
duration: 1500
});
}
// ✅ 修改:在当前已加载商品中搜索(前端搜索)
const filtered = goods.filter(item =>
(item.name && item.name.toLowerCase().includes(searchKeyword.toLowerCase())) ||
(item.spec && item.spec.toLowerCase().includes(searchKeyword.toLowerCase())) ||
(item.yolk && item.yolk.toLowerCase().includes(searchKeyword.toLowerCase()))
)
// ✅ 修改:向服务器发送搜索请求,获取完整的搜索结果
// 重置分页状态,获取第一页的搜索结果
this.setData({ this.setData({
filteredGoods: filtered page: 1,
}) hasMoreData: true
}, () => {
this.loadGoods().then(() => {
console.log('搜索结果加载完成');
wx.hideLoading();
}).catch(err => {
console.error('搜索结果加载失败:', err);
wx.hideLoading();
wx.showToast({
title: '搜索失败,请重试',
icon: 'none'
});
});
});
}, },

Loading…
Cancel
Save