samedi 12 janvier 2019

Filter array of Json Objects using selected checkboxes - React

I am new to react. I have an array of Json Objects and I want to filter this array based on the selected checkboxes. I have multiple checkboxes based on City, Language and Topic. Please refer the screenshot belowenter image description here

Here is my code for the page shown above. How do I implement this functionality? I want that if someone selects a checkbox then data gets filtered and if deselects that then also data gets re-rendered. How do I achieve this?

import React from 'react';
import NavigationBar from './NavigationBar';
import SearchPageResultsStyle from "../assets/css/SearchResultsPage.css"
import Pagination from './Pagination';

class SearchResultsPage  extends React.Component{
    constructor(props) {
        super(props);
        console.log("Printing in the results component: this.props.location.state.data.results")
        console.log(this.props.location.state.data.results)
        this.state = {
            results: this.props.location.state.data.results,
            keyword: this.props.location.state.data.keyword,
            pageOfItems: [],
            city: ''
        };
        this.onChangePage = this.onChangePage.bind(this);
    }

    onChangePage(pageOfItems) {
        // update local state with new page of items
        this.setState({pageOfItems});
    }

    handleCityDelhi(event) {
        this.setState({city: event.target.name})

    }
    render() {

        const renderItems = this.state.pageOfItems.map((item, index) => {
            return (
                <div>
                    <h3 style= key={index}>{item.text}</h3>
                    <a href={'https://google.com'} key={index}>{item.tweetUrl}</a>
                    <br/>
                    <p><span style= key={index}>topic: </span>{item.topic}</p>
                    <p><span style= key={index}>city: </span>{item.city}</p>
                    <p><span style= key={index}>lang: </span>{item.lang}</p>
                    <p><span style= key={index}>Hashtags: </span></p>
                    <hr/>
                </div>
            )
        })
        return (
            <div>
                <NavigationBar/>
                <h4 style=>Showing search results for <span style=>'{this.state.keyword}'</span></h4>
                <hr/>
                <div className={'wrap'} style={SearchPageResultsStyle}>
                    <div className={'fleft'}>
                        <h4>Topics</h4>
                        <input name={'Crime'} type={'checkbox'} /> Crime
                        <hr/>
                        <h4>City</h4>
                        <input name={'Delhi'} type={'checkbox'} onChange={this.handleCityDelhi}/> Delhi
                        <hr/>
                        <h4>Language</h4>
                        <input name={'Hindi'} type={'checkbox'}/> Hindi
                        <hr/>
                    </div>
                    <div className={'fcenter'}>
                        {renderItems}
                        <Pagination items={this.state.results} onChangePage={this.onChangePage}/>
                    </div>
                    <div className={'fright'}></div>
                </div>
            </div>
        )
    }

}

export default SearchResultsPage;



Aucun commentaire:

Enregistrer un commentaire