首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NET6使用箭头键在表中从单元格移动到单元格

NET6使用箭头键在表中从单元格移动到单元格
EN

Stack Overflow用户
提问于 2022-04-13 08:21:25
回答 1查看 172关注 0票数 0

我希望使用箭头键向上和向下移动一个单元格或向下移动一个表格中的输入框。如果我在一个单元格中,我可以使用Tab键移动到下一个单元格,我希望能够对上下箭头进行同样的操作。我环顾四周,但没有找到任何解决方案(至少是我所理解的任何解决方案)。

这个html代码:

代码语言:javascript
运行
复制
@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 {

}

对如何取得进展有什么建议吗?

彼得·施文尼森

EN

Stack Overflow用户

发布于 2022-04-13 11:38:35

这是如何在表格单元格中实现键盘移动的方法。

这是你的剃刀页

代码语言:javascript
运行
复制
<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函数,它处理单元格的移动

代码语言: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;
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71854185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档