I have an issue with my rangedate filter in Next.js-TypeScript. I am using primereact library. Here is the code:
const [filters, setFilters] = useState<DataTableFilterMeta>({
woroStartDate: { operator: FilterOperator.AND, constraints: [{ value: null, matchMode: FilterMatchMode.DATE_IS }] },
woroStatus: { value: 'OPEN', matchMode: FilterMatchMode.EQUALS }, //other column filter
});
const [startDate, setStartDate] = useState<string | Date | Date[] | null>(null);
const [endDate, setEndDate] = useState<string | Date | Date[] | null>(null);
const handleStartDateChange = (e: any) => {
setStartDate(e.value);
};
const handleEndDateChange = (e: any) => {
setEndDate(e.value);
};
const onDateFilterChange = () => {
const newFilters = { ...filters };
if (startDate && endDate) {
const startDateValue = startDate ? startDate.toString() : null;
const endDateValue = endDate ? endDate.toString() : null;
newFilters['woroStartDate'] = {
matchMode: FilterMatchMode.DATE_IS,
value: [startDateValue, endDateValue],
};
} else {
newFilters['woroStartDate'] = {
operator: FilterOperator.AND,
constraints: [{ value: null, matchMode: FilterMatchMode.DATE_IS }],
};
}
setFilters(newFilters);
};
//filter form
<div>
const renderHeader = () => {
return (
<Calendar className="w-full md:w-10rem" value={startDate} onChange={handleStartDateChange} dateFormat="dd/mm/yy" showButtonBar />
<span className="mx-2">to</span>
<Calendar className="w-full md:w-10rem" value={endDate} onChange={handleEndDateChange} dateFormat="dd/mm/yy" showButtonBar />
<React.Fragment>
<Button className="ml-2" severity="secondary" type="button" tooltip="Filter Date" tooltipOptions={{ position: 'right', showDelay: 300 }} icon="pi pi-filter" outlined rounded onClick={onDateFilterChange} />
</React.Fragment>
</div>
);
};
const header = renderHeader();
//datatable and column
<DataTable
value={woros}
filters={filters}
emptyMessage="No Work Order found."
globalFilterFields={['woroStartDate', 'woroStatus']}
header={header} >
<Column
field="woroStartDate"
header="WorkOrder Date"
sortable
style={{ width: '25%' }}
body={(rowData: any) =>
new Date(rowData.woroStartDate).toLocaleDateString("en-GB", {
day: "numeric",
month: "short",
year: "numeric"
})
}
/>
The filter has not been successful in filtering my datatable. And when I click the filter button, there is an error: TypeError: value.toDateString is not a function.
And there are only 20 data in the table, so it seems that no changes are needed on the backend, but I still haven't found a solution for this yet. hope someone can help me. thanks..