LoginSignup
5
4

More than 3 years have passed since last update.

BootstrapVueのテーブルにリンクを埋め込む方法

Last updated at Posted at 2020-01-10

BootstrapVueのテーブルb-tableタグにリンクを埋め込む方法です。

完成品

スクリーンショット 2020-01-10 23.24.52.png


<b-table  :items="items3">
      <template v-slot:cell(link)="data">
        <router-link to="/work">
          <a href="#" v-on:click="openLinkInNewTab(data.value.url)">{{ data.value.name}}</a>
        </router-link>
      </template>
    </b-table>

<script>
export default {
  data: function () {
    return {
      items3: [
        { 
          link: {
            name: 'google',
            url: 'https://www.google.com/'
          },
          msg: 'googleへのリンクです'
         },
        { 
          link: {
            name: 'yahoo',
            url: 'https://www.yahoo.co.jp/'
          },
          msg: 'yahooへのリンクです'
         }
      ]
    }
  },
  methods: {
    openLinkInNewTab (url) {
      window.open(url, '_blank')
    },
  }
}
</script>

b-tableの基本

スクリーンショット 2020-01-10 23.28.37.png

<b-table  :items="items1">
</b-table>


<script>
export default {
  data: function () {
    return {
      items1: [
        {
          name: 'hoge',
          msg:  'hogeメッセージです'
        },
        {
          name: 'fuga',
          msg:  'fugaメッセージです'
        }
      ]
    }
  }
}
</script>

簡単なb-tableタグの使い方です。

テーブルの部分<b-table :items="items1">は<b-table v-bind:items="items1">の省略記法です。

とりあえずリンクを埋め込む

スクリーンショット 2020-01-10 23.36.55.png


<b-table  :items="items2">
  <template v-slot:cell(url)="data">
    <a :href="data.value" >{{ data.value}}</a>
  </template>
</b-table>

<script>
export default {
  data: function () {
    return {
      items2: [
        { 
          url: 'https://www.google.com/',
          msg: 'googleへのリンクです'
         },
        { 
          url: 'https://www.yahoo.co.jp/',
          msg: 'yahooへのリンクです'
         }
      ]
    }
  }
}
</script>

この時<template v-slot:cell(url)="data">はテーブルのurlカラムのセルにdataを紐付けるようである。dataとは何かというとitemsの配列の要素一つ一つのことを指すようである。
{{ data.value}}{{ data}}に変更すると下の通り、dataの構造がわかる。

{
    "item": {
        "url": "https://www.google.com/",
        "msg": "googleへのリンクです"
    },
    "index": 0,
    "field": {
        "key": "url",
        "label": "Url"
    },
    "unformatted": "https://www.google.com/",
    "value": "https://www.google.com/",
    "detailsShowing": false,
    "rowSelected": false
}

そのため先程のコードを下記のとおりに変更しても同様の挙動をする

<b-table  :items="items2">
  <template v-slot:cell(url)="{item}">
    <a :href="item.url" >{{ item.url}}</a>
  </template>
</b-table>

リンクの表示名を変更する

スクリーンショット 2020-01-11 0.30.29.png

<b-table  :items="items3">
  <template v-slot:cell(link)="data">
    <a :href="data.value.url" >{{ data.value.name}}</a>
  </template>
</b-table>

<script>
export default {
  data: function () {
    return {
      items3: [
        { 
          link: {
            name: 'google',
            url: 'https://www.google.com/'
          },
          msg: 'googleへのリンクです'
         },
        { 
          link: {
            name: 'yahoo',
            url: 'https://www.yahoo.co.jp/'
          },
          msg: 'yahooへのリンクです'
         }
      ]
    }
  }
}
</script>

先程の状態からitemsに紐付ける配列の構造を少し変更するだけで簡単にaタグの表示名を変更できる。

リンク先を新しいタブで開く

先ほどと画面の見た目は変わらないが、リンクをクッリクした際に新しいタブで表示する。

<b-table  :items="items3">
  <template v-slot:cell(link)="data">
    <router-link to="/hoge">
      <a href="#" v-on:click="openLinkInNewTab(data.value.url)">{{ data.value.name}}</a>
    </router-link>
  </template>
</b-table>

<script>
export default {
  data: function () {
    return {
      items3: [
        { 
          link: {
            name: 'google',
            url: 'https://www.google.com/'
          },
          msg: 'googleへのリンクです'
         },
        { 
          link: {
            name: 'yahoo',
            url: 'https://www.yahoo.co.jp/'
          },
          msg: 'yahooへのリンクです'
         }
      ]
    }
  },
  methods: {
    openLinkInNewTab (url) {
      window.open(url, '_blank')
    },
  }
}
</script>

この際<router-link to="/hoge">を使用しないとhref="#"のせいで、もとのタブは別画面に遷移してしまう

参考

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4