nour
nour

Reputation: 163

React js material ui core table get data from row on click

I have found a code for a material table that accepts a list as input and applies pagination, sorting and filtering on it. The thing is I need to find a way to extract the data from the row onClick and redirect the page to a new route along with those data. How can I do that?

In the component, I call the table as follows:

export default function ViewAllUsers() {
     const [filterFn, setFilterFn] = useState({ fn: items => { return items; } })
     const records = ....//List of records
     const {
        TblContainer,
        TblHead,
        TblPagination,
        recordsAfterPagingAndSorting
    } = useTable(records, headCells, filterFn);

    const handleSearch = e => {
        let target = e.target;
        //Handle search
    }

return (
        <>
            <Paper className={classes.pageContent}>
                <Toolbar>
                    <Controls.Input onChange={handleSearch}/>
                </Toolbar>
                <TblContainer>
                    <TblHead />
                    <TableBody>
                        {
                            recordsAfterPagingAndSorting().map(item =>
                            (<TableRow key={item.id}>
                                <TableCell>{item.id}</TableCell>
                                <TableCell>{item.fullName}</TableCell>
                            </TableRow>)
                            )
                        }
                    </TableBody>
                </TblContainer>
                <TblPagination/>
            </Paper>
}

and the useTable hook is:

export default function useTable(records, headCells, filterFn) {
    const pages = [5, 10, 25]
    const [page, setPage] = useState(0)
    const [rowsPerPage, setRowsPerPage] = useState(pages[page])
    const [order, setOrder] = useState()
    const [orderBy, setOrderBy] = useState()

   const TblContainer = props => (
        <Table className={classes.table}>
            {props.children}
        </Table>
    )
    const TblHead = props => {

        const handleSortRequest = cellId => {
           //Some code
        }

        return (<TableHead>
            <TableRow>
                {
                    headCells.map(headCell => (
                        <TableCell key={headCell.id}
                            sortDirection={orderBy === headCell.id ? order : false}>
                            {headCell.disableSorting ? headCell.label :
                                <TableSortLabel
                                    active={orderBy === headCell.id}
                                    direction={orderBy === headCell.id ? order : 'asc'}
                                    onClick={() => { handleSortRequest(headCell.id) }}>
                                    {headCell.label}
                                </TableSortLabel>
                            }
                        </TableCell>))
                }
            </TableRow>
        </TableHead>)
    }

    const TblPagination = () => (
        <TablePagination
            component="div"
            page={page}
            rowsPerPageOptions={pages}
            rowsPerPage={rowsPerPage}
            count={records.length}
            onChangePage={handleChangePage}
            onChangeRowsPerPage={handleChangeRowsPerPage}
            id="TablePagination"
        />
    )

    return {
        TblContainer,
        TblHead,
        TblPagination,
        recordsAfterPagingAndSorting
    }
}

Upvotes: 0

Views: 4257

Answers (1)

Meet Patel
Meet Patel

Reputation: 26

You can simply use an onClick handler to pass the item data through it:

export default function ViewAllUsers() {
     const [filterFn, setFilterFn] = useState({ fn: items => { return items; } })
     const records = ....//List of records
     const {
        TblContainer,
        TblHead,
        TblPagination,
        recordsAfterPagingAndSorting
    } = useTable(records, headCells, filterFn);

    const handleSearch = e => {
        let target = e.target;
        //Handle search
    }

    const handleItemClick = item => {
        //Redirect to new route from here with the item data
    }


    return (
        <>
            <Paper className={classes.pageContent}>
                <Toolbar>
                    <Controls.Input onChange={handleSearch}/>
                </Toolbar>
                <TblContainer>
                    <TblHead />
                    <TableBody>
                        {
                            recordsAfterPagingAndSorting().map(item =>
                            (<TableRow key={item.id} onClick={() => handleItemClick(item)}>
                                <TableCell>{item.id}</TableCell>
                                <TableCell>{item.fullName}</TableCell>
                            </TableRow>)
                            )
                        }
                    </TableBody>
                </TblContainer>
                <TblPagination/>
            </Paper>
         </>
      )
}

Upvotes: 1

Related Questions