MVContrib グリッドをKnockoutJSと一緒に使用しようとしています。これを行うには、tbody でデータ バインディングを指定する必要があります<tbody data-bind="foreach: people">
。これを行う方法が見つかりません。
@Html.Grid(Model).Attributes()
私のバインディングを<table>
タグに適用します。tbody
属性を設定する方法はありますか?
MVContrib グリッドをKnockoutJSと一緒に使用しようとしています。これを行うには、tbody でデータ バインディングを指定する必要があります<tbody data-bind="foreach: people">
。これを行う方法が見つかりません。
@Html.Grid(Model).Attributes()
私のバインディングを<table>
タグに適用します。tbody
属性を設定する方法はありますか?
簡単な答えはノーtbody
です。現在の実装ではに属性を設定する方法はありません。
ただし、この機能は自分で実装できます。
RenderBodyStart
クラスから独自のバージョンのmothodを実装する必要がありますGridRenderer
。
あなたが構築できるものGridRenderer
と呼ばれるものの実装はすでにあり ます:HtmlTableGridRenderer
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));
}
}
また、ビューでは、呼び出す代わりに 、カスタムレンダラーを指定できるメソッドRender()
を使用できます。RenderUsing
@Html.Grid(Model))
.RenderUsing(new BodyWithAttributesHtmlTableGridRenderer<MyModel>(
new Dictionary<string, object>(){{"data-bind", "foreach: people"}}))
そして、生成された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>
これは、何が可能であるかを示すための迅速で汚い解決策であり、属性の受け渡しをより適切にするために使用できる拡張ポイントがさらにあることに注意してください。