1. 获取当前行选中的index
<el-table ref="tableF" :row-class-name="tableRowClassName" @row-click="onRowClick" highlight-current-row :data="tableData" style="width: 100%" v-loading="tableLoading"> <el-table-column prop="name" show-overflow-tooltip label="策略名称"></el-table-column> <el-table-column prop="issue_time" show-overflow-tooltip label="下发时间"> <template slot-scope="scope">{{scope.row.issue_time && scope.row.issue_time!=0 ? $moment.unix(scope.row.issue_time).format("YYYY-MM-DD HH:mm:ss") : '--'}}</template> </el-table-column> <el-table-column label="操作" width="80"> <template slot-scope="scope"> <el-button type="text" @click.stop="detailsDialog(scope.row)">详情</el-button> </template> </el-table-column> </el-table>
首先,给table加一个属性::row-class-name="tableRowClassName"
tableRowClassName({row, rowIndex}) { row.row_index = rowIndex; }
然后给表格添加: @row-click = "onRowClick"
onRowClick (row, event, column) { this.currentRowIndex = row.row_index; }
这时属性: currentRowIndex 存的就是当前选中行的index。
2. table默认选中行
this.tableData = res.Data.list; this.$refs.tableF.setCurrentRow(this.tableData[this.currentRowIndex]