我刚开始使用角度4进行开发,我有一个要求。我想知道实现以下内容的最佳方法。
我想在我的网站上创建一个下拉菜单,该菜单显示在我网站的标题上。点击Dropdowm菜单图片,我想要一个菜单打开,当它悬停时会再次打开它们的子菜单。
我的HTML是
<span ngbDropdown #myDrop="ngbDropdown" class="dropdown-wrapper">
<img class="hidden-md-up" src="More Menu.svg" aria-hidden="true" ngbDropdownToggle/>
<span ngbDropdownMenu aria-labelledby="dropdownBasic1">
<p class="dropdown-label"><img src="selectall.svg" class="dropdown-item">Select All</p>
<p class="dropdown-label"><img src="filter.svg" class="dropdown-item">Filter</p>
<p class="dropdown-label"><img src="export.svg" class="dropdown-item">Export</p>
<p class="dropdown-label"><img src="edit.svg" class="dropdown-item">Bulk Edit</p>
<p class="dropdown-label"><img src="sort.svg" class="dropdown-item">Sort</p>
<p class="dropdown-label"><img src="tileview.svg" class="dropdown-item" (click)="openSubMenu()">
Tile View
</p>
</span>
</span>
目前我在我的html中使用了ANGULAR-BOOTSTRAP Dropdown来实现这一点,但是我无法创建子菜单。我想在上面提到的代码中点击我的最后一项打开子菜单。
提前感谢您的帮助。
答案 0 :(得分:4)
好吧,如果您还在使用Angular-Bootstrap,您可以轻松使用手风琴模块。如果您正在寻找扩展现有下拉模块的方法,那么您可能不需要我的解决方案。它可能比你想象的要多一些代码,但请注意结构基本上是树视图的实现,所以请随意使用它。
我将提出一个总体思路,这个概念将来自我最近实施的树形视图。请注意,该代码将为您提供如何实现您想要实现的功能的好主意。复制粘贴应该工作,但不能保证。如果你想深入理解它,你应该阅读整个代码。
为了使用代码,我建议您需要一些基本结构来指示您的导航结构。一些简单的json
或ts对象应该完成这项工作。我考虑过这样的结构:
structure = [
{
title: "foo",
link: "/foo",
children: [
{
title: "bar",
link: "/bar",
children: []
}
]
},
{
title: "baz",
link: "/baz",
children: []
}
];
你应该明白这个想法。这将为您提供一个易于更改和实现的基础结构,您只需更改对象中的详细信息即可更改结构。将其放在要呈现导航的父组件中。你可以做到与众不同,但我喜欢使用角度潜力的想法,并使代码非常通用。如果你是一名程序员,你就会明白需要一个易于使用的界面,它基本上可以自己完成所有事情。
要显示结构,您还需要一个TreeViewComponent,它可以具有以下标记:
<ul class="tree-root-list">
<app-tree-node *ngFor="let item of structure"
[current]="item">
</app-tree-node>
</ul>
正如您应该认识到的那样,为了显示您的下拉树导航菜单,您还需要另一个组件。 TreeNodeComponent应具有以下(ish)标记:
<li class="node-item">
// this function will trigger the final action triggered when a node is selected
// if there are subnodes open the list, else link to the respective page, route, etc..
<p #title (click)="nodeSelected()">
<span class="icon-container">
<span>
//optional icon
</span>
</span>
<span class="node-title">
// optional text
{{title}}
</span>
</p>
<ul class="child-list"
*ngIf="hasChildElements()"
[collapse]="children_collapsed"
(collapsed)="collapsed($event)"
(expanded)="expanded($event)">
// thats the part the structure object comes in handy,
// since you don't need to add several node tags
// you'll just need to add or remove elements in the structure
// variable
<app-tree-node *ngFor="let child of current.children"
[current]="child">
</app-tree-node>
</ul>
</li>
这是TreeNodeComponent的一些基本代码。只需根据您的需要进行调整,以使其正常工作。您想要的一般功能应该包括在内。
@Component({
selector : 'app-tree-node',
templateUrl: './tree-node.component.html',
styleUrls : ['tree-node.component.scss']
})
export class TreeNodeComponent implements OnInit {
// inputs
@Input() current: any;
@Input() is_root: boolean;
// ...
// public members
public children_collapsed = true;
// view nodes
// can be useful when you want to highlight certain nodes depending on specific actions
// though not a requirement for the main task
@ViewChildren(TreeNodeComponent) child_nodes: QueryList<TreeNodeComponent>;
/**
* c'tor ...
*/
/**
* click event when node is selected
*/
public nodeSelected(): void {
// if there are child elements
if (this.hasChildElements()) {
// change collapsed state
this.children_collapsed = !this.children_collapsed;
} else {
// probably route to somewhere, do whatever you want to do with no children present here
}
}
/**
* check whether there are children
*/
public hasChildElements(): boolean {
return this.current.hasOwnProperty("children");
}
/**
* fires if div is collapsed
* @param event
*/
public collapsed(event: any): void {
// change icon
// needed this to give a visual feedback whether the folder is opened or closed
if (this.hasChildElements()) {
this.folder_icon.nativeElement.className = "glyphicon glyphicon-folder-close";
}
}
/**
* fires if div is expanded
* @param event
*/
public expanded(event: any): void {
if (this.hasChildElements()) {
this.folder_icon.nativeElement.className = "glyphicon glyphicon-folder-open";
}
}
}
我不会给你样式表,因为这首先是一个完全矫枉过正,第二个css很奇怪,每个人都有不同的解决方案。 我确定你会弄清楚如何恰当地调整组件的样式。
同样无法保证提议的代码在复制和粘贴后仍然有效。原始项目变得非常庞大,具有许多不同的树功能,您在用例中不需要这些功能。因此,您可能不会需要一些变量。
干杯,玩得开心。