簡體   English   中英

對於C#列表,當一條記錄的值更改時,如何對列進行重新排序?

[英]For a C# List, how do I reorder a column when one record's value changes?

我有一個Kendo網格,用戶可以在其中通過上下移動行來更改行的順序。 我通過遞增或遞減一個來調整訂單字段。 現在,我需要重新排列網格中其余項目的順序。 將數據傳遞回控制器,現在正嘗試重新排序列表。 我覺得我已經接近了,但不能完全正確。 有人可以幫我弄清楚該怎么做嗎?

例:

從這個網格開始

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;
}

這樣的事情會起作用,但是我沒有時間檢查語法,因此請注意大寫等:

 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);
 } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM