我有一个Kendo网格,用户可以通过上下移动行来更改行的顺序。我通过递增或递减1来调整订单字段。我现在需要重新排序网格中的其余项目。将数据传递回控制器,现在我正在尝试重新排序列表。我觉得我很亲密,但不能完全正确。有人可以帮我弄清楚如何做到这一点吗?
示例:
从这个网格开始
Test | Order
A | 1
B | 2
C | 3
D | 4
用户点击B行和向上箭头,以便网格现在如下所示:
Test | Order
B | 1
A | 1
C | 3
D | 4
这部分工作正常,所以我认为不需要任何代码。这是我需要帮助的地方。所以我到了控制器,我有一个这样的列表:
[0] Test: B, Order: 1
[1] Test: A, Order: 1
[2] Test: C, Order: 3
[3] Test: D, Order: 4
有两个记录,其中订单等于1.如何重新排序列表(以便我可以将新订单保存到数据库?
我想要的是一个如下所示的列表:
[0] Test: B, Order: 1
[1] Test: A, Order: 2
[2] Test: C, Order: 3
[3] Test: D, Order: 4
我提出了这个解决方案,但递归部分无法正常工作。我收到的清单是空白的。有人能找到什么问题吗?
[HttpPost]
public ActionResult UpdateComments([DataSourceRequest] DataSourceRequest request, ModelComment model)
{
List<ModelComment> comments = new List<ModelComment>();
comments.Add(model);
List<ModelComment> otherComments = data.GetCommentsByAnalysisID(model.analysisID);
List<ModelComment> changedComments = ChangeDuplicateOrders(otherComments, model.commentsSelectedID, model.order);
return Json(comments.ToDataSourceResult(request));
}
private List<ModelComment> ChangeDuplicateOrders(List<ModelComment> comments, int ID, int order)
{
List<ModelComment> changedComments = new List<ModelComment>();
foreach (ModelComment comment in comments)
{
if (comment.commentsSelectedID != ID)
{
ModelComment changedComment = comment;
if (comment.order == order)
{
changedComment.order = order + 1;
changedComments.Add(changedComment);
}
List<ModelComment> remainingComments = new List<ModelComment>();
remainingComments = comments;
remainingComments.Remove(changedComment);
List<ModelComment> processedcomments = ChangeDuplicateOrders(remainingComments, changedComment.commentsSelectedID, changedComment.order);
changedComments = (changedComments.Concat(processedcomments)).ToList();
}
}
return changedComments;
}
编辑: 这是视图,以便您可以在客户端看到我正在做的事情:
@using (Html.BeginForm("LabApprovals_History", "Home"))
{
<div class="breadcrumb">
@Html.Raw(@ClsUtility.GetCurrentCrumb("Lab Approvals Comments"))
</div>
if (@ViewBag.Message != null && @ViewBag.Message != "")
{
<div id="divErrorMessage" style="color: #1A78C2;">
<br />
@Html.Raw(@ViewBag.Message)
</div>
<br />
<div style="clear: both">
<br />
</div>
}
<br />
<div style="text-align: center; padding-left: 10px;">
<div id="grid"></div>
@(Html.Kendo().Grid<DALubeBarcode.Models.ModelComment>().Name("gridComments")
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("ReadComments", "Home", new { analysisID = Model.analysisID }))
.Update(update => update.Action("UpdateComments", "Home"))
.Model(model => model.Id(p => p.commentsSelectedID))
)
.ToolBar(toolbar => toolbar.Save())
.Columns(columns =>
{
columns.Bound(m => m.commentsSelectedID).Visible(false);
columns.Bound(m => m.commentText).Title("Comment").Width("500px");
columns.Bound(m => m.order)
.ClientTemplate("<input type='button' class='k-button' onclick=up(\'#=uid#\') value='up' />").Title("");
columns.Bound(m => m.order)
.ClientTemplate("<input type='button' class='k-button' onclick=down(\'#=uid#\') value='down' />").Title("");
columns.Bound(m => m.order).Title("Order").Width("50px");
})
.Sortable()
.HtmlAttributes(new { style = "height:600px;" })
.Resizable(resize => resize.Columns(true))
.Editable(editable => editable.Mode(GridEditMode.InCell))
)
</div>
}
<script>
function up(uid) {
var grid = $("#gridComments").data("kendoGrid");
var dataItem = grid.dataSource.getByUid(uid);
dataItem.order = dataItem.order - 1;
var index = grid.dataSource.indexOf(dataItem);
var newIndex = Math.max(0, index - 1);
if (newIndex != index) {
grid.dataSource.remove(dataItem);
grid.dataSource.insert(newIndex, dataItem);
}
var dataSource = $("#gridComments").data("kendoGrid").dataSource;
dataSource.data()[newIndex].dirty = true;
dataSource.sync();
return false;
}
function down(uid) {
var grid = $("#gridComments").data("kendoGrid");
var dataItem = grid.dataSource.getByUid(uid);
dataItem.order = dataItem.order + 1;
var index = grid.dataSource.indexOf(dataItem);
var newIndex = Math.min(grid.dataSource.total() - 1, index + 1);
if (newIndex != index) {
grid.dataSource.remove(dataItem);
grid.dataSource.insert(newIndex, dataItem);
}
var dataSource = $("#gridComments").data("kendoGrid").dataSource;
dataSource.data()[newIndex].dirty = true;
dataSource.sync();
return false;
}
答案 0 :(得分:0)
这样的东西可行,但我没有时间检查语法,所以要注意大写等等:
private void reorder (myList, oldIndex, newIndex) {
ModelComment itemToMove = myList.First(x=>x.order==oldIndex);
ModelComment insertPoint = myList.First(x=>x.order==newIndex);
myList.Remove(itemToMove);
if (newIndex < oldIndex) {
myList.select(x=>x.order>=newIndex && x.order<oldIndex).foreach(x=>x.order++);
} else {
myList.select(x=>x.order<=newIndex && x.order>oldIndex).foreach(x=>x.order--);
}
itemToMove.order = newIndex;
myList.Insert(myList.IndexOf(insertPoint), itemToMove);
}