在 System.Web.Helpers.WebGrid 中定义内联行样式
我正在将我的应用程序移至 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类,但每一行会有不同的样式。它容易实现吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以我必须用 hack 来完成。首先,将颜色作为另一列的一部分。它必须作为
MvcHtmlString
返回以避免额外的编码:然后我们在
$(document).ready()
中设置背景颜色: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:Then we are setting bacground color in
$(document).ready()
:据我所知,除了指定 CSS 类之外,WebGrid 目前不支持行样式设置。
如果您想使用 WebGrid,那么我能想到的唯一替代方法是:
display:none
的列上的 CSS 类隐藏该列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:
display:none
background-color
style to that of the text colour in the hidden columnThat 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.
好吧,我在实现这一点时遇到了一些困难,所以我想根据已接受的答案展示我所做的事情。也许这会对你有所帮助。
我刚刚从我的行对象中检索了颜色,如下所示:
状态列之前就在那里,但现在整行根据状态字段中的值进行着色。
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.
I just retrieved the Color from my Row object like this:
The Status column was there before, but now the whole row is colored based on the value in the Status field.
我相信,您可以使用alternatingRowStyle:“(交替行的CSS类)”来执行此操作。
就像:
这应该将 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:
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