当前位置:   article > 正文

vue 获取elementUI中的el-table里每一行的index并传到方法中_el-table获取index

el-table获取index

要获取Element UI中el-table中每一行的index并将其传递给方法,可以使用el-table中提供的row-index属性:

  • 通过在el-table组件上绑定@row-click事件,获取所点击的行的index:
<el-table @row-click="handleRowClick">
</el-table>
...
methods: {
  handleRowClick(row, event, column) {
    const rowIndex = event.target.parentNode.rowIndex - 1;
    ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 请注意,在处理handleRowClick方法时,我们需要计算出所点击的行在el-table数据数组中的位置,这里使用了event.target.parentNode.rowIndex-1来获取其索引值。

  • 使用element-ui中的slot-scope属性,将每一行的索引传递给自定义列组件中的方法

<el-table-column label="操作">
  <template slot-scope="scope">
    <el-button @click="handleClick(scope.$index)">点击</el-button>
  </template>
</el-table-column>
...
methods: {
  handleClick(rowIndex) {
    ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 在上述代码中,使用了Vue的slot-scope特性,将每一行的数据scope以及$index(表示当前行的索引)传递给了自定义列组件中的模板。这里我们直接将$index作为方法参数传入即可得到所点击的行的索引。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/279818
推荐阅读
相关标签
  

闽ICP备14008679号