【问题标题】:How to get index of tr in Bootstrap Vue table?如何在 Bootstrap Vue 表中获取 tr 的索引?
【发布时间】:2021-07-07 12:56:48
【问题描述】:

当我在 vue 中使用 v-for 时,我会这样做:

v-for="(test, index) in tests"

如何使用 Bootstrap vue 做同样的事情?我想获取所有TR的索引。

<b-table
  :items="items"
  :fields="columns"
>

【问题讨论】:

  • 你想在哪里以及如何使用这个index?

标签: vue.js bootstrap-vue


【解决方案1】:

也许你可能想要不同的东西,但你可能想看看解决方案。

如果没有提供主键,将根据显示的行的索引号(即在显示的表行中的位置)自动生成键。

<template>
  <div>
    <b-table small :fields="fields" :items="items" responsive="sm">
      <!-- A virtual column -->
      <template #cell(index)="data">
        {{ data.index + 1 }}
      </template>

      <!-- A custom formatted column -->
      <template #cell(name)="data">
        <b class="text-info">{{ data.value.last.toUpperCase() }}</b>, <b>{{ data.value.first }}</b>
      </template>

      <!-- A virtual composite column -->
      <template #cell(nameage)="data">
        {{ data.item.name.first }} is {{ data.item.age }} years old
      </template>

      <!-- Optional default data cell scoped slot -->
      <template #cell()="data">
        <i>{{ data.value }}</i>
      </template>
    </b-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        fields: [
          // A virtual column that doesn't exist in items
          'index',
          // A column that needs custom formatting
          { key: 'name', label: 'Full Name' },
          // A regular column
          'age',
          // A regular column
          'sex',
          // A virtual column made up from two fields
          { key: 'nameage', label: 'First name and age' }
        ],
        items: [
          { name: { first: 'John', last: 'Doe' }, sex: 'Male', age: 42 },
          { name: { first: 'Jane', last: 'Doe' }, sex: 'Female', age: 36 },
          { name: { first: 'Rubin', last: 'Kincade' }, sex: 'Male', age: 73 },
          { name: { first: 'Shirley', last: 'Partridge' }, sex: 'Female', age: 62 }
        ]
      }
    }
  }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 2019-06-13
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多