How to change text and icon of (columnsButton) in material table - reactjs

How can I change the text "(Add or remove columns)" and icon of (columnsButton or Hiding Columns Button) in a material table?
For example for the code below:
function HideColFrmTbleNotFrmColsBtn() {
return (
<MaterialTable
options={{
// Allow user to hide/show
// columns from Columns Button
columnsButton: true,
}}
data={[
{
name: "Foo",
surname: "Bar",
},
{
name: "Baz",
surname: "Fee",
},
]}
columns={[
{
title: "Name",
field: "name",
// `name` field is hidden in table
// but not in Columns Button
hidden: true,
},
{
title: "Surname",
field: "surname",
},
]}
/>
);
}
https://material-table-core.com/demos/columns/hide/
You can see this code on the document page related to the material-table, and you can also see its details. I want to use columnsButton property to hide the table's columns, but I want to change its text (Add or remove columns) and icon. How can I do this?
Please see this picture I want to change (Add or remove columns):

Related

How to hide the options menu for only one column in MUI datagrid?

I am using MUI x (community version) and I want to hide the options' menu from a particular column, not for all the columns. I still want the others to display this menu, but only for this column, I want to hide it. How?
disableColumnMenu disables it for all the columns.
// const columns = [ ... etc, then eventually
{ field: 'actionMenu', headerName: strings.actions, sortable: false, filterable: false },
// ]
<DataGrid
disableSelectionOnClick
rows={rows}
columns={columns}
density="compact"
loading={isLoading}
components={{ Toolbar: GridToolbar }}
localeText={{
noRowsLabel: strings.noModerators,
}}
/>
You are very close.
Try this one in the field you want to hide the menu
{
field: 'actionMenu',
headerName: strings.actions,
sortable: false,
filterable: false,
disableColumnMenu: true // This will hide it only for the Actions column
},
Also please have a look at this working codesandbox, I am hiding the menu only for the 'Age' column and the rest they display the menu on hover
If you're trying to achieve what I was trying to achieve in the question, MUI provides a built-in way to create an "actions" column.
The "actions column" will include "action cells", the action cells maybe buttons, or maybe a menu button where you click it and drops a listed menu with other buttons.
Using the built-in way is a better decision because it makes your action column compatible with the already built-in data grid filters.
To declare an action column, in you column definition, use the type: 'actions' property with the getActions: (cell) => [] property, and with the <GridActionsCellItem /> component, example:
export default function ProductsDataGrid() {
const columns = [
{
field: "name",
width: 200,
type: "string",
},
{
field: "price",
width: 100,
type: "number",
},
{
field: "actions",
type: "actions",
width: 100,
getActions: (cell) => [
<GridActionsCellItem
label="delete"
icon={<DeleteIcon />}
onClick={() => openDeleteModal(cell.row)}
/>,
],
},
];
const data = [{ name: "Egg", price: 20 }];
return <DataGrid columns={columns} rows={data} autoHeight />;
}
This will result in showing a delete button inside a cell.
If you want it to show up inside the menu, just use the showInMenu prop:
<GridActionsCellItem
showInMenu
label="delete"
icon={<DeleteIcon />}
onClick={() => openDeleteModal(cell.row)}
/>,
The actions' column by default doesn't have a filter, and it won't show you the header actions.

react-bootstrap-table2 column width change on cell click

I have base react-bootstrap-table code:
import React from 'react'
import BootstrapTable from 'react-bootstrap-table-next';
import cellEditFactory from 'react-bootstrap-table2-editor';
const products = [
{
id: "0",
name: "test",
price: "2100"
},
{
id: "1",
name: "test",
price: "2100"
},
{
id: "2",
name: "test",
price: "2120"
}
];
const columns = [
{
dataField: "id",
text: "Product ID"
},
{
dataField: "name",
text: "Product Name",
},
{
dataField: "price",
text: "Product Price"
}
];
export default class App extends React.Component {
render() {
return (
<BootstrapTable
keyField="id"
data={ products }
columns={ columns }
cellEdit={ cellEditFactory({mode: 'click'})}
/>
)
}
}
My problem is that column changes width, when I click editable cell. I see on live demo that is way to block this effect. live demo
I try avoid this effect by adding css with max width, but it's not problem cell, but whole column:
editorStyle: {
backgroundColor: '#20B2AA',
maxWidth: '30%',
},
You need add .table {table-layout: 'fixed'}.
I was able to resolve this issue by using style props for each column in my table. Specifically, I added the following props:
editorStyle: {width:'100%', overflow:'scroll', padding:'inherit', height:'inherit'},
style: {width:'20%'}
To come up with the width of 20% I had to play around a bit. My table has more than 5 columns, and I found that having my total width across all columns sum to more than 100% gave me good looking columns that didn't expand when editing.

