Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Table] Specify background colour for striped rows within a table #5286

Closed
diedsmiling opened this issue Sep 29, 2016 · 6 comments
Closed

[Table] Specify background colour for striped rows within a table #5286

diedsmiling opened this issue Sep 29, 2016 · 6 comments
Labels
component: table This is the name of the generic UI component, not the React module!

Comments

@diedsmiling
Copy link

diedsmiling commented Sep 29, 2016

Description

One can set stripedRows prop for a <TableBody> component, and even rows will get different background color. But how can this color be changed? Or not just the color, but even some other style properties. I thought about adding a prop stripedRowsStyle for the possibility to customize the appearance of striped rows. Does it makes sense to make a PR with this feature?

Images & references

table - material-ui f0 9f 94 8a 2016-09-29 12-29-04

@diedsmiling diedsmiling changed the title Specify bacground colour for striped rows within a table Specify background colour for striped rows within a table Sep 29, 2016
@lucasbento lucasbento added component: table This is the name of the generic UI component, not the React module! Feature Request labels Oct 15, 2016
@oliviertassinari oliviertassinari changed the title Specify background colour for striped rows within a table [Table] Specify background colour for striped rows within a table Dec 18, 2016
@JLNNN
Copy link

JLNNN commented May 17, 2017

Any news on this? :)

@diedsmiling
Copy link
Author

My pr was rejected, because they do not support first version anymore. Focusing on next version. :)

@bradrisse
Copy link

bradrisse commented Jun 13, 2017

Table


<TableBody>
{data.map((row, index) => (
     <TableRow key={index} style={{ padding: '5px 20px', height: 25, ...this.getStripedStyle(index) }}>
           ....Columns
     </TableRow>
))}
</TableBody>

Get Striped Style

  getStripedStyle(index) {
    return { background: index % 2 ? '#fafafa' : 'white' };
  }

@ayoola-moore
Copy link

ayoola-moore commented Oct 9, 2017

From the above, while the background color changes, it doesn't change to the appropriate color specified. For example -

getStripedStyle(index) {
    return { background: index % 2 ? '#fafafa' : '#323' };
}

changed the color, but displayed a different color order than the one specified above.

@jboron
Copy link

jboron commented Feb 6, 2018

This is my solution

table.component.html

<mat-table #table class="striped" [dataSource]="dataSource">
    <!-- Position Column -->
    <ng-container matColumnDef="position">
        <mat-header-cell *matHeaderCellDef> No.</mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.position}}</mat-cell>
    </ng-container>


    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row [ngClass]="{'highlight': index%2 == 1}" *matRowDef="let row; columns: displayedColumns;let index = index"></mat-row>
</mat-table>

table.component.scss

.highlight {
    background: #42A948; /* green */
}

@franverona
Copy link

franverona commented Dec 27, 2019

I've solved it by assigning a root class to TableBody, then a root class to each TableRow, and use CSS. I think it's a better solution as I can control the background color using CSS (as I'm using a SCSS variable).

Component.tsx

<TableContainer id="movies-table" component={Box}>
  <Table className="table" aria-label="simple table">
    <TableHead classes={{ root: 'table-header' }}>
      <TableRow>
        <TableCell>Movie</TableCell>
        <TableCell>Rate</TableCell>
          Registered at
        </TableCell>
      </TableRow>
    </TableHead>
    <TableBody classes={{ root: 'table-body' }}>
      <TableRow classes={{ root: 'table-row' }}>
        <TableCell>
          The Godfather
        </TableCell>
        <TableCell>
          5/5
        </TableCell>
      </TableRow>
      <TableRow classes={{ root: 'table-row' }}>
        <TableCell>
          Marvel Avengers: Endgame
        </TableCell>
        <TableCell>
          4.5/5
        </TableCell>
      </TableRow>
      <TableRow classes={{ root: 'table-row' }}>
        <TableCell>
          The Mask
        </TableCell>
        <TableCell>
          3/5
        </TableCell>
      </TableRow>
    </TableBody>
  </Table>
</TableContainer>

Component.scss

.table-body {
  .table-row {
    background-color: #fff;
  }

  .table-row:nth-child(2n) {
    background: #eee;
  }
}

Or if you are using CSS:

Component.css

.table-body > .table-row {
  background-color: #fff;
}

.table-body > .table-row:nth-child(2n) {
    background: #eee;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: table This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

8 participants