Knockout.js MVCContrib网格-我可以指定tbody属性吗?

Knockout.js MVCContrib网格-我可以指定tbody属性吗?,knockout.js,mvccontrib,mvccontrib-grid,Knockout.js,Mvccontrib,Mvccontrib Grid,我正在尝试与一起使用MVCContrib网格。为此,我必须在tbody中指定数据绑定,例如。我找不到做这件事的方法 @Html.Grid(Model).Attributes()将我的绑定应用于标记。有没有办法设置tbody属性?简短的回答是没有,在当前的实现中没有办法在tbody上设置属性 但您可以自己实现此功能: 您只需要从GridRenderer类中实现自己版本的renderbart方法 有一个名为HtmlTableGridRenderer的GridRenderer的实现,您可以在此基础上构

我正在尝试与一起使用MVCContrib网格。为此,我必须在tbody中指定数据绑定,例如
。我找不到做这件事的方法


@Html.Grid(Model).Attributes()
将我的绑定应用于
标记。有没有办法设置
tbody
属性?

简短的回答是没有,在当前的实现中没有办法在
tbody
上设置属性

但您可以自己实现此功能:

您只需要从
GridRenderer
类中实现自己版本的
renderbart
方法

有一个名为
HtmlTableGridRenderer
GridRenderer
的实现,您可以在此基础上构建:

public class BodyWithAttributesHtmlTableGridRenderer<T> 
    : HtmlTableGridRenderer<T> where T : class
    {
        private readonly IDictionary<string, object> bodyAttributes;

        public BodyWithAttributesHtmlTableGridRenderer(
            IDictionary<string, object> bodyAttributes)
        {
            this.bodyAttributes = bodyAttributes;
        }

        protected override void RenderBodyStart()
        {
            string str = BuildHtmlAttributes(bodyAttributes);
            if (str.Length > 0)
                str = " " + str;
            RenderText(string.Format("<tbody{0}>", str));
        }
    }
生成的html将如下所示:

<table class="grid">
    <thead>
        <tr>
            <th>Prop</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: people">
        <tr class="gridrow">
            <td>1</td>
        </tr>
        <tr class="gridrow_alternate">
            <td>2</td>
        </tr>
    </tbody>
</table>

道具
1.
2.

您应该注意,这只是一个快速而肮脏的解决方案,可以显示哪些是可能的,并且有更多扩展点,您可以使用这些扩展点来使属性传递更加友好。

谢谢!事实上,我找到了这个链接,它解释了与您相同的解决方案。
<table class="grid">
    <thead>
        <tr>
            <th>Prop</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: people">
        <tr class="gridrow">
            <td>1</td>
        </tr>
        <tr class="gridrow_alternate">
            <td>2</td>
        </tr>
    </tbody>
</table>