{"id":10904,"date":"2024-07-16T07:06:45","date_gmt":"2024-07-16T07:06:45","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10904"},"modified":"2024-07-16T07:06:46","modified_gmt":"2024-07-16T07:06:46","slug":"material-react-table-with-searchable-dropdown","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/material-react-table-with-searchable-dropdown","title":{"rendered":"Material React Table with Searchable Dropdown"},"content":{"rendered":"<p>To add a searchable dropdown functionality to the &#8220;State&#8221; column in your Material React Table using the Autocomplete component from Material-UI, you can follow these steps. Here\u2019s how you can update your code to replace the MenuItem dropdown with the Autocomplete component:<\/p>\n<ol>\n<li><strong>Import the necessary components from Material-UI:<\/strong> Ensure you import Autocomplete and TextField from @mui\/material.<\/li>\n<li><strong>Update the columns definition:<\/strong> Modify the columns definition to use the Autocomplete component for the &#8220;State&#8221; column.<\/li>\n<\/ol>\n<p>Here\u2019s the updated code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport { useMemo } from 'react';\r\nimport {\r\n  MaterialReactTable,\r\n} from 'material-react-table';\r\nimport { Autocomplete, TextField, MenuItem } from '@mui\/material';\r\n\r\nconst Example = () =&gt; {\r\n  const states = ['California', 'Texas', 'New York', 'Florida', 'Illinois']; \/\/ Example states array\r\n\r\n  const columns = useMemo(\r\n    () =&gt; [\r\n      \/\/ Other columns...\r\n      {\r\n        accessorKey: 'state',\r\n        header: 'State',\r\n        Edit: ({ row }) =&gt; (\r\n          &lt;Autocomplete\r\n            options={states}\r\n            autoComplete\r\n            renderInput={(params) =&gt; &lt;TextField {...params} label=\"State\" \/&gt;}\r\n            onChange={(event, value) =&gt; {\r\n              row._valuesCache['state'] = value;\r\n            }}\r\n          \/&gt;\r\n        ),\r\n      },\r\n    ],\r\n    [states]\r\n  );\r\n\r\n  return (\r\n    &lt;MaterialReactTable\r\n      columns={columns}\r\n      data={[]} \/\/ Your data here\r\n      \/\/ Other props...\r\n    \/&gt;\r\n  );\r\n};\r\n\r\nexport default Example;\r\n<\/pre>\n<p>&nbsp;<br \/>\nIn this updated code, the Autocomplete component is used to provide a searchable dropdown for the &#8220;State&#8221; column. The onChange event handler updates the row&#8217;s state value when a new state is selected from the dropdown. This allows users to search for and select a state from the list of options.<\/p>\n<p>By incorporating the Autocomplete component from Material-UI, you can enhance the user experience with a searchable dropdown menu in your table.<\/p>\n<h3>Output screenshot:<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/07\/16065941\/unnamed-1-1.png\" alt=\"edit-user\" width=\"937\" height=\"764\" class=\"alignnone size-full wp-image-10906\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/07\/16065941\/unnamed-1-1.png 937w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/07\/16065941\/unnamed-1-1-300x245.png 300w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/07\/16065941\/unnamed-1-1-768x626.png 768w\" sizes=\"(max-width: 937px) 100vw, 937px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To add a searchable dropdown functionality to the &#8220;State&#8221; column in your Material React Table using the Autocomplete component from Material-UI, you can follow these steps. Here\u2019s how you can update your code to replace the MenuItem dropdown with the Autocomplete component: Import the necessary components from Material-UI: Ensure you import Autocomplete and TextField from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10905,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10904"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10904"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10904\/revisions"}],"predecessor-version":[{"id":10908,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10904\/revisions\/10908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10905"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}