Defining an Inline Row Style in System.Web.Helpers.WebGrid

I am moving my application to MVC 3 and trying to use System.Web.Helpers.WebGrid. I would like to get the html code, for example:

<table>
    <tr style="background-color: <%= item.Color %>">
    </tr>
    <tr style="background-color: <%= item.Color %>">
    </tr>
    <tr style="background-color: <%= item.Color %>">
    </tr>
</table>

There is a property rowStylethat allows you to define a css class for each line, but there will be a different style for each line. Is it easy to reach?

+3
source share
5 answers

So I had to end up with a hack. First include the color as part of another column. It should have been returned as MvcHtmlStringto avoid extra encoding:

<%  
    var grid = new WebGrid(Model);
%>
<%= 
    grid.GetHtml(
        tableStyle: "table_class",
        columns:  grid.Columns(
            grid.Column("Importance", CTRes.Importance, (item) => 
                new MvcHtmlString(Html.Encode(item.Importance) + 
                "<div class='color' style='display: none;'>#" + item.Color + "</div>"))
        )
    ) 
%>

Then we set the background color to $(document).ready():

<script type="text/javascript">
    $(document).ready(
        function () {
            $('.table_class .color').each(function (index, element) { $(element).parent().parent().css('background-color', $(element).html()); });
        }
    );
</script>
+6
source

, WebGrid , CSS.

WebGrid, , , :

  • CSS display:none
  • jQuery, background-color ,

, , , WebGrid HTML . .

+2

alternatingRowStyle: "( CSS )", , .

<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("FirstName"),
            grid.Column("LastName"),
            grid.Column("Salary",format:@<text>$@item.Salary</text>)
        )
    )
</div>

css "alt" .

:

http://weblogs.asp.net/shijuvarghese/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

0
source

Well, it was a little difficult for me to implement this, so I wanted to show what I did based on the accepted answer. Perhaps this will help you.

grid.Column("Status", "Status", (item) => 
     new MvcHtmlString(Html.Encode(item.Status) +
     "<div class='color' style='display: none;'>#" + item.RowColor + "</div>"))

I simply extracted the color from my Row object as follows:

public class MyRowType {
        public String Status { get; set; }

        public String RowColor
        {
            get{
                switch (Status)
                {
                    case "RUNNING":
                        return "0000FF";
                    case "FAILED":
                        return "FF0000";
                    default:
                        return "00FF00";
                }
            }
        }
    }

The status column was there before, but now the entire row is colored depending on the value in the "Status" field.

0
source
grid.Column("ColumnName", canSort: true,
    format: (item) =>
    {
        String Oabsent=string.Empty;
        if (item.ColumnName)
        {
            Oabsent += 
                 "<span style=\"color:#FF0000 ; display:table\">" 
                 + item.ColumnName+ "</span><br/>";
        }
        else { Oabsent += item.ColumnName; }

        return new HtmlString(Oabsent);
    }),
0
source

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


All Articles