美文网首页基础前端
纯前端实现模糊搜索

纯前端实现模糊搜索

作者: CondorHero | 来源:发表于2020-07-20 02:08 被阅读0次
纯前端实现模糊搜索

匆匆完成,待整理。。。

一、前置知识

模糊查询一般都是后台来做的,我们只需要调用接口,而且后台做这个是非常的方便,只需要写下 Mysql 语句,检索下数据库就行了。接下来我们使用纯前端来实现下,技术框架 React。

首先我们需要的数据格式如下:

data: [
    { 
        key: 6028.069991021317,
        areaCode: 6028.069991021317,
        areaName: "四川省",
        areaPinyin: "sichuansheng",
        presentState: "未完成"
    }
];

如何制造出这个假数据呢,我是通过 mockjs 来生成的,然后通过 pinyin 把中文专成英文,包的版本如下:

"mockjs": "1.1.0",
"pinyin": "2.9.1",

这时候我们书写 data.js 文件,来生成大量的假数据。以下是 data.js 的内容:

import Mock from "mockjs";
import pinyin from "pinyin";
const random = Mock.Random;

// 汉字转拼音
const han2pinyin = (han) => {
    return [].concat(...pinyin(han, {
        // 拼音不加音调
        style: pinyin.STYLE_NORMAL
    })).join("");
};

const data = new Array(100).fill(0).map(item => {
    const province = random.province();
    const number = random.integer(1000, 9999) + Math.random();
    return {
        key: number,
        areaCode: number,
        areaName: province,
        presentState: random.pick(["已完成", "未完成"]),
        areaPinyin: han2pinyin(province)
    };
});
export default data;

现在生成了大量数据,去配置下入口 index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App.js";
import { createStore } from "redux";
import { Provider } from "react-redux";
import "antd/dist/antd.css";
import reducers from "./reducers";

const store = createStore(reducers);

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById("root")
);

引入了 antd 和 简单配置了下 redux,在看下 reducers.js 文件,合并 reducers 用的:

import getForecastList from "./counterStore.js";
import { combineReducers } from "redux";
export default combineReducers({
  getForecastList
});

counterStore.js 一个纯函数:

export default (state = { getForecastList: [] }, action) => {
    if (action.type === "SAVEFORECASTLIST") {
        return {
            getForecastList: action.palyload
        };
    }
    return state;
};

在组件中使用:App.js

import { connect } from "react-redux";
import React, { Component } from "react";
import { Button, Space, Table, Input } from "antd";
import data from "./data";

class App extends Component {
    state = {
        aheadSearch: "",
        getForecastList: data
    };
    forecastSearch(EventName) {
        if (EventName !== "reset") {
            let aheadSearch = this.state.aheadSearch.trim();
            let getForecastList;
            if (this.props.getForecastList.length === 0) {
                this.props.saveForecastList(this.state.getForecastList);
                getForecastList = this.state.getForecastList;
            } else {
                getForecastList = this.props.getForecastList;
            }
            // 中英文搜索
            const isletter = /^[a-zA-Z]+$/.test(aheadSearch);
            if (isletter) {
                getForecastList = getForecastList.filter(({ areaPinyin }) =>
                    aheadSearch.length > areaPinyin.length
                        ? ((areaPinyin = new RegExp(areaPinyin, "gim")),
                            areaPinyin.test(aheadSearch))
                        : ((aheadSearch = new RegExp(aheadSearch, "gim")),
                            aheadSearch.test(areaPinyin))
                );
            } else {
                getForecastList = getForecastList.filter(({ areaName }) =>
                    aheadSearch.length > areaName.length
                        ? aheadSearch.includes(areaName)
                        : areaName.includes(aheadSearch)
                );
            }
            this.setState({ getForecastList });
        } else {
            this.setState({
                aheadSearch: "",
                getForecastList: this.props.getForecastList
            });
        }
    }
    handleChange(EventName, e) {
        if (e && e.target) {
            this.setState({
                [EventName]: e.target.value
            });
        } else {
            this.setState({
                [EventName]: e
            });
        }
    }
    getForecastCol() {
        const columns = [
            {
                title: "销区",
                dataIndex: "areaName",
                align: "center"
            },
            {
                title: "提报状态",
                dataIndex: "presentState",
                align: "center"
            }
        ];
        return columns;
    }
    render() {
        return (
            <div>
                <h1>中英文忽略大小写搜索🔍</h1>
                <Space>
                    销区:{" "}
                    <Input
                        value={this.state.aheadSearch}
                        onChange={this.handleChange.bind(this, "aheadSearch")}
                    />
                    <Button onClick={this.forecastSearch.bind(this, "search")}>
                        查询
          </Button>
                    <Button onClick={this.forecastSearch.bind(this, "reset")}>
                        重置
          </Button>
                </Space>
                <Table
                    columns={this.getForecastCol()}
                    dataSource={this.state.getForecastList}
                    scroll={{ y: 200 }}
                    order
                    pagination={false}
                />
            </div>
        );
    }
}

export default connect(
    ({ getForecastList: { getForecastList } }) => ({
        getForecastList: getForecastList
    }),
    dispatch => ({
        saveForecastList(list) {
            dispatch({ type: "SAVEFORECASTLIST", palyload: list });
        }
    })
)(App);

当前时间 Monday, July 20, 2020 02:08:24

相关文章

  • 纯前端实现模糊搜索

    匆匆完成,待整理。。。 一、前置知识 模糊查询一般都是后台来做的,我们只需要调用接口,而且后台做这个是非常的方便,...

  • js 实现本地模糊搜索

    前端实现模糊搜索 indexOf、split 、match、test indexOf方法 语法:stringObj...

  • js实现模糊查询纯前端

    今天为大家分享js实现模糊查询的功能! (如果解决了您的问题,请帮忙给我点个赞吧,谢谢!!!) 话不多说直接上代码...

  • 前端js实现本地模糊搜索

    很多时候我们做模糊查询是传关键字给后台请求后台接口,但是有时候一些轻量级的列表前端来做可以减少ajax请求,在一定...

  • 前端js模糊搜索(模糊查询)

    1.html结构: 查询结果放ul里面 2.css样式: 查询结果对应的显示框css 列表都是li组成css 这个...

  • 关于ES6的find

    在项目中,一些没分页的列表的搜索功能由前端来实现,搜索一般分为精确搜索和模糊搜索。搜索也要叫过滤,一般用filte...

  • 前端模糊查询

    实现前端模糊查询效果 实验证明 这种方法可以达到模糊查询的效果,但是把搜索的字删了,数据也就没了,不能复原,应该要...

  • 前端模糊搜索实现与深拷贝cloneDeep

    对获取到的原始数组数据进行深拷贝,以免改变原始数组结构;通过输入值匹配数组对象值来重组匹配出来的数组。 1.安装l...

  • web前端特效演示合集

    前端特效01:使用纯CSS实现书籍3D翻页效果 前端特效02:使用纯CSS实现动态太极 前端特效03:HTML5画...

  • Elasticsearch 实现模糊搜索

    1、match query实现模糊搜索,该方式会对匹配文本进行分词然后匹配分词后的每个词项,匹配操作有OR和AND...

网友评论

    本文标题:纯前端实现模糊搜索

    本文链接:https://www.haomeiwen.com/subject/lnztqktx.html