Webgrid MVC 3 Conditional Row Style

I use WebGrid to display a list of items,

I like to set the background color of the lines based on the condition. I want to set the background color of the entire row, not just one cell.

Any example?

Thank you

+4
source share
2 answers

This is an old question, but I just stumbled upon it and got an answer that, in my opinion, is not too hacky. The previous answer is provided only if the value that you use to conditionally change the background color is the value of the table cell.

If this is not the case, you can set the data- attribute for the first cell in the rows of the table using the Format property for WebGridColumn. Here the first column of my table contains hyperlinks. I define it in my code (controller action in MVC), and I added the data-in-error attribute from the IsInError property of my object. You can set the value of this attribute in any way that makes sense for your application.

 new WebGridColumn { ColumnName = "Id", Header = "ID", Format = (x) => new HtmlString(String.Format("<a href=\"#\" data-in-error=\"{0}\">{1}</a>", x.Value.IsInError, x.Value.Id)) }); 

Then, using jQuery, I find all the rows in my table that have a binding in the first cell of the row, and set the class of this row to "error".

 $(document).ready(function () { $('table tbody tr td:first-child a[data-in-error="True"]').each(function () { $(this).parent().parent().addClass('error'); }); }); 

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1434990/


All Articles