Transform the header style columns of react-js Material-tables into rows

Currently experimenting with gradient designs on a material table. While I am able to apply the right color combination to the rows, I seem to be getting column-based results on the title of the table. Attached is a screenshot of my output for reference.

https://i.stack.imgur.com/HM69b.png

Below is the code snippet:

function RowStyling() {
  return (
    <MaterialTable
      title="Row Styling Preview"
      columns={[
        { title: 'Name', field: 'name' },
        { title: 'Surname', field: 'surname' },
        { title: 'Birth Year', field: 'birthYear', type: 'numeric' },
        {
          title: 'Birth Place',
          field: 'birthCity',
          lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
        },
      ]}
      data={[
        { name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
        { name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
      ]}
      options={{
        rowStyle: {
            background: 'linear-gradient(to right,#137a8b,#30e4fb)',
           border: '2px white solid',
           borderRadius: '50px'
        },
         headerStyle: {
                background: 'linear-gradient(to right,#137a8b,#30e4fb)',
                color: '#FFF',
              }
      }}
    />
  )
}

If you want to make the headers look like the rows, refer to the material-table styling documentation here.

Answer №1

Hello, it appears that there may be a bug in the material-table library. I attempted to create a linear gradient header color using the same rows in this example on codesandbox but encountered some unexpected behavior:

  1. If you specify
    background: "linear-gradient(to right,#137a8b,#30e4fb)"
    in the headerStyle, the style is applied to the header cells instead of the entire header (as seen when inspecting the element).

https://i.stack.imgur.com/FdJPo.png

The linear gradient is only applied to the th elements and not the tr as expected.

  1. The same issue occurs if you use

    backgroundImage: "linear-gradient(to right,#137a8b,#30e4fb)"
    .

  2. If you set backgroundColor: "red", the result is again individual cell styling rather than the entire header having the specified background color.

Note: I also attempted to override the css classes by wrapping MaterialTable in a Material UI ThemeProvider but did not see any changes.

EDIT

To override a class using ThemeProvider, follow these steps:

  1. Define a theme object and override the desired class properties:

    const Theme = {
     palette: {
       primary: {
         contrastText: "#FF0000",
         dark: "#FF0000",
         main: "#FF0000",
         light: "#FF0000"
      }
    },
    overrides: {
     MuiTableHead: {
       root: {
         background: "black"
       }
     }
    }
    };
    
  2. Create a theme using createMuiTheme:

    const theme = createMuiTheme(Theme);
    
  3. Wrap MaterialTable in a ThemeProvider component:

    <ThemeProvider theme={theme}>
       <MaterialTable
        ....
        />
    </ThemeProvider>
    

However, even with the black background specified in the component style, the header remains red as set in headerStyle.

Answer №2

Upon initial inspection of the element, I identified its classes and proceeded to define them within the withStyles function of material UI in order to override its CSS, as shown in the code snippet below:

const styles = withStyles({
  root: {
    "& .MuiTypography-h6": {//Title in MTable
      color: "#545454"
    },
    '& .MuiPaper-root': {//table header root,where title and toolbox relies 
      color: 'black',
      transition: 'box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
      backgroundColor: 'white',
      borderRadius: '10px;',
      padding: '10px'
    },
    "& .MuiTableRow-head": {//title columns
      background: "linear-gradient(to right,#137a8b,#30e4fb) !important",
      border: '2px white solid',
      borderRadius: '10px',

    },
    "& .MuiTableCell-head": {//table cells,all
      background: "none"
    }
}
});

Subsequently, I encapsulated this material table within a div tag with a designated CSS class like so:

<div className={classes.root}>
         <MaterialTable/>
  </div>

Note: You have the option to use makeStyles in functional components and withStyles in class-based components.

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

Registration of Laravel Vue.js components

I am currently working on a Vue.js application in conjunction with Laravel. To get started, I registered Vue.js like this: import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); import App from './compone ...

FireFox appears to be experiencing issues with accessing the contentDocument of a frame using jQuery

While this line of code functions correctly on Chrome and IE, it encounters an issue on FireFox. The error message displayed in the FireFox console is: ReferenceError: $ is not defined var lang = $($('#navFrame')[0].contentDocument).find('# ...

Deactivate Firestore listener in useEffect to cease updates

Looking for a solution with useEffect to stop listening to Firebase Firestore collection changes? Data from Firebase can be retrieved successfully, but encountering issues accessing the unsubscribe function. Any ideas on how to resolve this problem? l ...

What is the process for setting a background image on a Button component using Material-UI in a React

I am struggling to figure out how to add a background image to a Material-ui button in React. I attempted adding 'backgroundImage' to the styles, but it didn't work. Can someone help me make this work? Here is my code: const useStyles ...

What is the best way to use jQuery to filter data in a table by clicking on a specific table row?

My website contains a table with player names and servers, but I want to make them clickable for filtering purposes. For instance, clicking on a player name should reload the leaderboards to show which servers that player plays on, and clicking on a server ...

Utilizing ng-disabled with a custom directive

Is it possible to achieve the following: <directiveName parameter1=value1 parameter2=value2 ng-disabled="true"> </directiveName> I tried this but couldn't get it to work and didn't find many examples of its use. However, I can togg ...

After ReactDOM is used, the JavaScript code is no longer functioning properly

While working on my latest project, I utilized react.js and encountered an issue where javascript seemed to stop working when using ReactDOM to render a class extended from React.Component. I noticed that the alert() method would not work if it was placed ...

The setState method fails to properly update the state

I am currently utilizing React JS. Below is the code for my React class: class MyReactComponent extends React.Component{ constructor(props){ super(props); this.state = { passAccount: { email: "Email&quo ...

"Padding has not been recognized as a valid input value

I'm struggling with getting padding to be accepted by the browser when I style a card using Material-UI. const useStyles = makeStyles((theme) => ({ cardGrid: { padding: '20px auto' }, })) Unfortunately, the browser is not recogni ...

What is the impact of using overflow: hidden on block elements?

HTML <div class="top"><p>hello</p></div> <div class="bottom"><p>hello</p></div> CSS .bottom { overflow: hidden; } While delving into the world of CSS, I encountered an interesting qu ...

jQuery's conditional always evaluates the first condition

One of the elements in my jQuery code is a conditional function designed to display a specific message with its own style when necessary: function msg(type, text) { if (type="success"){ $('.messageDisplay').attr('class', &a ...

Immer along with a subclass of Map

Recently, I decided to implement useImmerReducer in my React application for the first time, but encountered an issue. In my project, I utilize a custom class called Mappable, which is derived from Map: import { immerable } from "immer"; type K ...

Neither req.body nor req.file contain any data

Hey everyone, I'm new to coding and currently working on creating a basic markdown blog. However, I'm facing an issue where req.body doesn't contain my markdown field and req.file is undefined when trying to upload an article. I'm unsur ...

CSS - positioning the sidebar on the far left edge of the screen (refer to image)

Is there a way to create a layout similar to this design: For a demonstration, see here: http://jsfiddle.net/ymC82/ Current HTML code: <div class="wrapper"> <aside> Sidebar </aside> <article> ...

An issue arises when ng-pattern fails to recognize a regular expression

My form includes inline validation for a specific input field. <form name="coForm" novalidate> <div> <label>Transaction: </label> <input type="text" name="transaction" class="form-control" placeholder="<Dir ...

Why won't my Twitter Bootstrap navigation bar apply the styles?

I am currently working on a Rails app (4.0) and have incorporated Bootstrap 3 into my project. Both bootstrap.css and bootstrap-theme.css are stored in the stylesheets directory, while bootstrap.js is located in the javascripts directory. Most of the Boots ...

Unlocking the Secret Code

Presenting the question: checkPassword([["Peter", "P"], ["Sarah", "S"], ["Ethan", "R"]], {"Peter": "P", "Sarah": "Q", //"Ethan":"E"}) ...

What is the best way to switch between two icons using boot-strap and jQuery without the use of buttons?

Is there a way to make it so that clicking on the heart icon hides it and shows the filled heart icon instead using jQuery? I am struggling to figure out how to reference the other icon in my function. This is just a rough idea of what I want to achieve. ...

"Troubleshooting: Why are errors not appearing in ts-node

Whenever I encounter an error in my code while compiling with ts-node, the error does not seem to appear in the console. For instance:let data = await fs.readFileSync(path); In the following code snippet, I am using "fs" to read a file by passing a path ...

When deploying a Node.js Express API using Docker, the headers are not being sent

After creating a Node.js Express API, I implemented the /login endpoint for users to authenticate and receive an Authorization Token: @Post('/login') @UseBefore(validationMiddleware(LoginUserDto, 'body')) async logIn(@Res() res: R ...