如何在bootstrap vue中的b表中将一列划分为多行?

时间:2019-09-08 13:18:42

标签: vue-cli bootstrap-vue

我正在使用vue创建SPA。我有JSON数组:

[
    {
        date: new Date(2076, 5, 10),
        customer: {id: 0,name: "Foo bar",tol: "Some tol",city: "Some City",},
        items: [
            {code: "gs",name: "Generic Shoes",cost: 500,quantity: 5},
            {code: "nf",name: "North Facing Jacket",cost: 5000,quantity: 5},
            {code: "lp",name: "Lee Vice Jeans Pant",cost: 1500,quantity: 15}
        ],
    }
]

现在包含一个主要包含日期,客户和项目的对象。我想制作包含日期,客户和项目作为字段的表,并且表的每一行将包含多行项目。 像这样:

enter image description here

这东西只有一行,但是可以想象,{date, customer, items[]}可能有多行。

这是我能够做到的最好的结果:

<b-container>
    <b-table responsive="true" striped hover :items="DraftList" :fields="fields">
        <template slot="[date]" slot-scope="data">{{data.value|formatDate}}</template>
        <template slot="[customer]" slot-scope="data">{{data.value.name}}</template>
        <template slot="[items]" slot-scope="data">{{data.value}}</template>
    </b-table>
</b-container>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      fields: [
        { key: "date", sortable: true },
        {
          key: "customer",
          label: "Customer's Name",
          sortable: true
        },
        {
          key: "items",
          label: "Item List",
          sortable: true
        }
      ]
    };
  },
  computed: {
    ...mapState(["DraftList"])
  },
  mounted() {},
  filters: {
    formatDate: date => {
      if (date instanceof Date) {
        let month = "" + (date.getMonth() + 1);
        let day = "" + date.getDate();
        let year = date.getFullYear();
        if (month.length < 2) month = "0" + month;
        if (day.length < 2) day = "0" + day;
        return [year, month, day].join("-");
      }
      return null;
    }
  }
};
</script>

我被困住了,现在该怎么办?我也无法正确地进行搜索。

1 个答案:

答案 0 :(得分:1)

使用v-for和rowpan解决了它

<b-table-simple responsive="true" hover outlined>
    <colgroup>
        <col />
        <col />
    </colgroup>
    <colgroup>
        <col />
        <col />
        <col />
    </colgroup>
    <colgroup>
        <col />
        <col />
    </colgroup>
    <b-thead head-variant="light">
        <b-tr>
            <b-th rowspan="2">Date</b-th>
            <b-th rowspan="2">Customer's Name</b-th>
            <b-th colspan="4">Items</b-th>
        </b-tr>
        <b-tr>
            <b-th>code</b-th>
            <b-th>Name</b-th>
            <b-th>Cost</b-th>
            <b-th>Quantity</b-th>
        </b-tr>
    </b-thead>
    <b-tbody v-for="(draft,index) in DraftList" :key="index">
        <b-tr>
            <b-td :rowspan="draft.items.length+1">{{draft.date|formatDate}}</b-td>
            <b-td :rowspan="draft.items.length+1">{{draft.customer.name}}</b-td>
        </b-tr>
        <b-tr v-for="(item, itemIndex) in draft.items" :key="itemIndex">
            <b-td>{{item.code}}</b-td>
            <b-td>{{item.name}}</b-td>
            <b-td>{{item.cost}}</b-td>
            <b-td>{{item.quantity}}</b-td>
        </b-tr>
    </b-tbody>
</b-table-simple>