将收到的JSON打印到表中

时间:2018-12-26 17:40:48

标签: javascript vue.js

我有一个VueJS模板,其中包括一个表和一个函数,该函数使用指定的密钥向后端API发送请求,然后接收回JSON响应。

我遇到的麻烦是编写函数,以便它接收接收到的输出,然后将其打印到表中。

JSON示例:

{"driver_id":1,"driver_name":"{driver_first_name}, {driver_last_name}","driver_truck":"13","driver_trailer":"83","driver_status":"driving","has_violations":false},
{"driver_id":2,"driver_name":"{driver_first_name}, {driver_last_name}","driver_truck":"58","driver_trailer":"37","driver_status":"sleeping","has_violations":true},
{"driver_id":3,"driver_name":"{driver_first_name}, {driver_last_name}","driver_truck":"80","driver_trailer":"27","driver_status":"driving","has_violations":true},

这是我的代码:

<template>
  <b-container fluid>

    <!--Search Controls-->
    <b-row>
      <b-col md="6" class="my-1">
        <b-form-group horizontal label="Filter" class="mb-0">
          <b-input-group>
            <b-form-input v-model="filter" placeholder="Type to Search" />
            <b-input-group-append>
              <b-btn :disabled="!filter" @click="filter = ''">Clear</b-btn>
            </b-input-group-append>
          </b-input-group>
        </b-form-group>
      </b-col>
      <b-col md="6" class="my-1">
        <b-form-group horizontal label="Sort" class="mb-0">
          <b-input-group>
            <b-form-select v-model="sortBy" :options="sortOptions">
              <option slot="first" :value="null">-- none --</option>
            </b-form-select>
            <b-form-select :disabled="!sortBy" v-model="sortDesc" slot="append">
              <option :value="false">Asc</option>
              <option :value="true">Desc</option>
            </b-form-select>
          </b-input-group>
        </b-form-group>
      </b-col>
      <b-col md="6" class="my-1">
        <b-form-group horizontal label="Sort direction" class="mb-0">
          <b-input-group>
            <b-form-select v-model="sortDirection" slot="append">
              <option value="asc">Asc</option>
              <option value="desc">Desc</option>
              <option value="last">Last</option>
            </b-form-select>
          </b-input-group>
        </b-form-group>
      </b-col>
      <b-col md="6" class="my-1">
        <b-form-group horizontal label="Per page" class="mb-0">
          <b-form-select :options="pageOptions" v-model="perPage" />
        </b-form-group>
      </b-col>
    </b-row>
    <!--Search Controls-->

    <!-- Main table element -->
    <b-table show-empty
             stacked="md"
             :items="items"
             :fields="fields"
             :current-page="currentPage"
             :per-page="perPage"
             :filter="filter"
             :sort-by.sync="sortBy"
             :sort-desc.sync="sortDesc"
             :sort-direction="sortDirection"
             @filtered="onFiltered"
    >
      <template slot="truck_number" slot-scope="row">{{row.value.driver_truck}}</template>
      <template slot="trailer_number" slot-scope="row">{{row.value.driver_trailer}}</template>
      <template slot="violation_date" slot-scope="row">{{row.value.violation_date}}</template>
      <!-- View Specific Violation -->
      <template slot="actions" slot-scope="row">
        <b-button @click="getSpecificViolation(logbook_id)" id="view_specific_violation">View Violation</b-button>
      </template>

    </b-table>

    <b-row>
      <b-col md="6" class="my-1">
        <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0" />
      </b-col>
    </b-row>

  </b-container>
</template>

<script>

export default {
  name: 'List of Violations',
  data () {
    return {
      items: items,
      fields: [
        { key: 'truck_number', label: 'Truck Number', sortable: true, 'class': 'text-center' },
        { key: 'trailer_number', label: 'Trailer Number', sortable: true, 'class': 'text-center' },
        { key: 'violation_date', label: 'Date of Violation' },
        { key: 'actions', label: 'Actions' }
      ],
      currentPage: 1,
      perPage: 5,
      totalRows: items.length,
      pageOptions: [ 5, 10, 15 ],
      sortBy: null,
      sortDesc: false,
      sortDirection: 'asc',
      filter: null,
    }
  },
  },
  computed: {
    sortOptions () {
      return this.fields
        .filter(f => f.sortable)
        .map(f => { return { text: f.label, value: f.key } })
    }
  },
  created() {
    this.id = this.$route.params.id;
  },
  methods: {
    onFiltered (filteredItems) {
      // Trigger pagination to update the number of buttons/pages due to filtering
      this.totalRows = filteredItems.length
      this.currentPage = 1
    }
    navigate() {
      router.go(-1);
    }
    //Send Request all Violations for Driver_ID
    axios.post('http://localhost:3030/api/allViolations/driverID', 
    this.driver_id, // the data to post
    { headers: {
      'Content-type': 'application/x-www-form-urlencoded',
      }
    }).then(response => ....???); //print api json response
  }
}
</script>

2 个答案:

答案 0 :(得分:0)

首先,您在itemsdata的初始值应为[](一个空数组)。

此后,当您取回JSON时,几乎所有您需要做的就是设置this.items = response.data。尽管您粘贴的代码似乎很糟糕(axios.post调用位于methods的正下方)。

答案 1 :(得分:0)

首先,您的代码结构被破坏。您不应在导出默认模块之外定义computedcreatedmethods函数。

我为您创建了一个sandbox,只是为了重新排列代码并将响应值分配给items变量。

.then(response => (this.items = response.data));

如果我正确假设您正在使用Bootstrap-Vue插件。获取items对象之后,您可以按照here

中的文档进行操作。