具有Bootstrap-Vue的VueJS:表中的一列没有数据

时间:2018-06-21 14:52:24

标签: javascript twitter-bootstrap vue.js

我的VueJS和bootstrap-vue表有一个“小”问题。

“ Fahrer”列中有数据,但是只有当我单击排序箭头或details-tab(例如,当“刷新”视图时),该数据才可见。我不知道如何解决这个问题。

Screenshot

<template>
<div style="width: 80vw;">
<b-card no-body>
      <b-tabs card v-model="tabIndex" >
        <b-tab title="Übersicht">
          <b-table responsive flex style="width: 100%; max-height: 70vh;" @click="tableClick" striped hover :items="tours" :fields="fields" :current-page="currentPage" :per-page="perPage" >

    <template slot="actions" slot-scope="row">
        <b-button size="sm" @click.stop="tableClick(row.item)" class="mr-1">
          Info
        </b-button>
    </template>
    </b-table>
    <div class="pagination-div">
      <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage"/>
    </div>
        </b-tab>
        <b-tab title="Details">
          //...
        </b-tab>
      </b-tabs>
    </b-card>       
    </div> 
</template>

<script>
import { HTTP } from '@/axioscommon'
import Minimap from '@/components/Elements/Minimap'

export default {
  name: 'tours',
  components: {
    Minimap
  },
  data () {
    return {
      //...,
      tours: [{
        id: '',
        kfz: '',
        loadno: '',
        driver: '',
        contracts: [''],
        device: ''
      }],
      fields: [
        {
          key: 'loadno',
          label: 'Ladeschein',
          sortable: true
        },
        {
          key: 'tourname',
          label: 'Tourname',
          sortable: true
        },
        {
          key: 'driver',
          label: 'Fahrer',
          sortable: true
        },
        {
          key: 'kfz',
          label: 'Kennzeichen',
          sortable: true
        },
        {
          key: 'actions',
          label: '',
          sortable: false
        }
      ]
    }
  },
  methods: {
    tableClick (row, index) {
      //...
    },
    fetchInitialData () {
      HTTP.get('tour')
       .then(response => {
         this.totalRows = response.data.length
         for (let index = 0; index < response.data.length; index++) {
           HTTP.get('driver/' + response.data[index]['driverID'])
            .then(drv => {
              response.data[index].driver = drv.data['firstname'] + ' ' + drv.data['lastname']
            })
         }
         console.log(response.data)
         this.tours = response.data
       })
    }
  },
  mounted () {
    this.fetchInitialData()
  }
}
</script>
<style scoped>
.fade.show {opacity: 1}
</style>

我已经在几种浏览器上进行过测试-均未成功。我在用着: Vue 2.5.2 Bootstrap 4.0.0 Bootstrap-Vue 2.0.0 Webpack 2.6.1

1 个答案:

答案 0 :(得分:1)

this.tours = response.data完成之前设置

HTTP.get('driver/')。而且,您仅更改对象属性,因此Vue无法检测到更改。 您可以通过深度复制数组来解决此问题

fetchInitialData () {
    HTTP.get('tour')
     .then(response => {
       this.totalRows = response.data.length
       for (let index = 0; index < response.data.length; index++) {
         HTTP.get('driver/' + response.data[index]['driverID'])
          .then(drv => {
            const driverName = drv.data['firstname'] + ' ' + drv.data['lastname']
            const tours = [...this.tours]
            // or const tours = JSON.parse(JSON.stringify(this.tours))
            tours[index].driver = driverName
            this.tours = tours
          })
       }
       console.log(response.data)
       this.tours = response.data
     })
  }