简体   繁体   中英

React data table component, passing info for clicked row into modal

I'm making an offline PWA with ReactJS and I've started using the react-data-table-component, which has been great so far.

I've set the table to have an onRowClicked function that is called anytime a row is clicked, and so far, I only have that function opening a modal upon click which does indeed work.

My issue is that I want the modal to contain information from the row elements for the row that was clicked. So basically, if I click the first row that has the title "Hello There" (element.title) I want the modal to also contain the elements for that row.

This code shows everything I have, including how I'm mapping my documents from PouchDB into the elements object , which is what informs the rows in my data table.

What do I need to do in order to pass the info for the clicked row into the modal?

import React, { useState,useMemo} from 'react';
import { Column, Row } from 'simple-flexbox';
import { StyleSheet, css } from 'aphrodite/no-important';

const columns = [
  {
    name: 'Title',
    sortable: true,
    cell: row => (<div>{row.title}</div>),
  },
  {
    name: 'Date of Entry',
    selector: 'createdAt',
    sortable: true,
    right: true,
  },
];

class MedJournalComponent extends React.Component {

    constructor(props){
      super(props);
      this.state = {
        loading: true,
        elements: null,
        notebookDisplayOpen: false
      };

      this.fetchData.bind(this);
    }

    componentDidMount(){
      this.fetchData();
    }

    fetchData = () => {
      this.setState({
          loading: true,
          elements: null,
      });
      this.props.notes.db.allDocs({
          include_docs: true,
      }).then(result => {
          const rows = result.rows;
          this.setState({
              loading:false,
              elements: rows.map(row => row.doc),
          });
      }).catch((err) =>{
          console.log(err);
      });
    }

    notebookEntryHandler = () => {
      this.setState({notebookDisplayOpen: true});
    }

    closeNotebookEntry = () => {
      this.setState({notebookDisplayOpen: false});
    }

    render() {

        const { notebookDisplayOpen } = this.state;

        if (this.state.loading || this.state.elements === null) {
            return `loading...`;
        }
        return (
            <Column>

                <Modal open={notebookDisplayOpen} onClose={this.closeNotebookEntry}>
                    <div>Test</div>
                </Modal>

                <DataTable
                    title="Notebook Entries"
                    columns={columns}
                    data={this.state.elements}
                    theme="solarized"
                    selectableRows 
                    onRowClicked={this.notebookEntryHandler}
                    Clicked
                    Selected={handleChange}
                />

            </Column>
        );
    }
}

export default MedJournalComponent;

change:

notebookEntryHandler = () => {
  this.setState({notebookDisplayOpen: true});
}

to:

notebookEntryHandler = row => {
  this.setState({notebookDisplayOpen: true});
}

row represents the clicked row, you can store it in state and use it in the modal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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