我希望使用箭头键向上和向下移动一个单元格或向下移动一个表格中的输入框。如果我在一个单元格中,我可以使用Tab键移动到下一个单元格,我希望能够对上下箭头进行同样的操作。我环顾四周,但没有找到任何解决方案(至少是我所理解的任何解决方案)。
这个html代码:
@namespace MasterData.V2.Client.Pages.Operation.StoreCollection.StoreEditCollection
<h3>TestTableNavigate</h3>
<table>
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<tr>
<th><input type="text" value="1"/></th>
<th><input type="text" value="2"/></th>
<th><input type="text" value="3"/></th>
</tr>
<tr>
<th><input type="text" value="4"/></th>
<th><input type="text" value="5"/></th>
<th><input type="text" value="6"/></th>
</tr>
<tr>
<th><input type="text" value="7"/></th>
<th><input type="text" value="8"/></th>
<th><input type="text" value="9"/></th>
</tr>
</tbody>
</table>
@code {
}对如何取得进展有什么建议吗?
彼得·施文尼森
发布于 2022-04-13 11:38:35
这是如何在表格单元格中实现键盘移动的方法。
这是你的剃刀页
<h3>TestTableNavigate</h3>
@inject IJSRuntime JS
<table id="myTable" @ref=@myTable @onkeydown="@keydown">
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" value="1"/></td>
<td><input type="text" value="2"/></td>
<td><input type="text" value="3"/></td>
</tr>
<tr>
<td><input type="text" value="4"/></td>
<td><input type="text" value="5"/></td>
<td><input type="text" value="6"/></td>
</tr>
<tr>
<td><input type="text" value="7"/></td>
<td><input type="text" value="8"/></td>
<td><input type="text" value="9"/></td>
</tr>
</tbody>
</table>
@code {
private ElementReference myTable;
private Cell cell = new();
public async Task keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
cell = await JS.InvokeAsync<Cell>("navigateTable", myTable , cell, args.Key);
}
public class Cell{
public int CurrentRow { get; set; } = 0;
public int CurrentColumn { get; set; } = 0;
}
}这是JavaScript函数,它处理单元格的移动
function navigateTable(myTable, cell, key) {
// get all rows in table
var mytablebody = myTable.getElementsByTagName("tbody")[0];
// count number of rows in table body
var rowCount = mytablebody.getElementsByTagName("tr").length;
// check whick key is pressed
if (key == "ArrowUp") {
cell.currentRow = cell.currentRow - 1;
cell.currentRow = (cell.currentRow < 0) ? 0 : cell.currentRow;
}
else if (key == "ArrowDown") {
cell.currentRow = cell.currentRow + 1;
cell.currentRow = (cell.currentRow > rowCount - 1) ? rowCount - 1 : cell.currentRow;
}
else if (key == "ArrowLeft") {
cell.currentColumn = cell.currentColumn - 1;
}
else if (key == "ArrowRight") {
cell.currentColumn = cell.currentColumn + 1;
}
var myrow;
// get current row
myrow= mytablebody.getElementsByTagName("tr")[cell.currentRow];
// get number of cells in current row
var cellCount = myrow.getElementsByTagName("td").length;
// Take care of first cell in row
if (cell.currentColumn < 0) {
if (cell.currentRow > 0) {
cell.currentRow = cell.currentRow - 1;
cell.currentColumn = cellCount - 1;
myrow = mytablebody.getElementsByTagName("tr")[cell.currentRow];
}
else {
cell.currentColumn = 0;
}
}
// take care of last cell in row
if (cell.currentColumn > cellCount - 1) {
if (cell.currentRow < rowCount-1) {
cell.currentRow = cell.currentRow + 1;
cell.currentColumn = 0;
myrow = mytablebody.getElementsByTagName("tr")[cell.currentRow];
}
else {
cell.currentColumn = cellCount - 1;
}
}
// get current cell
var mycel = myrow.getElementsByTagName("td")[cell.currentColumn];
// focus to new cell location
var txt = mycel.children[0];
txt.focus();
// return current row and column to code behind
return cell;
}https://stackoverflow.com/questions/71854185
复制相似问题