使用ag网格,我需要能够点击tab键,将焦点元素从当前选定的网格/单元格更改为网格外部页面上的下一个元素。问题是,tab键似乎被锁定在网格中,并且不会移动到数据表之外的下一个元素。
我在存储最后一个焦点单元格的单元格上有一个偶数监听器(用于存储最后一个位置,以便能够返回到网格中的先前焦点单元格),但需要让下一个焦点单元格位于数据网格之外:
const cells = document.getElementsByClassName('ag-cell');
[...cells].map(cell => {
cell.addEventListener("keydown", function(e) {
if(e.key === "Tab") {
let lastCell = cell.attributes[2].value;
console.log("Last Cell Focused: ", lastCell)
}
})
})
如何在按键时从网格中删除焦点选择到下一个可焦点页面元素?
以下是当前网格的PLNKR链接:
Link
=================================
更新
我已经更新了我的代码,它现在正在寻找文档中触发的事件,而不是将事件监听器附加到每个单元格。但是,我仍然遇到一个问题,那就是它没有得到
last_cell
价值观和观察
focus-visible
课上
hasFocusVisible
.
//on arrow right if last_call === header that has 'focus-visible', set focus to first cell in body
const headerCells = document.getElementsByClassName('ag-header-cell-label');
const last_cell = headerCells[headerCells.length-1].attributes[2];
const hasFocusVisible = document.querySelector('.ag-header-cell-label').classList.contains('focus-visible');
document.addEventListener("keydown", function(e) {
if(e.key === "ArrowRight") {
// if(hasFocusVisible && last_cell) {
console.log("EVENT TRIGGERED FROM: ", event.target, "Last Cell Value: ", last_cell, hasFocusVisible);
//if last_call and 'ag-header-cell-label' has 'focus-visible', set focus to first cell in body
const bodyCell = document.getElementsByClassName('ag-cell')[0];
// }
}
});
更新Plnkr:
Link
=====================================
更新2
我已将元素选择器更新为以下内容:
const last_cell = document.querySelector('.ag-header-cell:last-child');
const hasFocusVisible = document.querySelector('.ag-header-cell-label').classList.contains('.focus-visible');
document.addEventListener("keydown", function(e) {
console.log('document.activeElement', document.activeElement)
const activeElement = document.activeElement;
if(e.key === "ArrowRight" && activeElement) {
if(last_cell) {
console.log("EVENT TRIGGERED FROM: ", event.target, "Last Cell Value: ", last_cell, hasFocusVisible);
//if last_call and 'ag-header-cell-label' has 'focus-visible', set focus to first cell in body
const bodyCell = document.getElementsByClassName('ag-cell')[0];
}
}
else if(e.key === "ArrowDown"){
//look for first child in first row with same id as header and set focus
document.querySelector('.ag-cell').focus();
}
});
然而,
哈斯可聚光的
变量总是出现
false
当注销焦点可见类的DIV时。我不确定我的逻辑是否错误,或者它不能得到
焦点可见
课堂上
ag-header-cell-label
当事件侦听器被激发时。