繁体   English   中英

如何在 Bootstrap Vue 表中动态添加一列

[英]How to dynamically add a column to Bootstrap Vue Table

我正在使用 Bootstrap Vue并希望动态添加一列或仅在数据中满足某些条件时才显示该列。 我正在尝试了解作用域插槽以及这是否是我应该使用格式化程序回调函数的方向。 理想情况下,只有在数据中可用时,我才想添加列“版本 2”。 我不知道从哪里开始。

代码

    <b-table striped responsive hover :items="episodes" :fields="fields" :filter="filter">
           <template v-slot:cell(version_1)="data">
        <a :href="`${data.value.replace(/[^a-z]-/i,'-').toLowerCase()}`">Definition</a>
      </template>

    </b-table>
</template>
<script>
data() {

    return {
      fields: [{
          key: 'category',
          sortable: true
        },
        {
          key: 'episode_name',
          sortable: true
        },
        {
          key: 'episode_acronym',
          sortable: true
        },
        {
          key: 'version_1',
          sortable: true
        }
      ],
      episodes: [],
      versions: [],
    }

  },   
mounted() {
    return Promise.all([
        // fetch the owner of the blog
        client.getEntries({
          content_type: 'entryEpisodeDefinitions',
          select: 'fields.title,fields.category,fields.slug,fields.episodeAcronym,fields.version,sys.id'
        })

      ])
        .then(response => {
        // console.info(response[0].items);
        return response[0].items
      })
      .then((response) => {

        this.episodes = response.reduce(function( result, item){
          if ( item.fields.version === 1 ) {
          return result.concat({
            category: item.fields.category,
            episode_name: item.fields.title,
            episode_acronym: item.fields.episodeAcronym,
            version_1: 'episodes/' + item.fields.slug + '/' + item.sys.id,
          })
          }
          return result
        }, [])

        this.versions = response.reduce(function( result, item){
          if ( item.fields.version === 2 ) {
          return result.concat({
            version_2: 'episodes/' + item.fields.slug + '/' + item.sys.id,
          })
          }
          return result
        }, [])

        console.info(response);
      })
      .catch(console.error)


  },
</script>

我认为这是您正在寻找的东西:

<b-table :items="items" :fields="fields">
    <template v-for="field in dynamicFields" v-slot:[`cell(${field.key})`]="{ item }">
</b-table>

在脚本中:

this.dynamicFields.push({key: 'someTestKey', label: 'Dynamic Label'})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM