【问题标题】:Angular 8 ng-bootstrap table header keyboard navigation on sort columns排序列上的 Angular 8 ng-bootstrap 表标题键盘导航
【发布时间】:2023-03-16 04:28:01
【问题描述】:

我正在构建一个带有可排序标题的数据表,如文档中的 example 所示。

问题

可排序的表格标题必须启用键盘导航才能访问。表格标题必须可以通过选项卡焦点进行选择,并且选定的列必须在按下回车按钮时触发排序功能。这样排序只能用于键盘导航(不需要鼠标)。

版本:

"@ng-bootstrap/ng-bootstrap": "^5.3.1",
"@angular/core": "~8.2.14",

尝试

我尝试向第 th 元素添加选项卡索引,这使元素选项卡聚焦。但是,按回车键不会触发排序功能。

<th scope="col" tabindex="0" sortable="name" (sort)="onSort($event)">Country</th>

我也试过添加(keydown)方法,但是键盘事件是不能赋值给排序事件的,因为没有传入column和direction参数所以不会起作用。

<th scope="col" tabindex="0" sortable="name" (keydown)="onSort($event)" (sort)="onSort($event)">Country</th>

Argument of type 'KeyboardEvent' is not assignable to parameter of type 'SortEvent'.
Type 'KeyboardEvent' is missing the following properties from type 'SortEvent': column, direction

示例

这是一个包含完整代码的堆栈闪电战: https://stackblitz.com/edit/angular-db3bux-g2z44n?file=src/app/table-complete.html

【问题讨论】:

  • 一种方法是将@HostListener 添加到您的指令中

标签: angular accessibility keyboard-events ng-bootstrap wai-aria


【解决方案1】:

我添加了@HostListener 并尝试了您的 stackblitz 似乎正在工作

export class NgbdSortableHeader {
  @HostListener('keydown', ['$event'])
  onKeyDown(e) {
    console.log(e);
    if(e.which === 13) { //only react to "Enter"
       this.rotate();
    }
  }

  @Input() sortable: SortColumn = '';
  @Input() direction: SortDirection = '';
  @Output() sort = new EventEmitter<SortEvent>();

  rotate() {
    this.direction = rotate[this.direction];
    this.sort.emit({column: this.sortable, direction: this.direction});
  }
}

【讨论】:

  • 谢谢!这是一个很好的开始,但按下回车键时似乎并没有真正触发排序功能。堆栈闪电战已更新。
  • 我的错,必须只是 ['$event'],更新答案
猜你喜欢
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2013-04-29
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
相关资源
最近更新 更多