4

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;
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • you can always search in rources ... https://github.com/jbetancur/react-data-table-component/blob/9cd157094a8cf14f59fe53c514a96f73070f0950/src/DataTable/DataTable.js#L131 ... as expected handler gets `row` – xadm Mar 28 '20 at 00:31
  • Wait, I didn't see that anywhere for some reason. So I should be able to keep my onRowClicked line the way it is, but in the function I'm calling it should expect row by default? – Geoff_S Mar 28 '20 at 00:45
  • it's logical, isn't it ? handler without that is unusable, not needed? – xadm Mar 28 '20 at 00:48
  • @xadm I guess my question is how to access it in my function though? I'm a react noob still, so I'm just trying to figure out from that source, how can I access the row in my ```notebookEntryHandler```? – Geoff_S Mar 28 '20 at 01:44
  • 2
    try `notebookEntryHandler = (row, e) => ` ? – xadm Mar 28 '20 at 01:47

1 Answers1

2

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.

StackedQ
  • 3,999
  • 1
  • 27
  • 41
  • 1
    That got it, thanks! This makes more sense now as well after reading the source as @xadm pointed out – Geoff_S Mar 28 '20 at 03:24