可以在网格对象上调用 editRow 和 UpdateRow 函数。这应该让你开始走上正轨:
在此示例中,我创建了两个相同的网格,分别称为 Clients1 和 Clients2,其中 clientId 是键。编辑按钮仅出现在 Clients1 网格上。
<script type="text/javascript">
//Finds the row to edit on Clients2 grid based on the cached ClientId.
//Passes this row to the editRow function.
function editRow() {
$('#Clients2').data('tGrid').editRow($($('#Clients2 table tr').find("td:contains(" +lastEditedClientId + ")").parent()))
}
//Finds the row to save on Clients2 grid based on the cached ClientId.
//Passes this row to the updateRow function.
function saveRow() {
$('#Clients2').data('tGrid').updateRow($($('#Clients2 table tr').find("td:contains(" + lastEditedClientId + ")").parent()))
}
var lastEditedClientId = -1;
//Attached to the Clients1 grid, onSave event
function onSave(e) {
lastEditedClientId = e.dataItem.ClientId; //Cache the primary key of the Clients1 grid
}
</script>
将事件附加到相应的按钮
<button id="editButton" onclick="editRow()">Edit Grid 2</button>
<button id="saveButton" onclick="saveRow()">Save Grid 2</button>
需要先编辑Clients1网格,否则不会设置lastEditedClientId。
@(Html.Telerik().Grid<Client>()
.Name("Clients1")
.Columns(columns =>
{
columns.Bound(o => o.ClientId);
columns.Bound(o => o.FirstName);
columns.Bound(o => o.LastName);
columns.Bound(o => o.City);
columns.Command(commands =>
{
commands.Edit();
}).Width(200);
})
.DataKeys(keys => keys.Add(c => c.ClientId))
.DataBinding(db1 => db1.Ajax().Select("_Index", "Home").Update("_Update", "Home"))
.ClientEvents(e => e.OnSave("onSave"))
.Pageable()
.Sortable()
.Filterable()
)
@(Html.Telerik().Grid<Client>()
.Name("Clients2")
.Columns(columns =>
{
columns.Bound(o => o.ClientId).ReadOnly(true);
columns.Bound(o => o.FirstName);
columns.Bound(o => o.LastName);
columns.Bound(o => o.City);
columns.Command(commands =>
{
commands.Edit();
}).Width(200).Visible(false); //Important to have the edit button column, otherwise the editRow() function doesn't work. Set it to be invisible.
})
.DataKeys(keys => keys.Add(c => c.ClientId))
.DataBinding(db1 => db1.Ajax().Select("_Index", "Home").Update("_Update", "Home"))
.Pageable()
.Sortable()
.Filterable()
)
当然,这将需要大量的错误处理。