Dropdown下拉菜单

向下弹出的列表。

何时使用

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

代码演示

Dropdown下拉菜单 - 图1

基本

最简单的下拉菜单。

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

Dropdown下拉菜单 - 图2

其他元素

分割线和不可用菜单项。

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

Dropdown下拉菜单 - 图3

触发事件

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

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

Dropdown下拉菜单 - 图4

多级菜单

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

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

Dropdown下拉菜单 - 图5

右键菜单

在区域内任意右击触发。

  1. import { Component, TemplateRef } from '@angular/core';
  2. import { NzDropdownContextComponent, NzDropdownService, NzMenuItemDirective } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-dropdown-context-menu',
  5. template: `
  6. <div
  7. style="background: rgb(190, 200, 200); padding: 32px;text-align: center"
  8. (contextmenu)="contextMenu($event, template)"
  9. >
  10. <ng-template #template>
  11. <ul nz-menu nzInDropDown (nzClick)="close($event)">
  12. <li nz-menu-item>1st menu item</li>
  13. <li nz-menu-item>2nd menu item</li>
  14. <li nz-menu-item nzDisabled>disabled menu item</li>
  15. <li nz-submenu>
  16. <span title>sub menu</span>
  17. <ul>
  18. <li nz-menu-item>3rd menu item</li>
  19. <li nz-menu-item>4th menu item</li>
  20. </ul>
  21. </li>
  22. <li nz-submenu nzDisabled>
  23. <span title>disabled sub menu</span>
  24. <ul>
  25. <li nz-menu-item>3rd menu item</li>
  26. <li nz-menu-item>4th menu item</li>
  27. </ul>
  28. </li>
  29. </ul>
  30. </ng-template>
  31. <span style="color:#fff;font-size: 14px;">Context Menu</span>
  32. </div>
  33. `,
  34. styles: []
  35. })
  36. export class NzDemoDropdownContextMenuComponent {
  37. private dropdown: NzDropdownContextComponent;
  38. contextMenu($event: MouseEvent, template: TemplateRef<void>): void {
  39. this.dropdown = this.nzDropdownService.create($event, template);
  40. }
  41. close(e: NzMenuItemDirective): void {
  42. console.log(e);
  43. this.dropdown.close();
  44. }
  45. constructor(private nzDropdownService: NzDropdownService) {}
  46. }

Dropdown下拉菜单 - 图6

弹出位置

支持 6 个弹出位置。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-placement',
  4. template: `
  5. <div>
  6. <nz-dropdown [nzPlacement]="'bottomLeft'">
  7. <button nz-button nz-dropdown>bottomLeft</button>
  8. <ul nz-menu>
  9. <li nz-menu-item>
  10. <a>1st menu item length</a>
  11. </li>
  12. <li nz-menu-item>
  13. <a>2nd menu item length</a>
  14. </li>
  15. <li nz-menu-item>
  16. <a>3rd menu item length</a>
  17. </li>
  18. </ul>
  19. </nz-dropdown>
  20. <nz-dropdown [nzPlacement]="'bottomCenter'">
  21. <button nz-button nz-dropdown>bottomCenter</button>
  22. <ul nz-menu>
  23. <li nz-menu-item>
  24. <a>1st menu item length</a>
  25. </li>
  26. <li nz-menu-item>
  27. <a>2nd menu item length</a>
  28. </li>
  29. <li nz-menu-item>
  30. <a>3rd menu item length</a>
  31. </li>
  32. </ul>
  33. </nz-dropdown>
  34. <nz-dropdown [nzPlacement]="'bottomRight'">
  35. <button nz-button nz-dropdown>bottomRight</button>
  36. <ul nz-menu>
  37. <li nz-menu-item>
  38. <a>1st menu item length</a>
  39. </li>
  40. <li nz-menu-item>
  41. <a>2nd menu item length</a>
  42. </li>
  43. <li nz-menu-item>
  44. <a>3rd menu item length</a>
  45. </li>
  46. </ul>
  47. </nz-dropdown>
  48. <nz-dropdown [nzPlacement]="'topLeft'">
  49. <button nz-button nz-dropdown>topLeft</button>
  50. <ul nz-menu>
  51. <li nz-menu-item>
  52. <a>1st menu item length</a>
  53. </li>
  54. <li nz-menu-item>
  55. <a>2nd menu item length</a>
  56. </li>
  57. <li nz-menu-item>
  58. <a>3rd menu item length</a>
  59. </li>
  60. </ul>
  61. </nz-dropdown>
  62. <nz-dropdown [nzPlacement]="'topCenter'">
  63. <button nz-button nz-dropdown>topCenter</button>
  64. <ul nz-menu>
  65. <li nz-menu-item>
  66. <a>1st menu item length</a>
  67. </li>
  68. <li nz-menu-item>
  69. <a>2nd menu item length</a>
  70. </li>
  71. <li nz-menu-item>
  72. <a>3rd menu item length</a>
  73. </li>
  74. </ul>
  75. </nz-dropdown>
  76. <nz-dropdown [nzPlacement]="'topRight'">
  77. <button nz-button nz-dropdown>topRight</button>
  78. <ul nz-menu>
  79. <li nz-menu-item>
  80. <a>1st menu item length</a>
  81. </li>
  82. <li nz-menu-item>
  83. <a>2nd menu item length</a>
  84. </li>
  85. <li nz-menu-item>
  86. <a>3rd menu item length</a>
  87. </li>
  88. </ul>
  89. </nz-dropdown>
  90. </div>
  91. `,
  92. styles: [
  93. `
  94. [nz-button] {
  95. margin-right: 8px;
  96. margin-bottom: 8px;
  97. }
  98. `
  99. ]
  100. })
  101. export class NzDemoDropdownPlacementComponent {}

Dropdown下拉菜单 - 图7

触发方式

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

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-trigger',
  4. template: `
  5. <nz-dropdown [nzTrigger]="'click'">
  6. <a nz-dropdown> Click me <i nz-icon type="down"></i> </a>
  7. <ul nz-menu>
  8. <li nz-menu-item>1st menu item</li>
  9. <li nz-menu-item>2nd menu item</li>
  10. <li nz-menu-divider></li>
  11. <li nz-menu-item nzDisabled>disabled menu item</li>
  12. <li nz-submenu>
  13. <span title>sub menu</span>
  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>
  20. <span title>disabled sub menu</span>
  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>
  28. `,
  29. styles: []
  30. })
  31. export class NzDemoDropdownTriggerComponent {}

Dropdown下拉菜单 - 图8

带下拉框的按钮

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

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-dropdown-dropdown-button',
  4. template: `
  5. <div style="height: 28px;">
  6. <nz-dropdown-button (nzClick)="log()">
  7. DropDown
  8. <ul nz-menu>
  9. <li nz-menu-item>1st menu item</li>
  10. <li nz-menu-item>2nd menu item</li>
  11. <li nz-submenu>
  12. <span title>sub menu</span>
  13. <ul>
  14. <li nz-menu-item>3rd menu item</li>
  15. <li nz-menu-item>4th menu item</li>
  16. </ul>
  17. </li>
  18. </ul>
  19. </nz-dropdown-button>
  20. <nz-dropdown-button nzDisabled>
  21. DropDown
  22. <ul nz-menu>
  23. <li nz-menu-item>
  24. <a>1st menu item</a>
  25. </li>
  26. <li nz-menu-item>
  27. <a>2nd menu item</a>
  28. </li>
  29. <li nz-menu-item>
  30. <a>3rd menu item</a>
  31. </li>
  32. </ul>
  33. </nz-dropdown-button>
  34. <nz-dropdown>
  35. <button nz-button nz-dropdown><span>Button</span> <i nz-icon type="down"></i></button>
  36. <ul nz-menu>
  37. <li nz-menu-item>
  38. <a>1st menu item</a>
  39. </li>
  40. <li nz-menu-item>
  41. <a>2nd menu item</a>
  42. </li>
  43. <li nz-menu-item>
  44. <a>3rd menu item</a>
  45. </li>
  46. </ul>
  47. </nz-dropdown>
  48. </div>
  49. `,
  50. styles: [
  51. `
  52. nz-dropdown-button {
  53. margin-right: 8px;
  54. }
  55. `
  56. ]
  57. })
  58. export class NzDemoDropdownDropdownButtonComponent {
  59. log(): void {
  60. console.log('click dropdown button');
  61. }
  62. }

Dropdown下拉菜单 - 图9

菜单隐藏方式

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

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

API

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

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

nz-dropdowncomponent

需要在触发下拉菜单的元素上加入 [nz-dropdown] 标记用于定位元素位置

参数说明类型默认值
[nzDisabled]菜单是否禁用boolean-
[nzPlacement]菜单弹出位置'bottomLeft'|'bottomCenter'|'bottomRight'|'topLeft'|'topCenter'|'topRight''bottomLeft'
[nzTrigger]触发下拉的行为'click'|'hover''hover'
[nzClickHide]点击后是否隐藏菜单booleantrue
[nzVisible]菜单是否显示,可双向绑定boolean-
[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]directive

用于标定下拉菜单定位元素

nz-dropdown-buttoncomponent

参数说明类型默认值
[nzDisabled]菜单是否禁用boolean-
[nzPlacement]菜单弹出位置'bottomLeft'|'bottomCenter'|'bottomRight'|'topLeft'|'topCenter'|'topRight''bottomLeft'
[nzSize]按钮大小,和 nz-button 一致'large'|'small'|'default''default'
[nzType]按钮类型,和 nz-button 一致'primary'|'ghost'|'dashed'|'danger'|'default''default'
[nzTrigger]触发下拉的行为'click'|'hover''hover'
[nzClickHide]点击后是否隐藏菜单booleantrue
[nzVisible]菜单是否显示boolean-
(nzVisibleChange)菜单显示状态改变时调用,参数为 nzVisibleEventEmitter<boolean>-
(nzClick)点击左侧按钮的回调EventEmitter<MouseEvent>-

NzDropdownServiceservice

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

参数说明参数返回
create创建右键菜单($event:MouseEvent, template:TemplateRef<void>)NzDropdownContextComponent
close关闭右键菜单--