从ajax侧边栏加载的带有bootstrap-4动态侧边栏菜单的Adminlte 3打开不起作用
Posted
技术标签:
【中文标题】从ajax侧边栏加载的带有bootstrap-4动态侧边栏菜单的Adminlte 3打开不起作用【英文标题】:Adminlte 3 with bootstrap-4 dynamic sidebar menu loaded from ajax sidebar open not working 【发布时间】:2022-01-03 04:05:26 【问题描述】:我想在带有 bootstrap 4 仪表板的 adminlte 3 仪表板中使用 AJAX 从数据库中绘制动态侧边栏菜单数据。当我使用 AJAX 动态加载侧边栏菜单数据时,侧边栏打开或折叠不起作用。我的 JSFiddle URL 如下所示。当静态数据推送到没有 ajax 的“.sidebar”类时,菜单打开/隐藏工作。但是使用 ajax 调用菜单打开/隐藏不起作用。
$(document).ready(function()
var url = "";
$.ajax(
url: url,
).done(function(resp)
//retrive data
var ret = '<nav class="mt-2"><ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="true"><li class="nav-item"><a href="#" class="nav-link"><i class="nav-icon fas fas fa-user"></i><p>Admin<i class="right fas fa-angle-left"></i></p></a><ul class="nav nav-treeview"><li class="nav-item"><a href="#add-order.php" class="nav-link"><i class="far fa-circle nav-icon"></i><p>Rahim</p></a></li></ul><ul class="nav nav-treeview"><li class="nav-item"><a href="#" class="nav-link"><i class="far fa-circle nav-icon"></i><p>Test</p></a></li></ul></li></ul></nav>';
$('.sidebar').html(ret);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body class="sidebar-mini sidebar-closed sidebar-collapse" style="height: auto;">
<!-- Site wrapper -->
<div class="wrapper">
<!-- Navbar -->
<nav class="main-header navbar navbar-expand navbar-primary navbar-dark">
<!-- Left navbar links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
</li>
<!--
<li class="nav-item d-none d-sm-inline-block">
<a href="#" class="nav-link">Home</a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="#" class="nav-link">Contact</a>
</li>
-->
</ul>
<!-- Right navbar links -->
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown user-menu">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">
<img src="public/images/login_man.png" class="user-image img-circle elevation-2" >
<span class="d-none d-md-inline">admin</span>
</a>
<ul class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
<!-- Menu Footer-->
<li class="user-footer">
<!--<a href="#" class="btn btn-default btn-flat">Profile</a>-->
<a href="logout.php" class="btn btn-danger btn-flat float-right">Sign out</a>
</li>
</ul>
</li>
<!--
<li class="nav-item">
<a class="nav-link" data-widget="fullscreen" href="#" role="button">
<i class="fas fa-expand-arrows-alt"></i>
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#" role="button">
<i class="fas fa-th-large"></i>
</a>
</li>
-->
</ul>
</nav>
<!-- /.navbar --><aside class="main-sidebar main-sidebar-custom sidebar-dark-primary elevation-4">
<a href="#" class="brand-link">
<i class="brand-image fas fa-tachometer-alt" style="opacity: .8"></i>
<span class="brand-text font-weight-light">Dashboard</span>
</a>
<div class="sidebar">
</div>
</aside>
<div class="container-fluid">
<div class="content-wrapper" style="min-height: 549px;">
</div>
</div>
</div>
</body>
【问题讨论】:
【参考方案1】:只要把代码放在成功函数后面就行了。
$('[data-widget="treeview"]').Treeview('init');
【讨论】:
以上是关于从ajax侧边栏加载的带有bootstrap-4动态侧边栏菜单的Adminlte 3打开不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何从侧边栏链接导航到同一页面上的新 bootstrap 4 选项卡?