Passing an Image in Material-UI Data-Grid Table - reactjs

there. I would like to add an image in a material-ui data-grid table. But it's not working. What am I missing here? I tried to search the docs but couldn't see any example that used images.
My code
import { DataGrid } from '#material-ui/data-grid';
const columns = [
{
field: 'image',
headerName: 'Image',
width: 150,
editable: true,
},
{
field: 'name',
headerName: 'Name',
width: 150,
editable: true,
},
];
const rows = [
{
id: 1,
image: "image URL",
type: 'image',
name: 'Cheese Burger',
price: 10_000,
menu: 'Daily Menu',
category: 'Burgers'
},
{
id: 2,
image: "image URL",
name: 'Diet Coke',
price: 10_000,
menu: 'Daily Menu',
category: 'Driks'
},
];
export const ItemsTable = () => {
return (
<div style={{ height: 300, width: '100%' }} className="mt-7">
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
checkboxSelection
disableSelectionOnClick
/>
</div>
);
}
Desired result
What's I'm getting

The renderCell prop allows you to inject components into the DataGrid, creating a column of custom cells.
https://smartdevpreneur.com/add-buttons-links-and-other-custom-cells-in-material-ui-datagrid/#How_to_Add_Other_Custom_Components
Material-Ui's documentation on renderCell.
https://mui.com/components/data-grid/columns/#render-cell
Just need to pass in the value into the image component src.
I made a codesandbox to show renderCell in action.
https://codesandbox.io/s/rendercellgrid-material-demo-edq5p?file=/demo.js
const columns = [
{
field: 'image',
headerName: 'Image',
width: 150,
editable: true,
renderCell: (params) => <img src={params.value} />, // renderCell will render the component
},
{
field: 'name',
headerName: 'Name',
width: 150,
editable: true,
},
];

Related

How to add MUI DataGrid Edit/delete Button

I am having a hard time adding custom buttons per row in the DataGrid component.
This is my code:
const columns = [
{ field: 'model_id', headerName: 'ID', width: 70, sortable: false },
{ field: 'model_code', headerName: 'Model Code', width: 130 },
{ field: 'model_description', headerName: 'Description', width: 400 },
{ field: 'actions', headerName: 'Actions', width: 400 }
];
DataGrid:
<DataGrid
rows={models}
columns={columns}
getRowId={(row) => row.model_id}
pageSize={5}
rowsPerPageOptions={[5]}
checkboxSelection />
What I want per row:
You can use renderCell function in column definition array like this:
const onButtonClick = (e, row) => {
e.stopPropagation();
//do whatever you want with the row
};
const columns = [
{ field: 'model_id', headerName: 'ID', width: 70, sortable: false },
{ field: 'model_code', headerName: 'Model Code', width: 130 },
{ field: 'model_description', headerName: 'Description', width: 400 },
{ field: 'actions', headerName: 'Actions', width: 400, renderCell: (params) => {
return (
<Button
onClick={(e) => onButtonClick(e, params.row)}
variant="contained"
>
Delete
</Button>
);
} }
];
You can take a look at this sandbox for a live working example of this solution.

How can i hide a column in MUI?

I don't wanna show id field of my table.I use "#mui/x-data-grid":"^5.6.1" version. Here is my code;
import * as React from 'react';
import { DataGrid } from '#mui/x-data-grid';
const columns = [
{ field: 'id', headerName: 'ID', width: 50},
{ field: 'testName', headerName: 'Test Name', width: 120,},
{ field: 'testDate', headerName: 'Test Date', width: 160 },
];
export default function DataTable(props) {
const rows = [id:1, testName:"test", testDate:"23/03/2022"];
return (
<div id="resultTable" >
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
/>
</div>
);
}
Id column can be hidden or display:none. I tried to use
display:false
or:
hidden: true
and also tried:
options: {display: false, filter: false,}
but it wasnt work.
I found the solution.
{ field: 'id', headerName: 'ID', width: 50, hide: true}
This is enough for me.
According to MUI documentation, hide: true is now deprecated.
Instead you should use the Column Visibility Model.
Example from the documentation:
<DataGrid
initialState={{
columns: {
columnVisibilityModel: {
// Hide columns status and traderName, the other columns will remain visible
status: false,
traderName: false,
},
},
}}
/>
Expanding on Robinson's answer from the docs There's also a way to do this programmatically. I used this code to hide some columns on mobile version.
export const MOBILE_COLUMNS = {
id: true,
value: true,
value2: false,
value3: false,
};
export const ALL_COLUMNS = {
id: true,
value: true,
value2: true,
value3: true,
};
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.up("sm"));
const [columnVisible, setColumnVisible] = React.useState(ALL_COLUMNS);
React.useEffect(() => {
const newColumns = matches ? ALL_COLUMNS : MOBILE_COLUMNS;
setColumnVisible(newColumns);
}, [matches]);
return (
<DataGrid
rows={rows}
columns={columns}
columnVisibilityModel={columnVisible}
/>
)
GridColDef.hide prop is deprecated, you should use the column visibility to hide the unwanted columns: https://mui.com/x/react-data-grid/columns/#controlled-visible-columns
I know that this question is already answered, but what I encountered was that I needed to hide a field but retrieve its value once I edit a row, which is also what OP commented on one of the answers.
The solution for this is adding the editable prop to the field like so:
{field: 'id', hide: true, editable: true}
Try removing the first object in columns array like so
const columns = [
//{ field: 'id', headerName: 'ID', width: 50}, //or delete it entirely
{ field: 'testName', headerName: 'Test Name', width: 120,},
{ field: 'testDate', headerName: 'Test Date', width: 160 },
]
or add the property hide: true and editable: true
const columns = [
{ field: 'id', headerName: 'ID', width: 50, hide: true, editable: true},
{ field: 'testName', headerName: 'Test Name', width: 120,},
{ field: 'testDate', headerName: 'Test Date', width: 160 },
]

