在 System.Web.Helpers.WebGrid 中定义内联行样式

发布于 2024-10-16 12:25:54 字数 440 浏览 2 评论 0 原文

我正在将我的应用程序移至 MVC 3 并尝试使用 System.Web.Helpers.WebGrid。我想获得像这样的html代码:

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

rowStyle 属性,允许为每一行定义css类,但每一行会有不同的样式。它容易实现吗?

I am moving my application to MVC 3 and try to use System.Web.Helpers.WebGrid. I would like to get html code like:

<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 rowStyle property, that allows to define css class for every row, but there will be different style for every row. Is it achieveable easily?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半枫 2024-10-23 12:25:54

所以我必须用 hack 来完成。首先,将颜色作为另一列的一部分。它必须作为 MvcHtmlString 返回以避免额外的编码:

<%  
    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>"))
        )
    ) 
%>

然后我们在 $(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>

So I had to finish with hack. First, include color as a part of another column. It had to be returned as MvcHtmlString to avoid additional 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 are setting bacground color in $(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>
青巷忧颜 2024-10-23 12:25:54

据我所知,除了指定 CSS 类之外,WebGrid 目前不支持行样式设置。

如果您想使用 WebGrid,那么我能想到的唯一替代方法是:

  1. 将列中的颜色值呈现为文本
  2. 使用 display:none 的列上的 CSS 类隐藏该列
  3. 使用jQuery 的位将 background-color 样式设置为隐藏列中文本颜色的样式

,这对我来说似乎是一个丑陋的黑客,所以我建议,如果您确实需要这种级别的控制您的表格显示,然后不必费心使用 WebGrid 并自行渲染 HTML。有很多关于实现您自己的分页和排序的信息。

As far as I know, WebGrid doesn't currently support styling on the row other than specifying the CSS class.

If you want to use WebGrid then the only alternative I can think of would be to:

  1. Render the colour values in a column as text
  2. Hide that column using a CSS class on the column with display:none
  3. Use a bit of jQuery to set the background-color style to that of the text colour in the hidden column

That seems like an ugly hack to me, so I would suggest, if you really need that level of control over your table display, then don't bother with the WebGrid and render the HTML yourself. There is a lot of information out there on implementing your own paging and sorting.

风柔一江水 2024-10-23 12:25:54

好吧,我在实现这一点时遇到了一些困难,所以我想根据已接受的答案展示我所做的事情。也许这会对你有所帮助。

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

我刚刚从我的行对象中检索了颜色,如下所示:

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";
                }
            }
        }
    }

状态列之前就在那里,但现在整行根据状态字段中的值进行着色。

Ok, I had a little difficulty implementing this so I wanted to show what I did based on the accepted answer. Maybe 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 just retrieved the Color from my Row object like this:

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 whole row is colored based on the value in the Status field.

忆悲凉 2024-10-23 12:25:54

我相信,您可以使用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>[email protected]</text>)
        )
    )
</div>

这应该将 css 类“alt”应用于结果网格中的交替行。

我从以下位置获取示例:

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

You can use the alternatingRowStyle: "(your CSS class for the alternating row)" to do this, I believe.

Like:

<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>[email protected]</text>)
        )
    )
</div>

This should apply the css class "alt" to alternating rows in the resulting grid.

I took the example from:

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

寒尘 2024-10-23 12:25:54
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);
    }),
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);
    }),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文