web-dev-qa-db-ja.com

Bootstrap Vue動的テーブルテンプレート

私はBootstrap Vue JSテーブルコンポーネントを使用してデータテーブルを作成しています: https://bootstrap-vue.js.org/docs/コンポーネント/テーブル

私はVueJSを使い始めたばかりで、この問題にどのように対処すればよいかわからず、ソリューションの検索がさらに複雑になります。

APIエンドポイントを使用してJSONデータを返します。

{
   "options":{
      "filter":false
   },
   "fields":[
      {
         "key":"id",
         "label":"Id",
         "editLink":false,
         "display":true,
         "sortable":true,
         "class":"shrink"
      },
      {
         "key":"name",
         "label":"Name",
         "editLink":true,
         "display":true,
         "sortable":true
      }
   ],
   "data":[ ]
}

これが私のテーブルテンプレートです。

<b-table striped hover bordered foot-clone class="table-sm"
   :items="users" :fields="displayedFields" :per-page="perPage" :current-page="currentPage" :filter="filter"
   @filtered="onFiltered"
   >
   <template v-for="(field, index) in fields">
      <template slot="{{field.key}}" slot-scope="row" v-if="field.editLink">
         <router-link :to="'/user/' + row.item.id" v-bind:key="index"></router-link>
      </template>
   </template>
   <template slot="status" slot-scope="row">
      <toggle-button :width="36" :height="18" v-model="row.status" :labels="false" :colors="{checked: '#00FF00', unchecked: '#FF0000', disabled: '#CCCCCC'}"/>
   </template>

</b-table>

最初のテンプレートタグは、ワイルドな推測からの試みです。 fields構成から列のテーブルを条件付きで選択できるようにしたいと思います。私の試みでは、フィールドのconfig editLinkがtrueのときにRouterLinkを配置したいことがわかります。

どうすればこれを達成できますか?

7
nebulousGirl

これは、bテーブルのある動的列を示すjsfiddleです: https://jsfiddle.net/nardnob/wvk6fxgt/

new Vue({
        el: "#app",
        data: {
   fields: [{
     key: "id",
     label: "Id",
     colType: "span"
   }, {
     key: "name",
     label: "Name",
     colType: "button"
   }, {
     key: "uhh",
     label: "uhh..",
     colType: "idk"
   }],
   items: [{
        id: 0,
    name: "Test 0"
   }, {
        id: 1,
    name: "Test 1"
   }, {
        id: 2,
    name: "Test 2"
   }]
        }
});
<div id="app">
  <b-table :items="items" :fields="fields">
    <template v-for="(field, index) in fields" :slot="field.key" slot-scope="data">
      <div v-if="field.colType === 'button'">
        <h5>{{data.item.name}}</h5>
        <b-button>Am Button</b-button>
      </div>
      <div v-else-if="field.colType === 'span'">
        <h5>{{data.item.name}}</h5>
        <span>Am Span</span>
      </div>
      <div v-else>
        <h5>{{data.item.name}}</h5>
        Am Confused
      </div>
    </template>
  </b-table>
</div>
3
nardnob