question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add the ability to pass `className` to the header of a Table

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

Currently, with the Table component, it is very easy to style the Table as a whole (using className), and very easy to style any individual row (using the rowClassName function). However, styling the header seems impossible from within the component.

If we pass a className to the table, targeting thead or .ant-table-thead within the referenced style doesn’t affect the header.

This makes the experience of altering the header of table very frustrating!! Considering that Ant Design is being advertised as an easy-to-use toolkit for building custom UIs, I think it is important that headers should be easily style-able. It is very common to want the header to look different than the rest of the rows, and currently - it is non-trivial. We can override the less variable for the header background (table-header-bg), but then what if we want to change the text-color too? What about other CSS variables?

There is no straightforward way to do this that I can find, and I think adding the ability to pass a headerClassName to the Table would be very elegant & useful to many developers/designers.

I have also asked this question here: https://github.com/react-component/table/issues/228

What does the proposed API look like?

import React from 'react';
import { Table } from 'antd';
import { css } from 'react-emotion';

const header = css({ backgroundColor: 'rgb(100, 108, 140)', color: 'white', margin: '50px'});
const body = css({ backgroundColor: 'green' });

const styledTable = ({data, columns}) => ( 
  <Table 
    dataSource={data} 
    columns={columns} 
    className={body} // note - this works currently! (but doesn't affect the header)
    headerClassName={header}  // I would love this! 
  />
));

Cheers, thanks for taking the time to read this!


<!-- generated by ant-design-issue-helper. DO NOT REMOVE -->

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
apieceofbartcommented, Aug 11, 2018

@thisissami This should work:

const body = css({
  backgroundColor: 'green',
  '& thead > tr > th': {
    backgroundColor: 'blue'
  }
})

If you are looking for a library and this is the only thing you’re missing in ant then let’s fix that and help others at the same time 😃

0reactions
thisissamicommented, Aug 14, 2018

@apieceofbart thanks for your help! Here’s a full react component anybody can grab if they’re having an issue with this in the future. (also addresses the issue of applying borders to a row as a whole)

import React from 'react';
import { Table } from 'antd';
import { css } from 'react-emotion';

const tableCSS = css({
  margin: '40px 120px',
  backgroundColor: 'white',
  '& table': {
    borderCollapse: 'collapse'
  },
  '& thead > tr > th': {
    backgroundColor: 'darkblue',
    color: 'white',
  },
  '& thead > tr': {
    borderWidth: '2px',
    borderColor: 'yellow',
    borderStyle: 'solid'
  }
});

const StyledTable = ({ data, columns }) => (
    <Table
      className={tableCSS}
      dataSource={data}
      columns={columns}
    />
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass a className to the Header of a Table in antd ...
Instead, the solution is to pass the className to the Table as a whole, and have CSS as follows in order to style...
Read more >
How to Make a Table in React using React Table library
React table is a library used for creating data tables with data grids ... ()} className="bg-secondary text-white fs-4" > {col.render("Header")} ...
Read more >
React Table: A complete tutorial with examples
In this React table tutorial, we'll show you how to implemenet your own data table in React using react-table, complete with examples.
Read more >
CSS Styling - Tabulator
Add custom styling to your tables using a full set of CSS classes. ... tabulator-handle, Invisible resize handle on the right of each...
Read more >
JavaScript adding a class name to the element - GeeksforGeeks
element.classList.add("newClass");. Example: This example uses .add() method to add class name.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found