My idea is push the new order along the row id into an array and send the array to backend and then in your backend you can update your database. I also refer to this high vote answer to achieve the reset order feature.
Try my code below:
My controller:
public IActionResult Tickets()
{
List<TicketModel> list = new List<TicketModel>
{
new TicketModel{ rowid="aa", A="11",B="12",C="13",abc="hello"},
new TicketModel{ rowid="bb", A="21",B="22",C="23",abc="world"},
new TicketModel{ rowid="cc", A="31",B="32",C="33",abc="!!"}
};
return View(list);
}
[HttpPost]
public string resetOrder([FromBody] List<TicketModel> postData) {
return "success";
}
My cshtml:
@model IEnumerable<WebAppMvc.Models.TicketModel>
<div class="table-responsive">
<table class="table" id="myTable">
<thead>
<tr>
<th><div class="dragHandle"></div>A</th>
<th><div class="dragHandle"></div>B</th>
<th><div class="dragHandle"></div>C</th>
</tr>
</thead>
<tbody>
@{int i = 1;}
@foreach (var ticket in Model)
{
<tr class="sortable">
<td class="index" id="@ticket.rowid">@i</td>
@{i++;}
<td>@ticket.A</td>
<td>@ticket.B</td>
<td>@(ticket.C + "(" + ticket.abc + ")")</td>
</tr>
}
</tbody>
</table>
<button id="updateBtn">update</button>
</div>
@section scripts{
<script src="~/js/jquery.ui.js"></script>
<script src="~/js/jquery.dragtable.js"></script>
<script src="~/js/jquery.tablesorter.min.js"></script>
<script>
$(function () {
var reslist = [];
var fixHelperModified = function (e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function (index) {
$(this).width($originals.eq(index).width())
});
return $helper;
},
updateIndex = function (e, ui) {
$('td.index', ui.item.parent()).each(function (i) {
//$(this).html(i + 1);
var data = new Object();
data.rowid = $(this).attr("id");
data.order = (i + 1).toString();
reslist.push(data);
console.info(reslist);
});
};
$("#myTable")
.sortable({
items: "tr.sortable",
helper: fixHelperModified,
stop: updateIndex
})
.dragtable({ dragHandle: ".dragHandle" })
.tablesorter();
});
$('#updateBtn').click(function(){
$.ajax({
url: "/home/resetOrder",
data: JSON.stringify(reslist),
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
reslist = [];
}
});
});
</script>
}
My model:
public class TicketModel
{
public string rowid { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string abc { get; set; }
public string order { get; set; }
}
update the changes
means change the order of the data? I'm not sure which kind of data should be update