我的问题是关于订购可嵌套菜单。
我一直在开发带有可拖动和可放置嵌套菜单的动态菜单。当我拖动一个项目时,我可以在onDragFinished函数中成功地将它保存到我的数据库中。此外,我设法达到了parentId和childId。
我的问题是我无法达到订单数量。我尝试了太多方法,但我无法成功。
我应该通过 Ajax 将项目的订单号发送到我的控制器。
<script>
$(document).ready(function () {
$('#nestable').nestable({
onDragFinished: function (currentNode, parentNode) //parentId and childId
{ //Ajax
var type = parseInt($(currentNode).data('id'));
var type2 = parseInt($(parentNode).data('id'));
console.log(type);
console.log(type2);
$.ajax({
method: "POST",
url: '@Url.Action("ChangeNodePosition","Pages")',
data: {
currentNode: type,
parentNode: type2
},
success: function (result) {
if (result == true)
{
alert("Save changes are successful");
}
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("Unable to save new list order: " + jqXHR.responseText);
});
}
});
$('#nestable-menu').on('click', function (e) {
var target = $(e.target),
action = target.data('action');
if (action === 'expand-all') {
$('.dd').nestable('expandAll');
}
if (action === 'collapse-all') {
$('.dd').nestable('collapseAll');
}
});
$('#draggable_list').change(function () {
$('#nestable').toggleClass('drag_disabled drag_enabled');
});
$('#nestable').nestable('collapseAll');
});
</script>
这是我的DragStop函数,
dragStop: function (e) {
var list = this;
var el = this.dragEl.children(this.options.itemNodeName).first();
el[0].parentNode.removeChild(el[0]);
this.placeEl.replaceWith(el);
this.dragEl.remove();
var $parents = $(el[0]).parents('.' + list.options.itemClass);
var $parent = null;
if ($parents.length > 0) $parent = $parents[0];
list.options.onDragFinished(el[0], $parent);
this.el.trigger('change');
if (this.hasNewRoot) {
this.dragRootEl.trigger('change');
}
this.reset();
}
最后,这是我的控制器,
[HttpPost]
public JsonResult ChangeNodePosition(int currentNode, int? parentNode) //order number getireceğiz
{
Menu location = _menuService.GetByMenuId(currentNode);
if (parentNode == null)
{
location.OrderNumber = location.OrderNumber + 1;
location.ParentID = null;
_menuService.Update(location);
}
else
{
location.OrderNumber = location.OrderNumber + 1;
location.ParentID = parentNode;
_menuService.Update(location);
}
return this.Json(true);
}