El-table中El-input 动态绑定ref 并获取焦点

 

 

一、表格需要加上::row-class-name="tableRowIndex"
methods中: tableRowIndex({row, rowIndex}) { //把每一行的索引放进row
      row.index = rowIndex;
     },

二、

<el-table-column label="名称" width="180">
  <template slot-scope="scope">
    <el-input v-model="scope.row.Name" :ref="'Input_'+scope.$index"></el-input>
  </template>
</el-table-column>

// ..每次添加一条信息时,自动将焦点聚焦到该行的名称input上。
this.$nextTick(_ => {
  if( this.list.length !== 0){
    this.list.forEach( (item, index) => {
      if( Number( index) === Number( this.list.length-1)) {
        this.$refs['Input_'+Number((item.index))].focus(); // 将最后一行的名称input框获取焦点。
      }
    });
  }
});

posted on 2022-03-14 15:37  _xinT  阅读(5402)  评论(0)    收藏  举报