javascript - 将接收到的 JSON 打印到表中

标签 javascript vue.js

我有一个 VueJS 模板,其中包含一个表和一个函数,该函数使用指定的 key 向后端 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>

最佳答案

首先,你的代码结构被打乱了。您不应在导出默认模块之外定义compulatedcreatedmethods 函数。

我创建了一个sandbox您只需重新排列代码并将响应的值分配给 items 变量即可。

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

如果我假设正确的话,您正在使用 Bootstrap-Vue 插件。获取 items 对象后,您可以按照 here 中的文档进行操作

关于javascript - 将接收到的 JSON 打印到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53935281/

相关文章:

javascript - 如何在 javascript 中打开模式而不是手动单击?

Javascript - 根据变量调用函数名称

laravel - Vuejs v-for 不填充 laravel blade 中的选择选项值

vue.js - <v-card> - 您是否正确注册了组件?

javascript - 获取点击图片的位置

javascript - 使用正则表达式解析 URL 查询参数

Javascript 日期 |将日期设置为一年的最后一秒

javascript - JWPlayer纵横比保持4 :3 for my 16:9 video

javascript - 如何使用vuejs组件获取json数据?

php - 无法使用 laravel 和 vue js 运行 watch