【问题标题】:Need a Advanced Search Filter - React native需要一个高级搜索过滤器 - React Native
【发布时间】:2020-04-19 16:46:24
【问题描述】:

我有一个 SearchBar 和一个简单的 FlatList。我的搜索过滤器逻辑工作正常,但对于简单搜索来说没问题。

例子: 如果我的 FlatList 包含三个项目

  1. 参观时间。
  2. 他知道参观时间。
  3. 访问和时间。

如果我在我的 SearchBar 中搜索 "visit h",它只会呈现 item 2 -> 2。他知道参观时间。

我希望它在我搜索 “visit h”时呈现所有 3 个项目 我的搜索过滤器应该查找包括“访问”和“h”的每个项目。 “访问”中还应包括“访问”

如何捕获这种类型的过滤器?有没有可以使用的js库?执行此搜索过滤器的任何有效方法?

我当前的代码如下:

export default class HomeComponent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            isLoading: true,
            notifications: [],
            query: '',
            temp: []
        }
    };

    componentDidMount() {
        fetch('https://140xya6a67.execute-api.ap-southeast-1.amazonaws.com/dev/', {
            method: 'GET',
        })
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    isLoading: false,
                    notifications: responseJson,
                    notificationRead: false,
                    temp: responseJson,
                })
            })
    };

    goToDetails() {
        return this.props.navigation.navigate(Details);
    }

    renderItem({item}) {
        return <NotificationItem item={item}
                    onPress={() => {this.goToDetails()}}/>
    }

    handleSearch(text) {
        const newData = _.filter(this.state.temp, (item) => {
            const itemData = item.Desc ? item.Desc.toUpperCase() : ''.toUpperCase();
            const textData = text.toUpperCase();
            return itemData.indexOf(textData) > -1;
        });
        this.setState({
            notifications: newData,
            query: text,
        });
    }

    renderContent() {
        let {notifications} = this.state;

        return (
            <View>
                <SearchBar placeholder='type here...' lightTheme round value={this.state.query}
                           onChangeText={(text) => {this.handleSearch(text)}}/>
                <FlatList
                    keyExtractor={(item, id) => item.id}
                    data={notifications}
                    renderItem={this.renderItem.bind(this)}
                />
            </View>
        );
    }

    render() {
        let {fill, container} = styles;

        return (
            <View style={[fill, container]}>
                {this.renderContent()}
            </View>
        );
    }
}

【问题讨论】:

    标签: javascript reactjs react-native search react-native-flatlist


    【解决方案1】:

    您正在描述全文搜索,甚至是模糊搜索。

    如果您有一小部分静态数据,您可以在运行时使用 fuse.js 之类的库纯粹在前端进行。

    对于动态数据,您需要一个后端来预先标记和准备数据。然后前端只发送用户输入的内容,并从后端获取搜索结果。

    如果您使用像 PostgreSQLMSSQL 这样的现代 RDBMS,您可以自己构建它。或者使用专门针对此问题构建的服务或工具,例如 algoliaElasticsearch

    【讨论】:

    • Fuse.js 谢谢你
    【解决方案2】:

    您可以编写自己的简单过滤器,如下所示(未测试):

    handleSearch(text) {
      if (!text || text.length === 0) {
        this.setState({
          notifications: this.state.temp,
          query: text,
        });
        return;
      }
    
      const newData = _.filter(this.state.temp, item => {
        const itemData = item.Desc ? item.Desc.toUpperCase() : ''.toUpperCase();
        const textParts = text.toUpperCase().split(' ');
    
        let shouldInclude = true;
        for (let i = 0; i < textParts.length; i++) {
          const part = textParts[i];
          shouldInclude = shouldInclude && (itemData.indexOf(part) > -1);
    
          if (!shouldInclude) {
            return false;
          }
        }
        return true;
      });
      this.setState({
        notifications: newData,
        query: text,
      });
    }
    

    【讨论】:

    • 嗨拉维,你给我的搜索逻辑工作正常。我需要添加一个我无法添加的要求。我有一个包含 2 个项目的平面列表:她的狗,我的猫。如果我搜索“狗”,则会出现第一项。所以是正确的。如果我搜索“M Ca”,则会出现第二个项目。所以是正确的。现在,如果我输入“Do Ca”,我希望这两个项目都出现。我如何在你的逻辑中实现这种类型的过滤???请帮忙
    • 更像是扩展搜索逻辑。
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多