Dropdown下拉菜单

向下弹出的列表。

何时使用

当页面上的操作命令过多时,用此组件可以收纳操作元素。点击或移入触点,会出现一个下拉菜单。可在列表中进行选择,并执行相应的命令。

  1. import { NzDropDownModule } from 'ng-zorro-antd/dropdown';

代码演示Dropdown下拉菜单 - 图2

Dropdown下拉菜单 - 图3

基本

最简单的下拉菜单。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-basic',
  4. template: `
  5. <a nz-dropdown [nzDropdownMenu]="menu">
  6. Hover me
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu nzSelectable>
  11. <li nz-menu-item>1st menu item</li>
  12. <li nz-menu-item>2nd menu item</li>
  13. <li nz-menu-item>3rd menu item</li>
  14. </ul>
  15. </nz-dropdown-menu>
  16. `
  17. })
  18. export class NzDemoDropdownBasicComponent {}

Dropdown下拉菜单 - 图4

其他元素

分割线和不可用菜单项。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-item',
  4. template: `
  5. <a nz-dropdown [nzDropdownMenu]="menu">
  6. Hover me
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>1st menu item</li>
  12. <li nz-menu-item>2nd menu item</li>
  13. <li nz-menu-divider></li>
  14. <li nz-menu-item nzDisabled>3rd menu item(disabled)</li>
  15. </ul>
  16. </nz-dropdown-menu>
  17. `
  18. })
  19. export class NzDemoDropdownItemComponent {}

Dropdown下拉菜单 - 图5

触发事件

点击菜单项后会触发事件,用户可以自由添加各种事件进行不同的操作。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-event',
  4. template: `
  5. <a nz-dropdown [nzDropdownMenu]="menu">
  6. Hover me, Click menu item
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item (click)="log('1st menu item')">1st menu item</li>
  12. <li nz-menu-item (click)="log('2nd menu item')">2nd menu item</li>
  13. <li nz-menu-item (click)="log('3rd menu item')">3rd menu item</li>
  14. </ul>
  15. </nz-dropdown-menu>
  16. `
  17. })
  18. export class NzDemoDropdownEventComponent {
  19. log(data: string): void {
  20. console.log(data);
  21. }
  22. }

Dropdown下拉菜单 - 图6

多级菜单

传入的菜单里有多个层级。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-sub-menu',
  4. template: `
  5. <a nz-dropdown [nzDropdownMenu]="menu" (nzVisibleChange)="change($event)">
  6. Cascading menu
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>1st menu item</li>
  12. <li nz-menu-item>2nd menu item</li>
  13. <li nz-submenu nzTitle="sub menu">
  14. <ul>
  15. <li nz-menu-item>3rd menu item</li>
  16. <li nz-menu-item>4th menu item</li>
  17. </ul>
  18. </li>
  19. <li nz-submenu nzDisabled nzTitle="disabled sub menu">
  20. <ul>
  21. <li nz-menu-item>3rd menu item</li>
  22. <li nz-menu-item>4th menu item</li>
  23. </ul>
  24. </li>
  25. </ul>
  26. </nz-dropdown-menu>
  27. `
  28. })
  29. export class NzDemoDropdownSubMenuComponent {
  30. change(value: boolean): void {
  31. console.log(value);
  32. }
  33. }

Dropdown下拉菜单 - 图7

右键菜单

在区域内任意右击触发。

  1. import { Component } from '@angular/core';
  2. import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
  3. @Component({
  4. selector: 'nz-demo-dropdown-context-menu',
  5. template: `
  6. <div class="context-area" (contextmenu)="contextMenu($event, menu)">
  7. Right Click on here
  8. </div>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>1st menu item</li>
  12. <li nz-menu-item>2nd menu item</li>
  13. <li nz-menu-item nzDisabled>disabled menu item</li>
  14. <li nz-submenu nzTitle="sub menu">
  15. <ul>
  16. <li nz-menu-item>3rd menu item</li>
  17. <li nz-menu-item>4th menu item</li>
  18. </ul>
  19. </li>
  20. <li nz-submenu nzDisabled nzTitle="disabled sub menu">
  21. <ul>
  22. <li nz-menu-item>3rd menu item</li>
  23. <li nz-menu-item>4th menu item</li>
  24. </ul>
  25. </li>
  26. </ul>
  27. </nz-dropdown-menu>
  28. `,
  29. styles: [
  30. `
  31. .context-area {
  32. background: #f7f7f7;
  33. color: #777;
  34. text-align: center;
  35. height: 200px;
  36. line-height: 200px;
  37. }
  38. `
  39. ]
  40. })
  41. export class NzDemoDropdownContextMenuComponent {
  42. contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent): void {
  43. this.nzContextMenuService.create($event, menu);
  44. }
  45. closeMenu(): void {
  46. this.nzContextMenuService.close();
  47. }
  48. constructor(private nzContextMenuService: NzContextMenuService) {}
  49. }

