js 本地搜索(多条件)+本地分页
标签搜索
侧边栏壁纸
博主昵称
2c

  • 累计撰写 19 篇文章
  • 累计收到 12 条评论

js 本地搜索(多条件)+本地分页

2c
2c
2022-11-24 / 0 评论 / 6 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年11月29日,已超过510天没有更新,若内容或图片失效,请留言反馈。
 let arr = [{
                        name: "q1121",
                        age: 12,
                        address: "辽宁大连1"
                    },
                    {
                        name: "q44",
                        age: 12,
                        address: "辽宁大连4"
                    },
                    {
                        name: "q55",
                        age: 12,
                        address: "辽宁大连5"
                    },
                    {
                        name: "q222",
                        age: 13,
                        address: "辽宁大连2"
                    },
                    {
                        name: "q332",
                        age: 12,
                        address: "辽宁沈阳1"
                    }
                ];
                //筛选条件
                let filter = {
                    name: "",
                    age: 12,
                    address: "辽宁",
                };
                //拿到有值的参数
                let tempFilter = {};
                for(key in filter) {
                    if(typeof(filter[key]) != "undefined" && typeof(filter[key]) != "null" && filter[key] != null && filter[key] != "") {
                        tempFilter[key] = filter[key];
                    }
                }
                //筛选
                let resultArr = arr.filter(
                    (item) => {
                        let flag = false;
                        for(key in tempFilter) {
                            if(item[key].toString().indexOf(tempFilter[key].toString()) >= 0) {
                                flag = true;
                            } else {
                                flag = false;
                                break;
                            }
                        }
                        if(flag) {
                            return item;
                        }
                    }
                );
                console.log(JSON.stringify(resultArr));
                //[
                //    {"name":"q1121","age":12,"address":"辽宁大连1"},
                //    {"name":"q44","age":12,"address":"辽宁大连4"},
                //    {"name":"q55","age":12,"address":"辽宁大连5"},
                //    {"name":"q332","age":12,"address":"辽宁沈阳1"}
                //]
            }

分页

pagination: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 30, 50, 100],
      },


------------

tableList: function () {
      return this.tableData.slice(
        (this.pagination.currentPage - 1) * this.pagination.pageSize,
        this.pagination.currentPage * this.pagination.pageSize
      );
    },

原文章

0

评论 (0)

取消