我正在使用Kendo Sortable来对网格中的行进行拖放重新排序(类似于to this example。我的网格还允许对行进行内联编辑。
在以下两种情况下,都将调用相同的数据源Update方法:内联编辑后单击update,或拖放一行以重新排序。
是否可以通过拖放重新排序来调用其他数据源Update方法?
这是网格代码:
@(Html.Kendo().Grid<BSS.WebMvc.ViewModels.SuggestionViewModel>()
.Name("gridSuggestions")
.Columns(columns =>
{
columns.Bound(p => p.DisplayOrder).Hidden(true);
columns.Bound(p => p.Item);
columns.Bound(p => p.Quantity);
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.ToolBar(toolbar => toolbar.Create().Text("Add New Suggestion"))
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("gridSuggestions_error_handler"))
.Model(model =>
{
model.Id(p => p.ID);
model.Field(p => p.DisplayOrder).DefaultValue(0);
model.Field(p => p.Item).DefaultValue(new BSS.WebMvc.ViewModels.ResultEntryViewModel());
model.Field(p => p.Quantity).DefaultValue(1);
})
.Create(update => update.Action("Suggestions_Create", "Items", new { parentItemId = Model.ID }))
.Read(read => read.Action("Suggestions_Read", "Items", new { id = Model.ID }))
.Update(update => update.Action("Suggestions_Update", "Items", new { parentItemId = Model.ID }))
.Destroy(update => update.Action("Suggestions_Delete", "Items", new { parentItemId = Model.ID }))
.ServerOperation(false)
.Sort(s => s.Add(m => m.DisplayOrder))
)
.Events(events => events.DataBound("gridSuggestions_dataBound").Edit("gridSuggestionEditOrCreate").SaveChanges("gridSuggestionSaveChanges").Cancel("gridSuggestionCancel").Save("gridSuggestionSave"))
)
以及具有相关javascript函数的可排序代码:
@(Html.Kendo().Sortable()
.Name("gridSuggestionSortable")
.For("#gridSuggestions")
.Filter("table > tbody > tr:not(.k-grid-edit-row):not(.blockDragDuringEdit):not(.preventDrag):not(.k-detail-row)")
.Cursor("move")
.HintHandler("hint")
.Axis(SortableAxis.Y)
.PlaceholderHandler("placeholder")
.ContainerSelector("#gridSuggestions tbody")
.Events(events => events.Change("onGridSuggestionsOrderChange"))
)
<script>
function onGridSuggestionsOrderChange(e) {
let grid = e.sender.element.data("kendoGrid"),
oldIndex = e.oldIndex,
newIndex = e.newIndex,
view = grid.dataSource.view(),
dataItem = grid.dataSource.getByUid(e.item.data("uid"));
dataItem.DisplayOrder = newIndex;
dataItem.dirty = true;
if (oldIndex < newIndex) {
for (let i = oldIndex + 1; i <= newIndex; i++) {
view[i].DisplayOrder--;
view[i].dirty = true;
}
} else {
for (let i = oldIndex - 1; i >= newIndex; i--) {
view[i].DisplayOrder++;
view[i].dirty = true;
}
}
grid.dataSource.sync();
}
</script>
答案 0 :(得分:1)
您可以在变更处理程序中调用其他方法:
function onGridSuggestionsOrderChange(e) {
...
dataItem.DisplayOrder = newIndex;
const updated = [dataItem.toJSON()];
if (oldIndex < newIndex) {
for (let i = oldIndex + 1; i <= newIndex; i++) {
view[i].DisplayOrder--;
updated.push(view[i].toJSON());
}
} else {
for (let i = oldIndex - 1; i >= newIndex; i--) {
view[i].DisplayOrder++;
updated.push(view[i].toJSON());
}
}
$.ajax({
type: "POST",
url: REORDER_UPDATE_URL,
contentType: 'application/json',
data: JSON.stringify(updated)
});
}