我发布此消息后的发现...
我是 Web 演示开发的新手,因此掌握客户端与服务器端元素的区别及其范围是关键。同样,学习剑道网格的各种细节也很有帮助。
继续我目前的解决方案...
获取从自定义命令事件中选择的行项的句柄未使用 Select() 完成,因为未选择行。正如之前在其他帖子中所述,这只是所需工作的一部分。在自定义命令事件处理程序 JavaScript 中(在下面的完整解决方案中再次看到):
var detailDataItem = this.dataItem($(e.target).closest("tr"));
我的解决方案:
在托管 Kendo Grid 的父窗口中:
@* Declare modal Kendo Grid window control *@
@helper ClientGrid()
{
@(Html.Kendo().Grid<Purevision.Models.Person>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.FirstName).Width(100);
columns.Bound(c => c.LastName).Width(130);
columns.Bound(c => c.Email).Width(140);
columns.Bound(c => c.Phone).ClientTemplate("#= (data.Phone) ? formatPhoneNumber(data.Phone) : 'none' #").Width(130);
columns.Bound(c => c.Comments).Hidden().Width(140);
columns.Bound(c => c.UserId).Hidden();
columns.Bound(c => c.Id).Hidden();
columns.Command(command =>
{
command.Custom("Archive").Click("archiveCommand");
command.Custom("Detail").Click("detailCommand");
}).Width(90);
})
.ToolBar(toolbar => toolbar.Create())
.Selectable(s => s.Mode(GridSelectionMode.Single))
.Events(e => e.Change("onChange").DataBound("onDataBound").DataBinding("onDataBinding"))
.Scrollable()
.Sortable()
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Edit"))
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(c => c.Id))
.Create(create => create.Action("People_Create", "Clients"))
.Read(read => read.Action("People_Read", "Clients"))
.Update(update => update.Action("People_Update", "Clients"))
.Destroy(update => update.Action("People_Destroy", "Clients"))
)
)
}
@* Declare modal Kendo Grid window control; MUST be named 'detailWindow' as referenced by partial view script *@
@(Html.Kendo().Window().Name("detailWindow")
.Title("Client Details")
.Visible(false)
.Modal(true)
.Draggable(true)
.Width(400)
.Content(@<text>
@Html.Partial("_edit", new Person())
</text>
)
<script type="text/javascript">
function detailCommand(e) {
var window = $("#detailWindow");
var kWnd = window.data("kendoWindow");
var data = this.dataItem($(e.target).closest("tr"));
e.preventDefault();
kendo.bind(window, data);
kWnd.center().open();
}
</script>
在 Kendo 模态窗口中呈现的局部视图 _edit.cshtml:
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-4">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<input type="button" id="updateButton" value="Update2" class="btn btn-default" />
在表单准备就绪期间连接按钮事件,该事件获取仍在客户端范围内的网格控件的句柄:
<script type="text/javascript">
// as mentioned by Tarek, bind each control's value attribute
$("#detailWindow [name]").each(function () {
var name = $(this).attr("name");
$(this).attr("data-bind", "value:" + name );
});
$(document).ready(function (e) {
var window = $("#detailWindow");
var grid = $("#grid").data("kendoGrid");
$("#updateButton").click(function (e) {
grid.saveChanges();
window.data("kendoWindow").close();
});
});
</script>
我愿意在这里提出重构建议。似乎有很多 JavaScript 客户端编码来完成针对 Kendo Grid 的自定义活动。 (叹气)不过,我很高兴拥有多功能性。 (微笑)
为了希望得到这个有用的答案,需要进行大量的重新编辑。让我知道。 ;)
参考:
Telerik Forums / Kendo UI Forum / Grid / How does Grid update its dataSource?