簡體   English   中英

Angular 將焦點設置為表中的輸入 在組件中使用表對象

[英]Angular set focus to an input that is in a <td> of the table Using table object in component

我有一個動態填充數據的表。我有多個輸入標簽,我想在用戶按下右箭頭鍵時將輸入的焦點轉移到下一個 td 標簽中的下一個輸入(類似於 ms excel )

我有行和列的索引,並使用@ViewChild 創建了一個表對象,但我無法使用行和列的索引來確定目標 td

您可以使用已有的表ElementRef訪問特定cell (td)

@ViewChild('ITable') ITableRef: ElementRef;

focusTD(rowNum, cellNum)
   this.ITableRef.tBodies[0].rows[rowNum].cells[cellNum].focus()
}

ngAfterViewInit() {
  this.focusTD(0,1); // selects the first tr (row) and second td (cell)
}

表元素有tBodies ,我選擇的第一個tBodies假設你的表有一個tBody ,那么你應該選擇一個row (tr)然后就可以選擇特定的cell (td)

關注表的第一個輸入字段=>

focusTD(rowNum, cellNum) this.ITableRef.nativeElement.tBodies[0].rows[rowNum].cells[cellNum].getElementsByTagName('input')[0].focus() }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM