I am trying to implement a simple onCellClick listener on Material UI table.
In earlier versions of it, a function in the table scope onCellClick used to give the row and column number where click was made as shown here
Currently when this function is placed -
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '#material-ui/core/styles';
import Table from '#material-ui/core/Table';
import TableBody from '#material-ui/core/TableBody';
import TableCell from '#material-ui/core/TableCell';
import TableHead from '#material-ui/core/TableHead';
import TableRow from '#material-ui/core/TableRow';
import Paper from '#material-ui/core/Paper';
const styles = {
}
let id = 0;
function createData(name, calories, fat, carbs, protein) {
id += 1;
return { id, name, calories, fat, carbs, protein };
}
const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];
function SimpleTable(props) {
const { classes } = props;
return (
<Paper className={classes.root}>
<Table className={classes.table}
onCellClick={(rowNumber,
columnId) =>
console.log(rowNumber,columnId)}>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell numeric>Calories</TableCell>
<TableCell numeric>Fat (g)</TableCell>
<TableCell numeric>Carbs (g)</TableCell>
<TableCell numeric>Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => {
return (
<TableRow key={row.id}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell numeric>{row.calories}</TableCell>
<TableCell numeric>{row.fat}</TableCell>
<TableCell numeric>{row.carbs}</TableCell>
<TableCell numeric>{row.protein}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</Paper>
);
}
export default withStyles(styles)(SimpleTable);
it throws the error Unknown event handler property onCellClick. It will be ignored.This is expected because unlike previous versions no onCellClick function is passed in the table source code.
How to achieve onCellClick functionality now in Material-UI?
Any help will be highly appreciated.
A cell can be a th or td element (see the source code) or a custom element that you pass through the component property.
Each of these will have support for the onClick property.
So you need something like this:
handleClick = (id, column) => {
return (event) => {
console.log(`You clicked on row with id ${id}, in column ${column}.`);
}
}
render() {
return (
// ...
{this.state.rows.map((row) => (
<TableRow key={row.id}>
<TableCell onClick={this.handleClick(row.id, "calories")}>
{row.calories}
</TableCell>
// ...
<TableCell onClick={this.handleClick(row.id, "protein")}>
{row.protein}
</TableCell>
</TableRow>
)}
// ...
);
}
Related
how to remove the shadow from the table? tried to find the api but couldn't find the right property. took an example from the library they all come with a shadow. I don't understand how I can remove it.
shadow below
https://codesandbox.io/s/fchdj1?file=/demo.tsx
import * as React from 'react';
import Table from '#mui/material/Table';
import TableBody from '#mui/material/TableBody';
import TableCell from '#mui/material/TableCell';
import TableContainer from '#mui/material/TableContainer';
import TableHead from '#mui/material/TableHead';
import TableRow from '#mui/material/TableRow';
import Paper from '#mui/material/Paper';
function createData(
name: string,
calories: number,
fat: number,
carbs: number,
protein: number,
) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];
export default function DenseTable() {
return (
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} size="small" aria-label="a dense table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow
key={row.name}
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
The problem is not with the table component, it's the paper it has a default shadow .
You're using TableContainer with paper , just remove the component={Paper} it'll be set to div by default.
Table Screenshot
I'm creating a table where the data will be fetched from api and displayed in the mui table or material table. I wanna show different action button to different rows like in the first row the button should display "connect" when I click on that connect it should be changed into "disconnect" the problem here is it changes all the existing "connect" button into disconnect and again when the disconnect is clicked the button should change into connect. I want this behaviour to apply for that particular row alone not the entire table. How can I achieve this?
You should extent the array of data that you use on the Mui table by adding an extra fields for each row. One for saving the status ("connect" or "disconnect") and other for recognizing the row that must change.
After that you will be able to call the onClick event of the Button component and will affect only the target row.
Taking the example from Mui page on codesandbox, I added the "id" and "isConnected" prop to the data array. You can see the modified version in this link and also bellow:
function createData(id, name, calories, fat, carbs, protein, isConnected) {
return { id, name, calories, fat, carbs, protein, isConnected };
}
const data = [
createData(1, "Frozen yoghurt", 159, 6.0, 24, 4.0, true),
createData(2, "Ice cream sandwich", 237, 9.0, 37, 4.3, false),
createData(3, "Eclair", 262, 16.0, 24, 6.0, false),
createData(4, "Cupcake", 305, 3.7, 67, 4.3, false),
createData(5, "Gingerbread", 356, 16.0, 49, 3.9, false)
];
export default function BasicTable() {
const [rows, setRows] = React.useState(data);
const handleChangeConnect = (id) => {
console.log("The id is ", id);
setRows(
rows.map((row) => {
if (row.id == id) {
return { ...row, isConnected: !row.isConnected };
} else return { ...row };
})
);
};
return (
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
<TableCell align="right">Is connected</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow
key={row.name}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
<TableCell align="right">
<Button
variant="contained"
color={row.isConnected ? "primary" : "secondary"}
onClick={() => {
handleChangeConnect(row.id);
}}
>
{row.isConnected ? "disconnect" : "connect"}
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
```l
Instead of using var for determining if it is connect or disconnect you need to use associative array for determine that example
if(check[index]==true){
<button onClick={handlechange}>Disconnect </button>
}
else {
<button onClick={handlechange}>connect </button>}
just started learning react. I dont know many basic things so here we go.
I am trying to implement an example of a table found on react material-ui.
Got the following code:
import { makeStyles } from '#material-ui/core/styles';
import Table from '#material-ui/core/Table';
import TableBody from '#material-ui/core/TableBody';
import TableCell from '#material-ui/core/TableCell';
import TableContainer from '#material-ui/core/TableContainer';
import TableHead from '#material-ui/core/TableHead';
import TableRow from '#material-ui/core/TableRow';
import Paper from '#material-ui/core/Paper';
const useStyles = makeStyles({
table: {
minWidth: 650,
},
});
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];
export default function BasicTable() {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
and then i got the following error :
Can someone please tell me why is that?
UPDATE:I had to update both react and react-dom to latest version
I copied your code, added import React from 'react' , and all it worked
How can I put two color backgrounds on two different lines?
I managed to set it up on a row before click on a table row and I would like to put a second backgroundcolor on another row when a condition is met
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import {TableBody, Table, TableCell, TableContainer, TableHead, TableRow, Paper} from "#material-ui/core";
import { useState } from "react";
const useStyles = makeStyles({
table: {
minWidth: 650
},
tableRow: {
"&$selected, &$selected:hover": {
backgroundColor: "#E8E8E8"
}
},
hover: {},
selected: {}
});
function createData(name, calories, fat, carbs, protein) { return { name, calories, fat, carbs, protein }; }
const rows = [ createData("Frozen yoghurt", 159, 6.0, 24, 4.0), createData("Ice cream sandwich", 237, 9.0, 37, 4.3), createData("Eclair", 262, 16.0, 24, 6.0), createData("Cupcake", 305, 3.7, 67, 4.3), createData("Gingerbread", 356, 16.0, 49, 3.9) ];
export default function SimpleTable() {
const classes = useStyles();
const [selectedIndex, setSelectedIndex] = useState(null);
const val = false;
const secondRow = 0;
const selectRow = (i) => {
setSelectedIndex(i);
if (val) {
// put a backgroundcolor on line 1
}
};
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, index) => (
<TableRow hover key={row.name} onClick={() => {selectRow(index);}} selected=selectedIndex === index} classes={{ hover: classes.hover, selected: classes.selected }} className={classes.tableRow} >
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
https://codesandbox.io/s/material-demo-forked-5u4jz?file=/demo.js
You can assign an array as your state for it to contain the selected indices. In the example below, I used shift on the array to retain the latest selection, remove the oldest one, and push the new one.
const [selectedIndex, setSelectedIndex] = useState([]);
const selectRow = (i) => {
// checks to see if it's already selected
if (selectedIndex.includes(i)) {
return;
}
let newSelected = [...selectedIndex];
// sample logic for the "only 2 rows selected" requirement
if (newSelected.length === 2) {
newSelected.shift();
newSelected.push(i);
} else {
newSelected.push(i);
}
setSelectedIndex(newSelected);
};
<TableRow
onClick={() => {
selectRow(index);
}}
selected={selectedIndex.includes(index)}
>
https://codesandbox.io/s/material-demo-forked-h5i77?file=/demo.js
I have a table created with ReactJs & Material Ui like the code below.
Her a live test: https://codesandbox.io/s/material-demo-692nz?file=/demo.js
I want to reduce the space between columns, there is always big space after first column no matter how many columns i add after it. It's like they have some 'space-between' style but cannot remove it.
Any tips ? thank you
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import Table from "#material-ui/core/Table";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableContainer from "#material-ui/core/TableContainer";
import TableHead from "#material-ui/core/TableHead";
import TableRow from "#material-ui/core/TableRow";
import Paper from "#material-ui/core/Paper";
const useStyles = makeStyles({
table: {
minWidth: 50
}
});
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3),
createData("Eclair", 262, 16.0, 24, 6.0),
createData("Cupcake", 305, 3.7, 67, 4.3),
createData("Gingerbread", 356, 16.0, 49, 3.9)
];
export default function SimpleTable() {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell align="left">Dessert (100g serving)</TableCell>
<TableCell align="left">Calories</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="left">{row.calories}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
Using inline-style, add width to <TableCell /> under <TableHead /> would be fine
<TableCell align="left" style={{width: 200}}>Dessert (100g serving)</TableCell>
Update:
There are multiple ways to define the width style
style={{width: '20vw'}}
or
style={{minWidth: 200}}
may work for the style requests.