Tips for efficiently filtering an array of objects in the Mui DataGrid

After transitioning my tables to Mui-datagrid on Material UI 5, I encountered a unique challenge with an array of objects. Specifically, I aim to implement a phone number filter in this column, but the numbers are stored within object lists.

 phone: [
  { type: "home", number: "795-946-1806" },
  { type: "mobile", number: "850-781-8104" }
]

I was hoping for a method like 'customFilterAndSearch' or a way to customize search functionality for this specific field.

   customFilterAndSearch: (term, rowData) =>
    !!rowData?.suppressedOptions.find(({ description }) =>
      description?.toLowerCase().includes(term.toLowerCase())
    ),

While experimenting with the filterOperators, I have not achieved success yet. I have provided a detailed example here https://codesandbox.io/s/mui-data-grid-vs05fr?file=/demo.js

Answer №1

After reviewing the DataGrid documentation, it appears that there is no direct way to modify the filter function for a specific function.

A possible workaround for your scenario could involve converting the data to a string before passing it to the datagrid. However, this may result in losing the current styling, such as making the phone type bold.

Alternatively, consider splitting the phone column into two separate columns as a cleaner solution to your issue.

You may also want to consider adding a helper function that maps all the phone lists to specific categories like mobilePhone or homePhone.

const mapPhoneObject = (rows) => {
      rows.forEach((row) => {
        row.phone.forEach((phone) => {
          row[`${phone.type}Phone`] = phone.number;
        });
      });
      return rows
    };

I have included a modified version of your code with my suggested function implementation, which I believe offers a viable solution to your problem: https://example.com/sandbox

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

in javascript, how can you access the value of a parent object within a nested object?

Within the material ui framework, I am utilizing the createMuiTheme function to create a theme. How can I access the values of the parent object within a nested object? (I am aware that I can declare global variables above the function); To better unders ...

Material UI button component does not have the applied class

I'm encountering an issue with applying a CSS class (redirectButton) to a Material UI button. Despite my efforts, the class is not being successfully applied. Below is the HTML code in question: {(isDataAdapterAdmin && !dataSources?.length) & ...

Spacing out the button and component in a single horizontal row

Is there a way to create some space between a button and a CircularProgress component that are aligned next to each other in the same row? I've tried using styles, but haven't been successful. Any suggestions on how to tackle this? <Grid con ...

Troubleshooting the "@material-ui/core" issue in React: Step-by-step guide

Issue with React: Unable to locate the '@material-ui/core' module in 'C:\Users\user\Downloads\inTech'22-Web Development (EDUGEN)\edu-gen\src\components' Resolution Command: To resolve, run npm in ...

What could be the reason for ThemeProvider (Material UI) failing to function properly in this scenario?

I've encountered something puzzling with Material UI. Despite my experience with it, I can't seem to figure out why my H2 tag in the nested component is rendering in Times instead of Arial. I've double-checked the docs and my code, but the i ...

"Customize the underline color of textfields in Material UI's Next Dialog

Is there a way to customize the underline color of a text box within a popup window using my secondary palette color? I've been struggling with this due to the lack of clarity in the documentation. ...

Is there a way to display the avatar image outside of the drawer in MUI ReactJS?

Currently, I am attempting to display the avatar image with a curved effect outside the border line of the sidebar. I have referenced the persistent drawer from MUI v5 for inspiration. You can view an example here: the codesandbox link The desired outcom ...

How to vertically align and center multiple divs with Flexbox programming

I have been grappling with the challenge of aligning two sets of rows vertically, each consisting of an image on one side and text on the other. While I usually use flex to achieve vertical alignment, it seems that a different approach is needed in this pa ...

Press the icon button within the card component without selecting the entire card

I have a card component with a clickable icon button inside it. I want to be able to click the icon button without triggering the ripple effect on the whole card. Currently, clicking the icon button causes the ripple effect to run on the entire card. Howe ...

Utilizing Color Theme Created in App.js's createMuiTheme Function within Child Components in React

I'm just getting started with React. I successfully set the primary color for my React application using MuiThemeProvider. Here's how: https://i.stack.imgur.com/VCzOC.png Every time the app loads, the primary color is randomly chosen. Now, I wa ...

Is it possible to encapsulate the cell text within MaterialTable while entering data for a new row in ReactJS?

Question: How can I ensure that text automatically wraps when inputting extremely long names in MaterialTable? I've encountered an issue with Material Table where, when entering a very lengthy name such as "LONG NAME LONG NAME LONG NAME LONG NAME," t ...

Tips for managing placeholder within CardMedia component in material-ui?

I'm looking for a way to manage placeholders in CardMedia from Material UI. If the image source is invalid, I want another image or placeholder to be shown. Here's my current code snippet: ` <CardMedia component="img" ...

Tips on adding several elements to an array depending on the selected value from various checkboxes?

I am working on a project where I need to populate an array based on selected checkboxes. Let's assume we have 3 arrays containing strings: const fruits = ["Apple", "Banana", "Orange", "Grapes"]; const vegetable ...

Unveiling the Theme: Navigating the Material UI withStyles and WithTheme for Theme

const StyledSlider = withStyles({ root: { color: theme => theme.palette.primary.main } }, { withTheme: true })(Slider); Initially, I thought the withTheme option would allow access to the theme inside the withStyles function. However, since the t ...

Is it possible to change the maximum column count in material-ui by transposing the grid?

Currently, I am working on setting up a grid layout of cards : https://codesandbox.io/s/u93zn In our scenario, we are facing an issue where the number of columns will always exceed the number of rows (and it surpasses the maximum limit that the grid can ...

Troubleshooting problems with Material UI card display when incorporating dynamic data sourced from GraphQL

Encountering a problem with the Card rendering utilizing Material UI within a GRANDstack project. When setting up the cards with fixed data, they render correctly: const displayCard = (ID) => { return ( <> <Grid item xs={12 ...

How can I retrieve the value of a MenuItem in Material UI?

Currently, I am utilizing Material UI for Menu and Menu Item components. However, I am encountering an issue while trying to retrieve the value of a menu item. Below is the code snippet in question: <Menu value= { this.state.selectedItem }> <M ...

What is the @page rule in Material-UI?

Trying to incorporate Material-UI styles with react-to-print to print components can be tricky, especially when dealing with a specific component that requires a particular page size. Here's an attempt at achieving this: const styles = (theme: Theme) ...

Tips for creating dynamic alerts using mui v5 Snackbar

My goal is to call an API and perform several actions. After each action, I want to display the response in a Snackbar or alert. Despite iterating through the messages in a map, I'm only able to show the first response and none of the others. Here is ...

Using Material UI DataGrid Pro to render cells with asynchronous data processing

Currently, I am utilizing Material UI's DataGrid Pro and have come across an issue with displaying a user's avatar image in a column. The challenge lies in asynchronously fetching the image while showing a skeleton URL placeholder during the load ...