For a C# List, how do I reorder a column when one record's value changes? -
i have kendo grid user can change order of rows moving rows , down. adjust order field incrementing or decrementting one. need reorder rest of items in grid. pass data controller , trying reorder list. feel i'm close, can't quite right. can me figure out how this?
example:
start grid
test | order | 1 b | 2 c | 3 d | 4
the user clicks on b row , arrow grid looks this:
test | order b | 1 | 1 c | 3 d | 4
this part working fine don't think code necessary. here's need help. controller , have list this:
[0] test: b, order: 1 [1] test: a, order: 1 [2] test: c, order: 3 [3] test: d, order: 4
there 2 records order equals 1. how reorder list (so can save new order database?
what want list looks this:
[0] test: b, order: 1 [1] test: a, order: 2 [2] test: c, order: 3 [3] test: d, order: 4
i came solution, recursive part isn't working right. list returned blank. can find what's wrong?
[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; }
edit: here's view can see i'm doing on client side:
@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; }
something work, dont have time check syntax, watch out capitalization , such:
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); }
Comments
Post a Comment