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]