有editRow和UpdateRow函數可以在網格對象上調用。這應該讓你開始正確的軌道:
在這個例子中,我創建了兩個相同的網格,稱爲客戶端1和客戶端2與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()
)
當然這需要大量的錯誤處理。
你能在這個問題上取得進展嗎? – Igorrious