开发者

ASP.NET MVC 3 WebGrid - Conditional Column Formatting

Is there anyway to do conditional formatting with Webgrid in ASP.NET MVC 3?

开发者_开发技巧

I know I can say: ... grid.Column("PropertyName", "Header Name", style: "bold") ...

and it will render HTML that for the TD that says: class="bold".

What I want, is to render some TDs in one style and other TDs in another style. Like: ... grid.Column("PropertyName", "Header Name", style: (item) => (item.Property > 100) ? "bold" : "normal")) ....

but this causes the error "Best overloaded method match ... has some invalid arguments."

Any idea if this is possible?

Thanks .Jim Biddison


I know I'm kind of late with the answer, but if someone is still looking for that kind of conditional formating / column value binding for WebGrid here's somehting that works :

@grid.GetHtml(
   columns: grid.Columns(
      grid.Column(format: (item) => (item.someproperty !=null) ? 
        Html.Raw("I've got value") : 
        Html.Raw("I don't :("))
   )
)


You can do this with some JQuery:

<script type='text/javascript'>
    $(document).ready(function () {
        jQuery.each($('tbody tr td'), function () {
            if (this.textContent == "some value") {
                $(this).addClass("some class");
            }
        });
    });
</script>

Of course, you'll have to modify the logic inside the each loop...

Hope that helps.


I don't think the style property accepts functions. You can use jQuery or here is a hack:

Hack


For googler, an improved version of the Torm answer:

@grid.GetHtml(
    columns: new[]
    {       
        grid.Column(format:item => Html.Raw("<span" + (item.Property > 100 ? " style='bold'" : "") + ">" + item.Property + "</span>")),
    }
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