Bladeren bron

菜单管理支持批量保存排序

RuoYi 4 dagen geleden
bovenliggende
commit
6f9f1ed9b2

+ 12 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
@@ -143,6 +144,17 @@ public class SysMenuController extends BaseController
         return toAjax(menuService.updateMenu(menu));
     }
 
+    /**
+     * 保存菜单排序
+     */
+    @PostMapping("/updateSort")
+    @ResponseBody
+    public AjaxResult updateSort(@RequestParam String[] menuIds, @RequestParam String[] orderNums)
+    {
+        menuService.updateMenuSort(menuIds, orderNums);
+        return success();
+    }
+
     /**
      * 选择菜单图标
      */

+ 36 - 7
ruoyi-admin/src/main/resources/templates/system/menu/menu.html

@@ -35,7 +35,10 @@
                 <a class="btn btn-primary" onclick="$.operate.edit()" shiro:hasPermission="system:menu:edit">
 		            <i class="fa fa-edit"></i> 修改
 		        </a>
-                <a class="btn btn-info" id="expandAllBtn">
+		        <a class="btn btn-info" onclick="saveSort()">
+                    <i class="fa fa-sort-amount-asc"></i> 保存排序
+                </a>
+                <a class="btn btn-default" id="expandAllBtn">
                     <i class="fa fa-exchange"></i> 展开/折叠
                 </a>
 	        </div>
@@ -52,6 +55,7 @@
 		var removeFlag = [[${@permission.hasPermi('system:menu:remove')}]];
 		var datas = [[${@dict.getType('sys_show_hide')}]];
 		var prefix = ctx + "system/menu";
+		var originalOrders = {};
 
 		$(function() {
 		    var options = {
@@ -87,14 +91,20 @@
 		            title: '排序',
 		            width: '10',
 		            widthUnit: '%',
-		            align: "left"
+		            align: "center",
+		            formatter: function(value, row, index) {
+		            	var menuIdText = $.common.sprintf("<input type='hidden' name='menuIds' value='%s'>", row.menuId);
+                    	var orderNumText = $.common.sprintf("<input type='text' name='orderNums' value='%s' class='form-control' style='display: inline-block; width:60px; text-align:center;'>", row.orderNum);
+                    	originalOrders[row.menuId] = row.orderNum;
+                    	return menuIdText + orderNumText;
+                    }
 		        },
 		        {
 		            field: 'url',
 		            title: '请求地址',
 		            width: '15',
 		            widthUnit: '%',
-		            align: "left",
+		            align: "center",
 		            formatter: function(value, row, index) {
                     	return $.table.tooltip(value);
                     }
@@ -104,7 +114,7 @@
 		            field: 'menuType',
 		            width: '10',
 		            widthUnit: '%',
-		            align: "left",
+		            align: "center",
 		            formatter: function(value, item, index) {
 		                if (item.menuType == 'M') {
 		                    return '<span class="label label-success">目录</span>';
@@ -122,7 +132,7 @@
 		            title: '可见',
 		            width: '10',
 		            widthUnit: '%',
-		            align: "left",
+		            align: "center",
 		            formatter: function(value, row, index) {
 		            	if (row.menuType == 'F') {
 		                    return '-';
@@ -135,7 +145,7 @@
 		            title: '权限标识',
 		            width: '15',
 		            widthUnit: '%',
-		            align: "left",
+		            align: "center",
 		            formatter: function(value, row, index) {
                     	return $.table.tooltip(value);
                     }
@@ -144,7 +154,7 @@
 		            title: '操作',
 		            width: '20',
 		            widthUnit: '%',
-		            align: "left",
+		            align: "center",
 		            formatter: function(value, row, index) {
 		                var actions = [];
 		                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.menuId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
@@ -156,6 +166,25 @@
 		    };
 		    $.treeTable.init(options);
 		});
+
+		/* 保存排序-菜单 */
+		function saveSort() {
+			var changedMenuIds = [];
+			var changedOrderNums = [];
+		    $("input[name='menuIds']").each(function() {
+		        var menuId = $(this).val();
+		        var currentOrder = $(this).next("input[name='orderNums']").val();
+		        if (originalOrders[menuId] !== currentOrder) {
+		            changedMenuIds.push(menuId);
+		            changedOrderNums.push(currentOrder);
+		        }
+		    });
+		    if (changedMenuIds.length === 0) {
+		        $.modal.alertWarning("未检测到排序修改");
+		        return;
+		    }
+		    $.operate.post(prefix + "/updateSort", { "menuIds": changedMenuIds.join(","), "orderNums": changedOrderNums.join(",") });
+		}
 	</script>
 </body>
 </html>

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java

@@ -121,6 +121,14 @@ public interface SysMenuMapper
      */
     public int updateMenu(SysMenu menu);
 
+    /**
+     * 保存菜单排序
+     * 
+     * @param menuIds 菜单ID
+     * @param orderNums 排序ID
+     */
+    public void updateMenuSort(SysMenu menu);
+
     /**
      * 校验菜单名称是否唯一
      * 

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ISysMenuService.java

@@ -129,6 +129,14 @@ public interface ISysMenuService
      */
     public int updateMenu(SysMenu menu);
 
+    /**
+     * 保存菜单排序
+     * 
+     * @param menuIds 菜单ID
+     * @param orderNums 排序ID
+     */
+    public void updateMenuSort(String[] menuIds, String[] orderNums);
+
     /**
      * 校验菜单名称是否唯一
      * 

+ 28 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java

@@ -11,11 +11,14 @@ import java.util.List;
 import java.util.Set;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.domain.Ztree;
 import com.ruoyi.common.core.domain.entity.SysMenu;
 import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.mapper.SysMenuMapper;
 import com.ruoyi.system.mapper.SysRoleMenuMapper;
@@ -321,6 +324,31 @@ public class SysMenuServiceImpl implements ISysMenuService
         return menuMapper.updateMenu(menu);
     }
 
+    /**
+     * 保存菜单排序
+     * 
+     * @param menuIds 菜单ID
+     * @param orderNums 排序ID
+     */
+    @Transactional
+    public void updateMenuSort(String[] menuIds, String[] orderNums)
+    {
+        try
+        {
+            for (int i = 0; i < menuIds.length; i++)
+            {
+                SysMenu menu = new SysMenu();
+                menu.setMenuId(Convert.toLong(menuIds[i]));
+                menu.setOrderNum(orderNums[i]);
+                menuMapper.updateMenuSort(menu);
+            }
+        }
+        catch (Exception e)
+        {
+            throw new ServiceException("保存排序异常,请联系管理员");
+        }
+    }
+
     /**
      * 校验菜单名称是否唯一
      * 

+ 6 - 0
ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml

@@ -188,4 +188,10 @@
 		)
 	</insert>
 
+	<update id="updateMenuSort" parameterType="SysMenu">
+	    update sys_menu
+	    set order_num = #{orderNum}
+	    where menu_id = #{menuId}
+	</update>
+
 </mapper>