Unique id property could not be found from Material-UI Datagrid

Error: Material-UI: The data grid component requires all rows to have a unique id property. I am seeing when I call the table.
Here is my code. I know that the _turbineId field is unique in the dataset.
Here is how I have the table defined.
const columns = [
{
field: "siteName",
headerName: "Wind Farm",
width: 150,
editable: true,
},
{
field: "turbineName",
headerName: "Turbine Name",
width: 150,
editable: true,
},
{
field: "controller",
headerName: "Controller Type",
type: "number",
width: 110,
editable: true,
},
{
field: "swVersion",
headerName: "Software Version",
type: "number",
width: 110,
editable: true,
},
{
field: "location",
headerName: "Country",
type: "number",
width: 110,
editable: true,
},
{
field: "_turbineId",
headerName: "Turbine Name",
type: "number",
width: 110,
},
];
Here is how I define the rows
const rows = windFarmData.map((data) => [
{
siteName: data.SiteName,
turbineName: data.TurbineName,
controller: data.Controller,
swVersion: data.SoftwareVersion,
location: data.Country,
_turbineId: data.TechnicalId,
},
]);
Here is the datagrid
export default function DataTable() {
return (
<div style={{ height: 400, width: "100%" }}>
<DataGrid
getRowId={(r) => r._turbineId}
rows={rows}
columns={columns}
pageSize={5}
checkboxSelection
disableSelectionOnClick
/>
</div>
);
}
I wonder if I have missed something or done something wrong.
It works now. I replaced this:
const rows = windFarmData.map((data) => [
{
siteName: data.SiteName,
turbineName: data.TurbineName,
controller: data.Controller,
swVersion: data.SoftwareVersion,
location: data.Country,
_turbineId: data.TechnicalId,
},
]);
with this:
const rows = sortWindFarmData;

How to place Icons Inside a Data Gird (Material UI)? How describe it in Typescript?

This is my Data Gird
import * as React from 'react'
import { DataGrid, GridColDef } from '#material-ui/data-grid'
import { ProductService } from '../services/ProductServices'
const columns: GridColDef[] = [
{ field: 'id', headerName: 'Id', type: 'number', width: 70 },
{ field: 'name', headerName: 'Nome', width: 200 },
{ field: 'description', headerName: 'Descrição', width: 400 },
{ field: 'price', headerName: 'Preço', type: 'number', width: 130 },
{ field: 'quantity', headerName: 'Quantidade', type: 'number', width: 130 },
{ field: 'situation', headerName: 'situação', type: 'number', width: 130 }
]
some code here geting the rows...
export const ProductBox: React.FC = () => {
return (
<div style={{ height: 400, width: '100%', marginTop: 75 }}>
<DataGrid rows={rows} columns={columns} pageSize={5} checkboxSelection />
</div>
)
}
And this is my row Interface:
interface IProduct {
id: number
name: string
price: number
description: string
quantity: number
situation: string
}
How to place a field with icon? And how to say that my icon is an icon for typescript?
I solved my own problem! Found the answer here:
https://material-ui.com/pt/components/data-grid/columns/

Itertating array data into a Material UI table

I am trying to create a table to list all of my users in one go but I am having trouble getting it to display properly.
If I have two arrays to mix in (one of column names and one for where the main array user list should these go inside the return statement? I am stuck at the minute were it is either outputting nothing on a blank screen or throwing this error.
const columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'firstname', headerName: 'First name', width: 130 },
{ field: 'lastName', headerName: 'Last name', width: 130 },
{ field: 'userName', headerName: 'Username', width: 130},
{ field: 'userEmail', headerName: 'Email', width: 180 },
{ field: 'userRole', headerName: 'Role', width: 80}
];
const rows = [
{ id: userLists.userID, firstname: userLists.userFirstName, lastName: userLists.userSurname, userName: userLists.username, userEmail: userLists.userEmail, userRole: userLists.userRoleID },
];
return (
<div style={{ height: 400, width: '100%' }}>
{userLists.map(( {}) => {
return <DataGrid rows={rows} columns={columns} pageSize={10} checkboxSelection />
})}
</div>
);
}
you should map your data this way :
const columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'firstname', headerName: 'First name', width: 130 },
{ field: 'lastName', headerName: 'Last name', width: 130 },
{ field: 'userName', headerName: 'Username', width: 130},
{ field: 'userEmail', headerName: 'Email', width: 180 },
{ field: 'userRole', headerName: 'Role', width: 80}
];
const rows =userLists.map(user=>({ id: user.userID, firstname: user.userFirstName, lastName: user.userSurname, userName: user.username, userEmail: user.userEmail, userRole: user.userRoleID }));
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid rows={rows} columns={columns} pageSize={10} checkboxSelection />
</div>
);
}

Resources