Dropdown下拉菜单 - 图8

弹出位置

支持 6 个弹出位置。

  1. import { Component } from '@angular/core';
  2. import { NzPlacementType } from 'ng-zorro-antd/dropdown';
  3. @Component({
  4. selector: 'nz-demo-dropdown-placement',
  5. template: `
  6. <div>
  7. <ng-container *ngFor="let position of listOfPosition">
  8. <button nz-button nz-dropdown [nzDropdownMenu]="menu" [nzPlacement]="position">{{ position }}</button>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>1st menu item length</li>
  12. <li nz-menu-item>2nd menu item length</li>
  13. <li nz-menu-item>3rd menu item length</li>
  14. </ul>
  15. </nz-dropdown-menu>
  16. </ng-container>
  17. </div>
  18. `,
  19. styles: [
  20. `
  21. [nz-button] {
  22. margin-right: 8px;
  23. margin-bottom: 8px;
  24. }
  25. `
  26. ]
  27. })
  28. export class NzDemoDropdownPlacementComponent {
  29. listOfPosition: NzPlacementType[] = ['bottomLeft', 'bottomCenter', 'bottomRight', 'topLeft', 'topCenter', 'topRight'];
  30. }

Dropdown下拉菜单 - 图9

触发方式

默认是移入触发菜单,可以点击触发。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-trigger',
  4. template: `
  5. <a nz-dropdown nzTrigger="click" [nzDropdownMenu]="menu">
  6. Click me
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>1st menu item</li>
  12. <li nz-menu-item>2nd menu item</li>
  13. <li nz-menu-divider></li>
  14. <li nz-menu-item nzDisabled>disabled menu item</li>
  15. <li nz-submenu nzTitle="sub menu">
  16. <ul>
  17. <li nz-menu-item>3rd menu item</li>
  18. <li nz-menu-item>4th menu item</li>
  19. </ul>
  20. </li>
  21. <li nz-submenu nzDisabled nzTitle="disabled sub menu">
  22. <ul>
  23. <li nz-menu-item>3rd menu item</li>
  24. <li nz-menu-item>4th menu item</li>
  25. </ul>
  26. </li>
  27. </ul>
  28. </nz-dropdown-menu>
  29. `
  30. })
  31. export class NzDemoDropdownTriggerComponent {}

Dropdown下拉菜单 - 图10

带下拉框的按钮

左边是按钮,右边是额外的相关功能菜单。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-dropdown-button',
  4. template: `
  5. <nz-button-group>
  6. <button nz-button (click)="log()">DropDown</button>
  7. <button nz-button nz-dropdown [nzDropdownMenu]="menu1" nzPlacement="bottomRight">
  8. <i nz-icon nzType="ellipsis"></i>
  9. </button>
  10. </nz-button-group>
  11. <nz-button-group>
  12. <button nz-button (click)="log()">DropDown</button>
  13. <button nz-button nz-dropdown [nzDropdownMenu]="menu2" nzPlacement="bottomRight">
  14. <i nz-icon nzType="user"></i>
  15. </button>
  16. </nz-button-group>
  17. <nz-button-group>
  18. <button nz-button disabled>DropDown</button>
  19. <button nz-button disabled nz-dropdown [nzDropdownMenu]="menu3" nzPlacement="bottomRight">
  20. <i nz-icon nzType="ellipsis"></i>
  21. </button>
  22. </nz-button-group>
  23. <button nz-button nz-dropdown [nzDropdownMenu]="menu4">
  24. Button
  25. <i nz-icon nzType="down"></i>
  26. </button>
  27. <nz-dropdown-menu #menu1="nzDropdownMenu">
  28. <ul nz-menu>
  29. <li nz-menu-item>menu1 1st menu item</li>
  30. <li nz-menu-item>menu1 2nd menu item</li>
  31. <li nz-menu-item>menu1 3rd menu item</li>
  32. </ul>
  33. </nz-dropdown-menu>
  34. <nz-dropdown-menu #menu2="nzDropdownMenu">
  35. <ul nz-menu>
  36. <li nz-menu-item>menu2 1st menu item</li>
  37. <li nz-menu-item>menu2 2nd menu item</li>
  38. <li nz-menu-item>menu2 3rd menu item</li>
  39. </ul>
  40. </nz-dropdown-menu>
  41. <nz-dropdown-menu #menu3="nzDropdownMenu">
  42. <ul nz-menu>
  43. <li nz-menu-item>menu3 1st menu item</li>
  44. <li nz-menu-item>menu3 2nd menu item</li>
  45. <li nz-menu-item>menu3 3rd menu item</li>
  46. </ul>
  47. </nz-dropdown-menu>
  48. <nz-dropdown-menu #menu4="nzDropdownMenu">
  49. <ul nz-menu>
  50. <li nz-menu-item>menu4 1st menu item</li>
  51. <li nz-menu-item>menu4 2nd menu item</li>
  52. <li nz-menu-item>menu4 3rd menu item</li>
  53. </ul>
  54. </nz-dropdown-menu>
  55. `,
  56. styles: [
  57. `
  58. nz-button-group {
  59. margin: 0 8px 8px 0;
  60. }
  61. `
  62. ]
  63. })
  64. export class NzDemoDropdownDropdownButtonComponent {
  65. log(): void {
  66. console.log('click dropdown button');
  67. }
  68. }

