当前位置:首页 > uni-app

uniapp搜索筛选

2026-03-05 00:31:13uni-app

实现搜索筛选功能

在UniApp中实现搜索筛选功能通常涉及前端界面设计和后端数据处理。以下是几种常见的方法:

使用<input>v-model绑定搜索关键词

uniapp搜索筛选

<template>
  <view>
    <input v-model="searchText" placeholder="输入关键词搜索" @input="filterData" />
    <view v-for="(item, index) in filteredList" :key="index">
      {{ item.name }}
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      originalList: [],
      filteredList: []
    }
  },
  methods: {
    filterData() {
      this.filteredList = this.originalList.filter(item => 
        item.name.includes(this.searchText)
      )
    }
  }
}
</script>

结合下拉筛选组件

<template>
  <view>
    <picker @change="bindPickerChange" :value="index" :range="filterOptions">
      <view>当前筛选:{{filterOptions[index]}}</view>
    </picker>
    <view v-for="(item, i) in filteredData" :key="i">
      {{item.name}}
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      filterOptions: ['全部', '类型1', '类型2'],
      index: 0,
      originalData: [],
      filteredData: []
    }
  },
  methods: {
    bindPickerChange(e) {
      this.index = e.detail.value
      this.applyFilter()
    },
    applyFilter() {
      if(this.index === 0) {
        this.filteredData = [...this.originalData]
      } else {
        this.filteredData = this.originalData.filter(item => 
          item.type === this.filterOptions[this.index]
        )
      }
    }
  }
}
</script>

优化搜索性能

对于大数据量列表,建议采用防抖技术减少频繁触发搜索:

uniapp搜索筛选

let timer = null
methods: {
  filterData() {
    clearTimeout(timer)
    timer = setTimeout(() => {
      // 实际过滤逻辑
    }, 300)
  }
}

多条件组合筛选

实现多个筛选条件的组合查询:

filterData() {
  this.filteredList = this.originalList.filter(item => {
    const nameMatch = item.name.includes(this.searchText)
    const typeMatch = this.selectedType === '全部' || item.type === this.selectedType
    const priceMatch = item.price >= this.minPrice && item.price <= this.maxPrice
    return nameMatch && typeMatch && priceMatch
  })
}

与后端API结合

当数据量较大时,建议将筛选逻辑放在后端:

async fetchFilteredData() {
  const res = await uni.request({
    url: '/api/filter',
    data: {
      keyword: this.searchText,
      category: this.selectedCategory,
      page: this.currentPage
    }
  })
  this.filteredList = res.data.list
}

注意事项

  • 移动端输入框建议添加confirm-type="search"属性优化键盘体验
  • 筛选结果为空时应显示友好提示
  • 复杂筛选条件可考虑使用弹出层或侧边栏布局
  • 分页加载时需要重置页码参数

标签: uniapp
分享给朋友:

相关文章

uniapp隐藏滚动条

uniapp隐藏滚动条

在UniApp中隐藏滚动条可以通过以下几种方法实现,具体根据使用场景选择: 全局样式设置(APP/H5) 通过修改全局CSS样式隐藏滚动条,适用于所有页面: ::-webkit-scrollb…

uniapp视频

uniapp视频

uniapp视频开发基础 uniapp支持通过video组件实现视频播放功能,该组件兼容多端(H5、小程序、App等),基本用法如下: <template> <video…

uniapp 后门

uniapp 后门

关于 uniapp 后门的问题,目前没有权威证据表明 uniapp 官方存在故意植入后门的行为。但作为开发者,需注意以下安全实践: 检查第三方插件和依赖 确保项目中使用的第三方插件来源可靠,定期更新…

uniapp 用法

uniapp 用法

uniapp 基本概念 uniapp 是一个基于 Vue.js 的跨平台开发框架,支持编译到 iOS、Android、H5 及小程序等多端。其核心特点是“一次开发,多端发布”,通过条件编译实现平台差异…

重构uniapp

重构uniapp

重构 Uniapp 项目的关键方法 代码结构优化 将页面、组件、静态资源按功能模块划分,避免全部堆放在根目录。建议采用以下结构: src/ ├── components/ // 通用组…

引擎uniapp

引擎uniapp

uniapp 引擎概述 uniapp 是一个基于 Vue.js 的跨平台开发框架,允许开发者使用单一代码库构建多端应用(如 iOS、Android、Web、小程序等)。其核心引擎通过条件编译和运行时适…