MVC3 WebGrid Formatting or Styling Column Headers
Asked Answered
E

3

12

I'm using the new MVC3 WebGrid. So far so good, just having issues styling/formatting the column headers. The best I've got is a workaround that applies the same css class from the first row of the WebGrid to the table header.

var headerCells = $("#grid tr:eq(0) th");
var firstRowCells = $("#grid tr:eq(1) td");

$.each(firstRowCells, function (index, value) {
    $(headerCells[index]).addClass($(firstRowCells[index]).attr("class"));
});

This example obviously lacks a check to make sure there are rows or indeed the specifed element id, but it applies the css class from the first row to the header row meaning you can style independently of each other.

td.my-column-style { width:100px }
th.my-column-style { text-align:right;}

Is there a built in way of styling the column header elements (not just using the headerStyle property)?

Edsel answered 1/2, 2011 at 23:27 Comment(0)
R
5

No, as of now there is no built-in way to style the header cells independently, only the header row via the headerStyle property.

I think your workaround is good enough.

Rox answered 5/2, 2011 at 22:28 Comment(1)
@Jason Indeed. WebGrid is a big catastrophe!Tildi
J
4

I know this is an old question, but this may be useful to viewers who stumble across it. The :nth-child css pseudo selector is your friend, if you don't want to rely on javascript to copy the classes. It is easy to add a class to your webgrid using the tableStyle property, and then you can style the individual headers with the following bit of css:

.webgridclass tr th:nth-child(1){
    background:#ff0;
}

.webgridclass tr th:nth-child(2){
    background:#f60;
}

Unfortunately, this is not supported in IE8 and earlier IE, but it does have full support in all proper browsers (newer than FF3).

Jotunheim answered 4/7, 2013 at 13:39 Comment(0)
C
1

We can do this using of Javascript code as below.

JsFiddle Example

$("table tr th:nth-child(n)").addClass("col-md-1");
Carlottacarlovingian answered 6/11, 2015 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.