Itertating array data into a Material UI table - reactjs

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>
);
}

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.

Passing an Image in Material-UI Data-Grid Table

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,
},
];

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/

ng-grid editable some cells

Is there any way for make my ng-grid JUST SOME CELLS editable?
$scope.gridOptionsReports = {
data: 'Reports',
enableCellEdit: true,
columnDefs: [
{ field: 'Row', displayName: ' Row ', width: 40 },
{ field: 'OperationStatus', displayName: 'Operation', width: 120 },
{ field: 'Date', displayName: 'Date', width: 100 },
{ field: 'Consideration', displayName: 'Consideration', width: 80 },
{ field: 'Description', displayName: 'Description', width: 400 }
]
};
I want to make my Date and Description field editable.But now all of my fields are editable.
Thanks.

Resources