Adding rowspan or column span functionality at the table while generating data-table using (React) material table

I am generating dataTable using material-table plugin in ReactJS. I couldn't find any direct way or option to generate rowspan or column span at dataTable using the plugin. Is there any way to do it ?
Here is a sample screenshot of what table might look like but will be shown via dataTable
The explanation of what you are trying to do is a little bit unclear.
If you want to add something like a button on each row in a col span you can define it in the "columns" prop of your Table like so:
columns={[
{ title: 'Update', field: '', render: rowData => <button onClick={() => doSomethingWithId(rowData.id)} className="myTableButtonStyle" /> },
{ title: 'Name', field: 'name' },
....
]}
This will add an update button which will call on the doSomethingWithId() function passing the id of the line as a parameter.
Is it what you are looking for? Else would you mind explaining a little bit more what you want?
EDIT
Here is what i obtain
with the following code
<MaterialTable
data={[
{ name: '', nationality: 'British', address: '', country: 'England' },
{ name: 'Noor', nationality: 'American', address: 'California', country: 'US' },
{ name: '', nationality: 'Chinese', address: '', country: 'China' },
{ name: '', nationality: '', address: '', country: '' },
]}
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Nationality', field: 'nationality' },
{ title: 'Address', field: 'address' },
{ title: 'Country', field: 'country' },
]}
options={{
rowStyle: {
height: '25px',
},
}}
title="Display Data"
/>
You don't need to add rows or colspan to achieve this render, just giving the good dataSet and the Columns definition will do.
In your data props you need to give a list of objects with all the datas you need.
You then define which datas are displayed where with the columns props. if you want a more personalize render in one column (ex: you want to display the country in bold) you can define it by giving a render in the columns object like so:
{title: 'Country', field: '', render: rowData => <strong>{rowData.country}</strong>
for the empty row to have the same size as others, use props options on your table.
Does this help you?

Table - how to expand from specific column

I am expanding my table rows on click using expandedRowRender, and they both expand from first column. Is there a way to specify from which column the row should expand from? For example, only expand from column 2 onwards?
columns = [
{
title: "Name",
dataIndex: "name",
},
{
title: "Surname",
dataIndex: "surnamen",
},
{
title: "Region",
dataIndex: "region",
},];
<Table
bordered
dataSource={this.dataSource}
columns={this.columns}
expandRowByClick={true}
expandIconAsCell={false}
expandedRowRender={(expaned) => expaned ? <minitable /> : null}
/>
Add children to the specific column:
dataSource={[
{
name: 'Alex',
surnamen: 'Abramov',
region: 'React-Realm',
children: [{ surnamen: 'myData' }]
}
]}
After that, you can control the indentSize prop.
Check out the demo, try filling indentSize={80} in InputNumber.

Add Image to DevExtreme React Grid Bootstrap4 Column

I'm using react to show a grid with data from my database.
The data is showing properly, but its all text data.
I would like to show an icon at the first column.
This is what I have right now:
<Grid
rows={products}
columns={[
{ name: "icon", title: "Icon" },
{ name: "name", title: "Name" },
{ name: "desc", title: "Desc" },
{ name: "price", title: "Price" },
]
}></grid>
My {products} contains all data.
When I run this the Icon column contains image344.png and not an actual image.
What do I need to do to make the Icon column show an image instead of text?
I also tried to set the icon value to this <img src='image344.png'> but it still shows text.
Please help!
In Grid if give value as text it will show the actual text only. so create the actual html element for the icon column, then it will show the image icon.
Sample data added below to render the image in the Grid view.
var products= [{icon:'image344.png',name:'test',desc:'test',price:'123'}];
products = products.map(function(data){
data.icon = <img src={data.icon}/>;
return data;
});
<Grid
rows={products}
columns={[
{ name: "icon", title: "Icon" },
{ name: "name", title: "Name" },
{ name: "desc", title: "Desc" },
{ name: "price", title: "Price" },
]
}></grid>

Resources