【发布时间】: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