从GridInForm C# project available from Telerik文件Index.cshtml包含此网格(以及其他内容):
@(Html.Kendo().Grid(Model.Products)
.Name("Products")
.ToolBar(tools => tools.Create().Text("Add new product"))
.Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom))
.Columns(columns =>
{
columns.Bound(p => p.Name).ClientTemplate("#= Name #" +
"<input type='hidden' name='Products[#= index(data)#].Name' value='#= Name #' />");
columns.Bound(p => p.ProductID).Hidden().ClientTemplate("#= ProductID #" +
"<input type='hidden' name='Products[#= index(data)#].ProductID' value='#= ProductID #' />");
columns.Command(command => command.Destroy()).Width(100);
})
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(p => p.ProductID);
model.Field(p => p.ProductID).Editable(false);
})
.ServerOperation(false)
)
)
而这个javascript函数:
function index(dataItem) {
var data = $("#Products").data("kendoGrid").dataSource.data();
return data.indexOf(dataItem);
}
我的问题是找到有关在模板中传递给data
函数的index
参数的更多信息(在调用ClientTemplate
中)。它是什么,它来自哪里?
这一行显示了index()
函数的用法:
"<input type='hidden' name='Products[#= index(data)#].ProductID' value='#= ProductID #' />"
此行中的data
是Kendo框架在解析客户端模板时创建的内置对象。它表示该特定行的数据对象。
你可以在这里阅读一下data
对象:http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq#how-do-i-use-a-javascript-function-in-a-column-client-template?在标题为“如何在列客户端模板中使用JavaScript函数?”的问题下。
如果你想知道它的外观,你可以在console.wirte(JSON.stringify(dataItem))
函数中添加一个index()
来查看它的确切结构。
编辑
此外,客户端模板只是一个文本字符串,Kendo Javascript在创建每一行中的每一列时进行解析。那时,它具有该行的数据对象,用于将值插入到网格中。 MVC只是实际在文档中生成HTML的Javascript库的包装器。