Vue || Uniapp 模糊查询

思路 :先请求接口,渲染到data里 ,给input输入框绑定v-model

            根据计算属性监听使用filter()方法和includes()进行过滤筛选。

<!-- 输入框 -->
    <input class="uni-input" placeholder="请输入name或memo查找" @input="handleInput" v-model="keyword" />
<!-- 员工的每一列 -->
<view>
    <view v-for="item in filteredItems">
        <span>{
   { item.memo }}</span>
        <span>{
   { item.name }}</span>
    </view>
</view>

export default {
    data() {
            return {
                // 模糊查询
                keyword: '',//输入框value
                items:[],
            };
        },
        // 计算属性监听filteredItems
        computed: {
            filteredItems() {
                if (this.keyword) {
                    return this.items.filter(item => item.name.includes(this.keyword)||
                    item.memo.includes(this.keyword)||
                    item.num.toString().includes(this.keyword)
                    )
                } else {
                    return this.items;
                }
            }
        },
          methods:{
            handleInput(event) {
                console.log(event);
                this.keyword = event.target.value;
                console.log(this.items);
            },
        }
}

以上是源代码部分 创作不易,觉得有用就点个大拇指+关注吧!       赠人玫瑰,手留余香~   

顺便再帮你理一下filter()方法includes()方法

filter() 
filter()方法用于把Array中的某些元素过滤掉,然后返回剩下的未被过滤掉的元素。
1、filter() 不会对空数组进行检测;
2、filter() 不会改变原始数组。
实例:
const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num > 14)
console.log(newArr);//打印 [17,18,32,33,16,40]
// 查找某个值-------------------------
const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num == 14)
console.log(newArr);//打印 [14]
//返回大于某个值和小于某个值的元素
const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num > 14 && num < 33)
console.log(newArr);//打印 [17, 18, 32, 16]


includes()
作用:
判断数组是否包含指定的值
判断字符串是否包含指定的子串
包含返回true,不包含返回false , 数组或者字符串都能够使用。
includes(a,b) 括号里第一个参数a为必选参数,表示要查找的某个参数;
第二位b为可选参数,表示从某个索引位置开始查找,默认为零,
若传的参数为负数,则用该数组的长度加上其值:arr.length+(-b)。
const arr = [1, 2, 3]
    console.log(arr.includes(2, 3)) // false
    console.log(arr.includes(2, 100)) // false
 -----------------------华丽的分割线--------------------------------------
const str = 'do not worry be happy'
    console.log(str.includes('do')) // true
    console.log(str.includes('don'))  // false

最后        ..............  栓Q  ..............

相关推荐

  1. Vue || Uniapp 模糊查询

    2023-12-12 23:36:01       54 阅读
  2. Vue实现模糊查询

    2023-12-12 23:36:01       62 阅读
  3. sql语句条件查询模糊查询

    2023-12-12 23:36:01       54 阅读
  4. mybatis多字段模糊查询

    2023-12-12 23:36:01       61 阅读
  5. Oracle中的模糊查询

    2023-12-12 23:36:01       26 阅读
  6. MySQL查询条件OR导致模糊查询失效

    2023-12-12 23:36:01       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-12 23:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-12 23:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-12 23:36:01       82 阅读
  4. Python语言-面向对象

    2023-12-12 23:36:01       91 阅读

热门阅读

  1. 如何在Git中忽略DS_Store文件

    2023-12-12 23:36:01       49 阅读
  2. 代码随想录 416. 分割等和子集

    2023-12-12 23:36:01       58 阅读
  3. typescript中常用object方法?

    2023-12-12 23:36:01       54 阅读
  4. 跨站脚本攻击(xss)

    2023-12-12 23:36:01       62 阅读
  5. Python迭代器与生成器研究记录

    2023-12-12 23:36:01       52 阅读
  6. Optional

    2023-12-12 23:36:01       54 阅读