Dropdown下拉菜单 - 图11

菜单隐藏方式

默认是点击关闭菜单,可以关闭此功能。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-overlay-visible',
  4. template: `
  5. <a nz-dropdown [nzDropdownMenu]="menu" [nzClickHide]="false" [(nzVisible)]="visible">
  6. Hover me
  7. <i nz-icon nzType="down"></i>
  8. </a>
  9. <nz-dropdown-menu #menu="nzDropdownMenu">
  10. <ul nz-menu>
  11. <li nz-menu-item>Clicking me will not close the menu.</li>
  12. <li nz-menu-item>Clicking me will not close the menu also.</li>
  13. <li nz-menu-item (click)="visible = false">Clicking me will close the menu</li>
  14. </ul>
  15. </nz-dropdown-menu>
  16. `
  17. })
  18. export class NzDemoDropdownOverlayVisibleComponent {
  19. visible = false;
  20. }

API

[nz-dropdown]directive

参数说明类型默认值
[nzDropdownMenu]Dropdown 下拉菜单组件NzDropdownMenuComponent-
[nzDisabled]菜单是否禁用boolean-
[nzPlacement]菜单弹出位置‘bottomLeft’ | ‘bottomCenter’ | ‘bottomRight’ | ‘topLeft’ | ‘topCenter’ | ‘topRight’‘bottomLeft’
[nzTrigger]触发下拉的行为‘click’ | ‘hover’‘hover’
[nzClickHide]点击后是否隐藏菜单booleantrue
[nzVisible]菜单是否显示,可双向绑定boolean-
[nzBackdrop]是否在 nzTriggerclick时增加背景蒙版booleantrue
[nzOverlayClassName]下拉根元素的类名称string-
[nzOverlayStyle]下拉根元素的样式object-
(nzVisibleChange)菜单显示状态改变时调用,参数为 nzVisibleEventEmitter<boolean>-

菜单使用 nz-menu,还包括菜单项 [nz-menu-item],分割线 [nz-menu-divider]

nz-dropdown 下的 nz-menu 默认不可选中。如果需要菜单可选中,可以指定 <ul nz-menu nzSelectable>.

nz-dropdown-menucomponent

用于包裹菜单项,可以通过 nzDropdownMenu 模板变量导出后传入 [nz-dropdown]NzContextMenuService

注意:每个 nz-dropdown-menu 只能作为一个 [nz-dropdown] 的输入项

  1. <a nz-dropdown [nzDropdownMenu]="menu">Hover me</a>
  2. <nz-dropdown-menu #menu="nzDropdownMenu">
  3. <ul nz-menu>
  4. <li nz-menu-item>1st menu item</li>
  5. <li nz-menu-item>2nd menu item</li>
  6. <li nz-menu-item>3rd menu item</li>
  7. </ul>
  8. </nz-dropdown-menu>

NzContextMenuServiceservice

用于右键弹出下拉菜单,具体参见示例

方法/属性说明参数返回
create创建右键菜单($event:MouseEvent | {x:number, y:number}, menu:NzDropdownMenuComponent)-
close关闭右键菜单--