vue element-ui get index of table row

once picture once · Aug 20, 2019 · Viewed 9.3k times · Source

I want to add a button only to the first row of a table column (labeled with 'Option' in the example code). Is there any simple way to check the row index for v-if v-if="scope.row.index === 0" ? scope.row.index wont work here.

<el-table :data="mydata">
<!-- more columns -->
  <el-table-column prop="option" label="Option">
    <template slot-scope="scope">
      <div v-if="scope.row.index === 0">
        <el-row>
          <el-col>
            <el-input v-model="scope.row.option"/>
          </el-col>
          <el-col>
            <el-button @click="">Check</el-button>
          </el-col>
      </el-row></div>
      <div v-else>
        <el-input v-model="scope.row.option" />
      </div>
    </template>
  </el-table-column>
<!-- more columns -->
</el-table>

Answer

once picture once · Aug 20, 2019

I found a solution to this by using the $index variable, which is the index of the current row.

<div v-if="scope.$index === 0">