<br />
<b>Notice</b>:  Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>all-in-one-seo-pack</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in <b>/home/fes/web/olgamatosova.sfdevserver.com/public_html/blog/wp-includes/functions.php</b> on line <b>6131</b><br />
<br />
<b>Notice</b>:  Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>wp-diary</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in <b>/home/fes/web/olgamatosova.sfdevserver.com/public_html/blog/wp-includes/functions.php</b> on line <b>6131</b><br />
{"id":34,"date":"2024-09-11T14:21:53","date_gmt":"2024-09-11T14:21:53","guid":{"rendered":"https:\/\/olgamatosova.sfdevserver.com\/blog\/?p=34"},"modified":"2025-01-25T17:25:35","modified_gmt":"2025-01-25T17:25:35","slug":"mui-datagrid-with-date-range-filter","status":"publish","type":"post","link":"https:\/\/olgamatosova.sfdevserver.com\/blog\/?p=34","title":{"rendered":"How To Connect MUI DATAGRID With Date Range filter"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"389\" src=\"https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-1024x389.png\" alt=\"\" class=\"wp-image-42\" style=\"width:844px;height:auto\" srcset=\"https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-1024x389.png 1024w, https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-300x114.png 300w, https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-768x292.png 768w, https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-1536x584.png 1536w, https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-2048x778.png 2048w, https:\/\/olgamatosova.sfdevserver.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-2024-09-09-at-15.23.33-600x228.png 600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Probably, each react developer encountered with non effective documentation for different libraries. And spent a lot of time to achievement desired result. <br>In this article, I will guide you through the process of adding a custom <code>DateRange<\/code> component into the MUI DataGrid, allowing users to filter by date ranges. We&#8217;ll be using the <code>DateRange<\/code> component from the <code><a href=\"https:\/\/www.npmjs.com\/package\/react-date-range\" title=\"\">react-date-range<\/a><\/code> library and integrating it into the DataGrid&#8217;s column settings<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1<\/strong>: First, ensure you have the required libraries installed in your project. You&#8217;ll need both MUI DataGrid and <code>react-date-range<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install --save @mui\/x-data-grid react-date-range<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><br><strong>Step 2<\/strong>: Create DateFilterComponent and import DateRange and styles into your component:<\/h3>\n\n\n\n<p> Your custom date range filter component should handle how the date filter is displayed and function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">import 'react-date-range\/dist\/styles.css'; \/\/ main style file\nimport 'react-date-range\/dist\/theme\/default.css'\nimport { DateRange } from 'react-date-range';<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">&lt;DateRange\n     editableDateInputs={true}\n     onChange={handleFilterChange}\n     moveRangeOnFirstSelection={false}\n     ref={dateRangeRef}\n     ranges={state}\n\/&gt;<\/code><\/pre>\n\n\n\n<p>Also for formatting dates let&#8217;s setup datajs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install dayjs<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">import * as dayjs from 'dayjs';\nimport 'react-date-range\/dist\/styles.css'; \/\/ main style file\nimport 'react-date-range\/dist\/theme\/default.css';\nimport { DateRange } from 'react-date-range';\n\nexport <em>function<\/em> DateFilterComponent(<em>props<\/em>) {\n    const { item, applyValue } = props;\n    const [state, setState] = useState(\n        [\n            {\n                startDate: new Date(),\n                endDate: new Date(),      \n                key: \"selection\"\n           }\n        ]);\n\n    const dateRangeRef = useRef(null);\n    const handleFilterChange = (value) =&gt; {    \n        applyValue({      \n            ...item,      \n            value: `${dayjs(value.selection.startDate).format(\"DD-MM-YYYY\")}\/${dayjs(value.selection.endDate).format(\"DD-MM-YYYY\")}`\n        });    \n        setState([value.selection]);  \n    };\n    return(\n       &lt;DateRange\n          editableDateInputs={true}\n          onChange={handleFilterChange}\n          moveRangeOnFirstSelection={false}\n          ref={dateRangeRef}\n          ranges={state}\n       \/&gt;\n    )\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Concepts<\/strong>:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Importing Dependencies<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The code imports <code>dayjs<\/code> for date manipulation and formatting.<\/li>\n\n\n\n<li>Stylesheets for <code>react-date-range<\/code> are imported to ensure the component displays correctly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Component Structure<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The component receives <code>item<\/code> and <code>applyValue<\/code> as props, which are used to apply the selected date range filter.<\/li>\n\n\n\n<li><code>useState<\/code> manages the selected date range, initializing both <code>startDate<\/code> and <code>endDate<\/code> to the current date.<\/li>\n\n\n\n<li><code>useRef<\/code> creates a reference to the DateRange component, allowing for direct interaction with the date picker.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Handling Date Selection<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>handleFilterChange<\/code> function formats the selected date range using <code>dayjs<\/code>, applying it in the &#8220;DD-MM-YYYY&#8221; format.<\/li>\n\n\n\n<li>This function also updates the component&#8217;s state with the new date range whenever the user makes a selection.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Rendering the Date Range Picker<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>DateRange<\/code> component from <code>react-date-range<\/code> is rendered with the ability to edit the date inputs.<\/li>\n\n\n\n<li>The selected date range is displayed and updated as the user interacts with the picker.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>This component integrates a date range filter in a custom way, making it a valuable addition for implementing advanced filters in MUI DataGrid.<br><br><strong>Step 3<\/strong>: Set up filterOperators for the date column: The filterOperators prop allows you to customize the operators for a specific column. You will apply the date filter operators using getGridDateOperators() and integrate it with the custom DateFilterComponent<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">  const columns = [\n      {\n        field: \"created_at\",\n        headerName: \"Date account created\",\n        type: \"date\",\n        filterOperators: getGridDateOperators()\n          .filter((operator) =&gt; operator.value == \"is\")\n          .map((operator) =&gt; {\n            return {\n              ...operator,\n              InputComponent: DateFilterComponent,\n              InputComponentProps: { type: \"date\" },\n            };\n          }),\n      }\n    ];<\/code><\/pre>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">&lt;DataGrid\n    columns={columns}\n    rows={membersList}\n    onSortModelChange={onSortChange}\n    slots={{\n       toolbar: CustomToolbar,\n    }}\n\/&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Probably, each react developer encountered with non effective documentation for different libraries. And spent a lot of time to achievement desired result. In this article, I will guide you through the process of adding a custom DateRange component into the MUI DataGrid, allowing users to filter by date ranges. We&#8217;ll be using the DateRange component [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[8,9,10,7,5],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-react","tag-datagrid","tag-date-range","tag-javascript","tag-mui-datagrid","tag-react"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":8,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}