【问题标题】:how search from array of object? and sort data by ascending and descending by click on column name?如何从对象数组中搜索?并通过单击列名按升序和降序对数据进行排序?
【发布时间】:2020-11-09 00:53:21
【问题描述】:

我是 Reactjs 的初学者,我从对象数组创建数组并创建表现在我想对创建的表执行搜索操作。我尝试了很多,但我无法得到我应该在 updateSearch() 函数中编写的内容来从表中搜索并显示搜索结果,以及 通过单击列名通过升序和降序对数据进行排序。所以,请帮我解决这个问题

    class Hello extends Component {
    constructor(props) {
        super(props)
    
        this.state = {
          search: '',
            Data:[
                {
                    id: 1,
                    fullName: 'abc',
                    email:'example@gmail.com',
                    
                },
                {
                    id: 2,
                    fullName: 'qps',
                    email:'qps@gmail.com',
                    
                },
                {
                    id: 3,
                    fullName: 'qwe',
                    email:'qwe@gmail.com',
                    
                },
             ]
        }
    }
    updateSearch(event){
        
        this.setState({
            search : event.target.value
        });
        console.log(event.target.value);
    } 
   
        render() {
            return (
                
                <div>
                    <h1>welcome to React</h1>
                    <input type="text" placeholder="Enter item to be searched"  value={this.state.search} onChange={this.updateSearch.bind(this)} />
                    <table className="table table-hover table-dark">
                    
                    <tbody>
                        <tr>
                            <th>ID</th>
                            <th>Full Name</th>
                            <th>Email</th>
                        </tr>
                    {
                       this.state.Data.map((item,index)=>(
                        <tr key={item.id}>
                            
                            <td >{item.id}</td>
                        
                            <td >{item.fullName}</td>
                            <td>{item.email}</td>
                        </tr>
                    ))
                    }
                    </tbody>
                    </table>
    
                </div>
            )
        }
    }
    export default Hello

【问题讨论】:

  • 您要搜索哪个属性? idfullNameemail?
  • 您一次只需要问一个问题。

标签: reactjs


【解决方案1】:

您可能正在尝试在组件中添加过滤器功能

class App extends Component {
  Data = [
      {
        id: 1,
        fullName: 'abc',
        email: 'example@gmail.com',

      },
      {
        id: 2,
        fullName: 'qps',
        email: 'qps@gmail.com',

      },
      {
        id: 3,
        fullName: 'qwe',
        email: 'qwe@gmail.com',

      },
    ];
  constructor(props) {
    super(props)
    this.state = {
      search: '',
      filteredArray: this.Data,

    }
  }
  updateSearch(event) {

    this.setState({
      search: event.target.value,
      filteredArray: this.Data.filter((data) => {
        return data.fullName.includes(event.target.value);
      })
    });

    console.log(event.target.value);
  }
  render() {
    return (

      <div>
        <h1>welcome to React</h1>
        <input type="text" placeholder="Enter item to be searched" value={this.state.search} onChange={this.updateSearch.bind(this)} />
        <table className="table table-hover table-dark">

          <tbody>
            <tr>
              <th>ID</th>
              <th>Full Name</th>
              <th>Email</th>
            </tr>
            {
              this.state.filteredArray.map((item, index) => (
                <tr key={item.id}>

                  <td >{item.id}</td>

                  <td >{item.fullName}</td>
                  <td>{item.email}</td>
                </tr>
              ))
            }
          </tbody>
        </table>

      </div>
    )
  }
}

【讨论】:

    【解决方案2】:

    您可以过滤Data.map中的项目,例如:

    { this.state.Data.filter((item) => {
        return !(this.state.search) // if search is not set, return all items
              || item.fullName.match(RegExp(search, 'i'))
              || item.email.match(RegExp(search, 'i'))
    }).map((item,index)=>(
        <tr key={item.id}>                        
            <td >{item.id}</td>
            <td >{item.fullName}</td>
            <td>{item.email}</td>
        </tr>
    )}
    

    工作代码

    class Hello extends React.Component {
      constructor(props) {
        super(props)
    
        this.state = {
          search: '',
          Data: [{
              id: 1,
              fullName: 'abc',
              email: 'example@gmail.com',
    
            },
            {
              id: 2,
              fullName: 'qps',
              email: 'qps@gmail.com',
    
            },
            {
              id: 3,
              fullName: 'qwe',
              email: 'qwe@gmail.com',
    
            },
          ]
        }
      }
      updateSearch(event) {
        this.setState({
          search: event.target.value
        });
      }
      render() {
         return (
             <div>
              <h1>welcome to React</h1>
              <input type="text" placeholder="Enter item to be searched"  value={this.state.search} onChange={this.updateSearch.bind(this)} />
              <table className="table table-hover table-dark">
              <tbody>
                  <tr>
                      <th>ID</th>
                      <th>Full Name</th>
                      <th>Email</th>
                  </tr>
                  { 
                  this.state.Data.filter((item) => {
                return !(this.state.search) // if search is not set, return all items
                        || item.fullName.match(RegExp(this.state.search, 'i'))
                        || item.email.match(RegExp(this.state.search, 'i'))
              }).map((item,index)=>(
                <tr key={item.id}>                        
                    <td >{item.id}</td>
                    <td >{item.fullName}</td>
                    <td>{item.email}</td>
                </tr>
              ))}
              </tbody>
              </table>
    
          </div>
    
         )
      }
    }
    
    ReactDOM.render( < Hello / > , document.getElementById('root'));
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <div id="root">
    </div>

    【讨论】:

    • 你好勇敢的大师,你的解决方案对我有用,但有一个问题,它搜索时区分大小写,让我知道我们如何使它不区分大小写
    猜你喜欢
    • 2019-11-10
    • 2018-05-21
    • 2015-08-30
    • 2020-06-13
    • 2019-12-07
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多