網站首頁 編程語言 正文
準備
1.框架
.netcore? 版本?yishaadmin開源框架
2.模板?
本文模板使用adminlte3.0,文檔地址
3.菜單表關鍵字段?
id 表主鍵(當前菜單)
ParentId 父級ID(父級菜單 為0時為頂級菜單,也可能為內容)
MenuUrl 菜單地址(只有頁面有地址,本身菜單是空)
MenuType 菜單類型(1是菜單 2是頁面 3是按鈕)
MenuIcon 圖標樣式
4.菜單表實體
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Util;
namespace YiSha.Entity.SystemManage
{
[Table("SysMenu")]
public class MenuEntity : BaseExtensionEntity
{
[JsonConverter(typeof(StringJsonConverter))]
public long? ParentId { get; set; }
public string MenuName { get; set; }
public string MenuIcon { get; set; }
public string MenuUrl { get; set; }
public string MenuTarget { get; set; }
public int? MenuSort { get; set; }
public int? MenuType { get; set; }
public int? MenuStatus { get; set; }
public string Authorize { get; set; }
public string Remark { get; set; }
[NotMapped]
public string ParentName { get; set; }
}
}
本文是由于框架內置菜單不支持頂級菜單顯示為內容,以及菜單最多只支持三級菜單的問題,故進行了調整。
1.實現思路
下圖1區域渲染為菜單,菜單通過點擊URL將內容填充到2區域。??
2.編碼
2.1? 建立渲染內容填充方法
將傳進來的url通過ajax調用最終渲染到內容區域(id為#Content的Div中),其中beforeSend方法顯示Loadding 可根據需要自行調整。url為{area:exists}/{controller=Home}/{action=Index}以及{controller=Home}/{action=Index}根據框架配置填寫至菜單
function LoadContent(url) {
if (url == null || url == "")
return;
$.ajax({
url: url,
beforeSend: function (XHR) {
$.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> '
+ "加載中..." + '</div>', css: { border: "none", backgroundColor: 'transparent' } });
},
success: function (data) {
$("#Content").html(data);
setTimeout(function () { $.unblockUI(); }, 100);
},
error: function (data, status, e) {
$("#Content").html("頁面加載失敗," + data.status + "," + url + "<br />" + data.responseText);
setTimeout(function () { $.unblockUI(); }, 100);
}
});
}
2.2? 建立分部視圖
通過建立分部視圖MenuTree,循環傳入的菜單,初始化時先獲取父級ID(ParentId)為0并且類別(MenuType)不為按鈕的菜單集合進行循環,根據menuEntity.MenuUrl判斷是否為頁面,如果依然為菜單則使用Html.PartialAsync("MenuTree")調用自身來實現遞歸,第二次則根據ViewData["Menu"]傳入的當前id作為父級id來尋找子集,直到尋找到最后的層級。
@using System.Collections.Generic
@using YiSha.Entity.SystemManage;
@model List<MenuEntity>
@{
if (Model.Any())
{
long id = 0L;
var menu = ViewData["Menu"] as MenuEntity;
if (menu != null)
id = menu.Id.Value;
@foreach (var menuEntity in Model.Where(o => o.ParentId == id && o.MenuType != (int)MenuTypeEnum.Button))
{
var icno = string.IsNullOrEmpty(menuEntity.MenuIcon) ? "fa fa-comment" : menuEntity.MenuIcon;
@if (!string.IsNullOrEmpty(menuEntity.MenuUrl))
{
<li class="nav-item">
<a href="#" class="nav-link" onclick="LoadContent('@menuEntity.MenuUrl')">
<i class="nav-icon @icno"></i>
<p>
@menuEntity.MenuName
</p>
</a>
</li>
}
else
{
ViewData["Menu"] = menuEntity;
<li class="nav-item">
<a href="#" class="nav-link">
<i class="nav-icon @icno"></i>
<p>
@menuEntity.MenuName
<i class="fas fa-angle-left right"></i>
</p>
</a>
<ul class="nav nav-treeview">
@await Html.PartialAsync("MenuTree",
Model,new ViewDataDictionary(ViewData))
</ul>
</li>
}
}
}
}
2.3 調用分布視圖
<aside class="main-sidebar sidebar-dark-primary elevation-4" style="width:200px;position:fixed">
<!-- Brand Logo -->
<!-- Sidebar -->
<div class="sidebar">
<!-- Sidebar Menu -->
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<li class="nav-header" style="font-size:1.0rem">
<img src="~/yisha/img/logo1.png" style="width: 30px; height: 30px; " />
任務管理系統
</li>
@await Html.PartialAsync("MenuTree", Model)
</ul>
</nav>
<!-- /.sidebar-menu -->
</div>
<!-- /.sidebar -->
</aside>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper" id="mainhead">
<div id="Content">
</div>
</div>
原文鏈接:https://www.cnblogs.com/zspwf/p/16202890.html
相關推薦
- 2023-05-26 Pycharm直接使用遠程服務器代碼并調試的解決方法_python
- 2022-09-22 Mybaits一級緩存和二級緩存分別是什么,區別是什么?
- 2022-06-09 pytorch分類模型繪制混淆矩陣以及可視化詳解_python
- 2022-11-04 golang?cache帶索引超時緩存庫實戰示例_Golang
- 2022-09-27 Android開發EditText實現密碼顯示隱藏_Android
- 2022-03-03 編輯時使用Object.assign({},row) el-form表單無法編輯 el-select
- 2022-03-21 Prometheus容器化部署的實踐方案_docker
- 2022-09-13 Android?Camera實現旋轉角度_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支