1
votes

I have a table with dynamic data being displayed on it if the user click any of the table rows a modal pops up and shows some more details about the row clicked, on the modal the user has two options, one to see the full details which redirects him to a page with the full details of the row clicked and the other to hide the row.

I tried using the filter method on the data fetched from the API but I wasn't successful how will I go along about implementing this functionality.Any help would be very much appreciated.

Here is the code i used :

import React, { useEffect, useState } from 'react';
import axios from 'axios';
import BootStrapTable from "react-bootstrap-table-next"
import paginationFactory from 'react-bootstrap-table2-paginator';
import {Modal , Button} from "react-bootstrap"
import { useHistory } from "react-router-dom";
import Footer from "./Footer"

export default function DataTable (){
    const history = useHistory();
    const [companies,setCompanies] = useState([]);
    const [modalInfo,setModalInfo] = useState([]);
    // eslint-disable-next-line
    const [showModal,setShowModal] = useState(false);
    const [show ,setShow] = useState(false);
    const handleCLose = () => setShow(false)
    const handleShow = () => setShow(true)
    const getCompaniesData = async () =>{
        try{
            const data = await axios.get("http://localhost:5000/companies")
            setCompanies(data.data)
        }
        catch(e){
            console.log(e)
        }
    }
    useEffect(()=>{
        getCompaniesData();
    },[])
    const columns = [
        {dataField:"id",text:"id"},
        {dataField:"name",text:"name"},
     
    ]
    const rowEvents = {
        onClick : (e,row)=>{
            console.log(row)
            setModalInfo(row)
            toggleTrueFalse()
            
        }
    }
    const toggleTrueFalse = () =>{
        setShowModal(handleShow);
    }
    const handleDetails = () =>{
        history.replace('/details', {details:modalInfo})
    }

 function handleHide (mid) {
    return companies.filter((item) => {
      return item.id === mid;
    });
  };

    const ModalContent = () =>{
     return (   <Modal show={show} onHide={handleCLose}>
<Modal.Header closeButton>
    <Modal.Title>
        
  {modalInfo.name}
    </Modal.Title>
</Modal.Header>
<Modal.Body>
<h1 >Company Details :</h1>
<ul>
     <ol>source_id : {modalInfo.source_id}</ol>
     <ol>source_name : {modalInfo.source_name}</ol>
     <ol>name : {modalInfo.name}</ol>
     <ol>city : {modalInfo.city}</ol>
     <ol>country : {modalInfo.country}</ol>
</ul>
</Modal.Body>
<Modal.Footer>

    <Button variant="secondary" onClick={handleDetails}>Full Details</Button>
    <Button className="btn btn-danger" variant="secondary" onClick={handleHide(modalInfo.id)}>Hide Element</Button>
</Modal.Footer>
        </Modal> )
    }
    return (
        <div>
        <h1 className="text-center">Share-work Data Table</h1>
        <div className="table-back">
        <BootStrapTable
        keyField="id"
        data={companies}
        columns={columns}
        pagination={paginationFactory()}
        rowEvents = {rowEvents}
        />
          <Footer/>
        </div>
       
        {show ? <ModalContent/> : null}
      
        </div>
    )
}

1

1 Answers

1
votes

You need to actually update the state.

Instead of this:

 function handleHide (mid) {
    return companies.filter((item) => {
      return item.id === mid;
    });
  };

update the state with the setCompanies function you defined above (const [companies,setCompanies] = useState([]);)

This is what you code should look like instead:

 function handleHide (mid) {
    setCompanies(companies.filter((item) => {
      return item.id === mid;
    }));
  };