3

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>

2 Answers 2

9

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">
Sign up to request clarification or add additional context in comments.

1 Comment

What if you filter the data or sort them? This won't work anymore I think...
2

vue template & scope.$index

my solution:

 <el-table
    :data="tableData"
    border
    class="app-downlaod-guide-table"
    style="width: 100%">
    <el-table-column
      v-for="({
        prop,
        label,
        align,
        width,
        slot,
      }, i) in channelClomuns"
      :key="prop + i"
      :prop="prop"
      :width="width"
      :align="align"
      :label="label">
      <template
        slot-scope="scope"
        v-if="prop === `putLink`">
        <a
          target="_blank"
          href="tableData[scope.$index].putLink">
          {{tableData[scope.$index].putLink}}
        </a>
      </template>
    </el-table-column>
  </el-table>


1 Comment

What if you filter the data or sort them? This won't work anymore I think...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.