How to apply filter on table contents in react

2019-09-14 18:27发布

问题:

import React, { Component } from 'react';

import {Doughnut} from "react-chartjs-2"

import {bindActionCreators} from "redux";
import {connect} from 'react-redux';

import {fetchBeacons} from '../actions/';

const DummyDoughnutData = {
  labels: ['beacon 1 - zone a', 'beacon 2 - zone c', 'beacon 3 - zone a', 'beacon 4 - zone b', 'beacon 5 - zone b'],
  datasets: [
    {
      borderColor: 'rgba(255,255,255,.55)',
      data: [ 30, 9, 17, 22, 11],
      backgroundColor: [
		'#063e70',
		'#107bb5',
		'#1A1C1D',
		'#666666',
		'#2F4F4F'

		]
    }
  ],
};


// const beacons=[
//         {zone:"zone a", status: "active", _id:1},
//         {zone:"zone c", status: "active", _id:2},
//         {zone:"zone a", status: "active", _id:3},
//         {zone:"zone b", status: "active", _id:4},
//         {zone:"zone b", status: "active", _id:5},
//         {zone:"zone b", status: "active", _id:6},
//         {zone:"zone c", status: "active", _id:7}
// ];

// class BeaconZoneRow extends Component {
//     render() {
//         return (
//             <tr>
//                 <th colSpan="2">
//                     {this.props.zone}
//                 </th>
//             </tr>
//         )

//     }

// }

class BeaconRow extends Component {
    render() {
        return (
            <tr>
                 <td>{this.props.beacon.name}</td>
                <td>{JSON.stringify(this.props.beacon.status)}</td>
                <td> {this.props.beacon.zone.name} </td>
            </tr>
        )
    }
}

class BeaconList extends Component {

    // Sort = (prop) => { return (a,b) => a[prop].localeCompare(b[prop])};

    render() {
        const rows = [];

        this.props.beacons.map( beacon => {
            return rows.push(<BeaconRow beacon={beacon} key={beacon._id}/>)
        });

        return (
            <div className="col-lg-6">
                <table className="table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Status</th>
                            <th>Zone</th>
                        </tr>
                    </thead>
                    <tbody>
                        {rows}
                    </tbody>
                </table>
            </div>
        )


    }
}

class BeaconDoughnutAnalysis extends Component {
    render() {
        return (
            <div className="col-lg-6">
                <Doughnut data={DummyDoughnutData} />
                <br/>
                <center>visits</center>
            </div>
        )
    }
}


class BeaconListComponent extends Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  handleSubmit(){
    this.props.router.push('/new-beacon');
  }

  componentWillMount = () => {
        this.props.fetchBeacons();
    };


    render() {
        return (
            <div>
                <div className="row">
                    <div className="col-sm-5">
                        <button className="btn btn-sm btn-info" onClick={this.handleSubmit}> Add Beacon</button>
                    </div>
                </div>
                <br/>
                { this.props.beacons && this.props.beacons.length > 0 ?
                    <div className="row">
                        <BeaconList beacons={this.props.beacons}/>
                        <BeaconDoughnutAnalysis/>
                    </div> :
                    <center><h1>...Loading</h1></center>
                }
            </div>
        )
    }
}

function mapStateToProps(state) {
    return {
        beacons: state.beacons
    }
}


function matchDispatchToProps(dispatch){
    return bindActionCreators({fetchBeacons: fetchBeacons}, dispatch);
}

export default connect(mapStateToProps, matchDispatchToProps)(BeaconListComponent);

i had fetched data from the server and i wanted to know how to filter that data in the table using react redux the code is shown above using which table is being displayed and i wanted to filter its contents

回答1:

Assuming you are providing the zone as a prop to the BeaconList component you just need to provide a check while mapping like below

class BeaconList extends Component {

    // Sort = (prop) => { return (a,b) => a[prop].localeCompare(b[prop])};

    render() {
        const rows = [];
        //Assuming you are filtering based on zone and you are giving the zone you want to filter as a prop zone

        this.props.beacons.map( beacon => {
            if(beacon.zone === this.props.zone) {
              return rows.push(<BeaconRow beacon={beacon} key={beacon._id}/>)
            }
        });

        return (
            <div className="col-lg-6">
                <table className="table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Status</th>
                            <th>Zone</th>
                        </tr>
                    </thead>
                    <tbody>
                        {rows}
                    </tbody>
                </table>
            </div>
        )


    }
}

I hope this helps