简体   繁体   English

React.js如何使用多个过滤器和下拉输入

[英]Reactjs how to use multiple filters and dropdown input

So I was able to add a search input to filter the data I receive from the database from help here, Could anybody walk me through how I can go about filtering multiple inputs and displaying on the results that are searched. 因此,我能够添加搜索输入来过滤从此处的帮助中从数据库接收到的数据,有人可以指导我如何过滤多个输入并显示搜索结果。 I want to filter by searchedValue, filterDay, filterCity.. Right now the app is displaying the entire list of items from the database or only what is listed in the searchedValue input. 我想按searchedValue,filterDay,filterCity进行筛选。现在,该应用程序正在显示数据库中项目的整个列表,或者仅显示searchedValue输入中列出的内容。

Also if anybody could send me documentation on how to only get the data from db when searched instead of constantly getting that data on every refresh. 另外,如果有人可以向我发送文档,说明如何仅在搜索时从db获取数据,而不是每次刷新时都不断获取该数据。 I'm sorry if my question is unclear here. 如果我的问题在这里不清楚,我感到抱歉。

Thanks. 谢谢。

import React, { Component } from 'react';
import { Table, Input } from 'reactstrap';
import { connect } from 'react-redux';
import { getItems, deleteItem } from "../actions/itemActions";
import PropTypes from 'prop-types';

class Directory extends Component {

    constructor() {
        super();

        this.state = {
            searchedValue: '',
            filterDay: '',
            filterCity: ''
        };
    }

    componentDidMount() {
        this.props.getItems();
    }

    onDeleteClick = (id) => {
        this.props.deleteItem(id);
    }

    onSearch = e => {
        this.setState({ searchedValue: e.target.value });
    }

    onFilterDay = e => {
        this.setState({ filterDay: e.target.value });
    }

    onFilterCity = e => {
        this.setState({ filterCity: e.target.value });
    }

    render() {
        const { items } = this.props.item;
        const filteredNames = items.filter((item) => item.MeetingName.includes(this.state.searchedValue));

        return(
            <div>
                <Input type="text" placeholder="search" onChange={this.onSearch} value={this.state.searchedValue} />
                <Input type="select" name="day" onChange={this.onFilterDay} value={this.state.filterDay}>
                    <option>SUNDAY</option><option>MONDAY</option><option>TUESDAY</option>
                </Input>
                <Input type="select" name="day" onChange={this.onFilterCity} value={this.state.filterCity}>
                    <option>DANA POINT</option><option>LAGUNA BEACH</option><option>SAN CLEMENTE</option>
                </Input>
                <br/>
                <Table>
                    <thead>
                    <tr>
                        <th>Day</th><th>Time</th><th>Meeting Name</th><th>Address</th><th>City</th><th>Zip Code</th><th></th>
                    </tr>
                    </thead>
                        <tbody>
                        {filteredNames.map(({ _id, Day, Time, MeetingName, Address, City, Zip }) => (
                            <tr key={_id}>
                                <td>{Day}</td><td>{Time}</td><td>{MeetingName}</td><td>{Address}</td><td>{City}</td><td>{Zip}</td>
                                {/*<Button
                                    className="remove-btn"
                                    color="danger"
                                    size="sm"
                                    onClick={this.onDeleteClick.bind(this, _id)}
                                >
                                    &times;
                                </Button>*/}
                            </tr>
                        ))}
                        </tbody>
                    </Table>
            </div>
        );
    }
}

Directory.propTypes = {
    getItems: PropTypes.func.isRequired,
    item: PropTypes.object.isRequired
}

const mapStateToProps = (state) => ({
    item: state.item
})


export default connect(
    mapStateToProps,
    { getItems, deleteItem }
    )(Directory);

I recently did the multi search in my project you can consider this as an example 我最近在我的项目中进行了多次搜索,您可以将其作为示例

   searchBy = () => {
   let searchBy = [];
   {
   searchBy.push(
    ...this.state.originalResponse.filter(item => {
      let searchText =
        this.state.searchText != ""
          ? item.question
              .toLowerCase()
              .search(this.state.searchText.toLowerCase()) !== -1
          : true;
      let category =
        this.state.category != ""
          ? item.type.toLowerCase() === this.state.category.toLowerCase()
          : true;
      let level =
        this.state.level != ""
          ? item.difficulty.toLowerCase() === this.state.level.toLowerCase()
          : true;

      return searchText && category && level;
    })
  );
  }
  console.table(searchBy, ["id", "question", "difficulty", "type"]);
  this.setState({ userArray: searchBy });
  };

https://github.com/AdnanShah/Sawal-Jawab-Game-App/blob/frontend/src/components/QuestionsList/index.js https://github.com/AdnanShah/Sawal-Jawab-Game-App/blob/frontend/src/components/QuestionsList/index.js

This component fetch data from back-end and display the data based on the multiple search.Hope this help 该组件从后端获取数据并基于多重搜索显示数据。希望此帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM