In material table react, I have the following columns:
columns={[
{
title: 'Name',
field: 'name',
type: 'string',
},
{
title: 'Age',
field: 'age',
type: 'string',
},
{
title: 'DOB',
field: 'birthDate',
type: 'date',
dateSetting: {
format: 'dd/MM/yyyy'
},
}
]}
When I try to filter the date column it shows filtered date as, for example, February 24th, but instead I want the date to be like dd/MM/yyyy format.
How can I change the value. I already tried giving dateSetting, but it does not work on filter. Thank You.
In material table react, I have the following columns:
columns={[
{
title: 'Name',
field: 'name',
type: 'string',
},
{
title: 'Age',
field: 'age',
type: 'string',
},
{
title: 'DOB',
field: 'birthDate',
type: 'date',
dateSetting: {
format: 'dd/MM/yyyy'
},
}
]}
When I try to filter the date column it shows filtered date as, for example, February 24th, but instead I want the date to be like dd/MM/yyyy format.
How can I change the value. I already tried giving dateSetting, but it does not work on filter. Thank You.
Share Improve this question edited Jun 2, 2023 at 14:37 NearHuscarl 82.1k23 gold badges320 silver badges282 bronze badges asked Feb 26, 2021 at 11:37 DevDev 551 gold badge1 silver badge7 bronze badges2 Answers
Reset to default 4You could achieve that by defining your own filterComponent
. Here is a working example I made based on this solution:
First, you need to create the ponent to be used as a filter, in my case I used KeyboardDatePicker
from material-ui/pickers, but could it be anything you need:
const CustomDatePicker = (props) => {
const [date, setDate] = useState(null);
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
margin="normal"
id="date-picker-dialog"
label="Date picker"
format="dd/MM/yyyy"
clearable
value={date}
onChange={(event) => {
console.log("Date picker value: ", event);
console.log(props.columnDef.tableData.id);
setDate(event);
props.onFilterChanged(props.columnDef.tableData.id, event);
}}
KeyboardButtonProps={{
"aria-label": "change date"
}}
/>
</MuiPickersUtilsProvider>
);
};
The key aspect is to call the onFilterChanged
function that is passed via props
.
Then, on your column definition just implement your ponent, like this:
const tableColumns = [
{ title: "Client", field: "id" },
{ title: "Name", field: "name" },
{
title: "Date",
field: "date",
type: "date",
dateSetting: { locale: "en-GB" },
filterComponent: (props) => <CustomDatePicker {...props} />
}
];
Full code and sandbox here. Hope that works for you!
import "date-fns";
import DateFnsUtils from "@date-io/date-fns";
import {
MuiPickersUtilsProvider,
KeyboardDatePicker
} from "@material-ui/pickers";
import React, { useState } from "react";
const CustomDatePicker = (props) => {
const [date, setDate] = useState(null);
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
margin="normal"
id="date-picker-dialog"
label="Date picker"
format="dd/MM/yyyy"
clearable
value={date}
onChange={(event) => {
console.log("Date picker value: ", event);
console.log(props.columnDef.tableData.id);
setDate(event);
props.onFilterChanged(props.columnDef.tableData.id, event);
}}
KeyboardButtonProps={{
"aria-label": "change date"
}}
/>
</MuiPickersUtilsProvider>
);
};
export default CustomDatePicker;
and
import React, { Fragment, useState } from "react";
import MaterialTable from "material-table";
import CustomDatePicker from "./customDatePicker";
const originalData = [
{
id: "client 1",
name: "Anna",
date: new Date("2021-03-01T20:11:54")
},
{
id: "client 2",
name: "Tom",
date: new Date("2020-03-30T11:01:54")
},
{
id: "client 3",
name: "Deb",
date: new Date("2021-02-28T21:11:54")
}
];
export default function CustomEditComponent(props) {
const [data, setData] = useState(originalData);
const tableColumns = [
{ title: "Client", field: "id" },
{ title: "Name", field: "name" },
{
title: "Date",
field: "date",
type: "date",
dateSetting: { locale: "en-GB" },
filterComponent: (props) => <CustomDatePicker {...props} />
}
];
return (
<Fragment>
<MaterialTable
columns={tableColumns}
data={data}
title="Material Table - Custom Filter Component"
options={{ search: false, filtering: true }}
editable={{
onRowAdd: (newData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
setData([...data, newData]);
resolve();
}, 1000);
}),
onRowUpdate: (newData, oldData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("old:", oldData);
console.log("new:", newData);
const dataUpdate = [...data];
const index = oldData.tableData.id;
dataUpdate[index] = newData;
setData([...dataUpdate]);
resolve();
}, 1000);
}),
onRowDelete: (oldData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
const dataDelete = [...data];
const index = oldData.tableData.id;
dataDelete.splice(index, 1);
setData([...dataDelete]);
resolve();
}, 1000);
})
}}
/>
</Fragment>
);
}