Vuetify 数据表:展开一行时获取数据

Jos*_*osh 6 expand vue.js axios vuetify.js

我有一个带有可扩展行的 Vuetify 数据表。每一行都与客户的订单相关,该订单由他们想要测试的样品组成。

目前,我正在检索所有样品的所有订单,但加载所有信息需要很长时间。

因此,当我展开一行时,我希望能够执行 API 调用来检索应显示在该特定订单的展开部分中的样本,而不是检索每个订单的所有样本。

我已经尽我所能研究了,但已经走到了尽头。这是我目前所在的位置:

<v-data-table
  :headers="orders_headers"
  :items="orders"
  show-expand
  :single-expand="true"
  :expanded.sync="expanded"
>

  <!-- Expand Buttons -->
  <template v-slot:item.data-table-expand="{ item, isExpanded, expand }">
    <v-btn @click="expand(true)" v-if="!isExpanded">Expand</v-btn>
    <v-btn @click="expand(false)" v-if="isExpanded">close</v-btn>
  </template>

  <!-- Expanded Data -->
  <template v-slot:expanded-item="{ headers, item }">
    <td :colspan="headers.length">

      <table v-for="(sample, index) in item.samples" :key="index">
        <tr>
          <th>Sample Acc</th>
          <td>{{ sample.sample_accession }}</td>
        </tr>
        <tr>
          <th>Sample Status</th>
          <td>{{ sample.sample_status }}</td>
        </tr>
      </table>

    </td>
  </template>
</v-data-table>
Run Code Online (Sandbox Code Playgroud)

我想我在写这篇文章的时候可能已经想到了

当我打字时,我意识到我可能需要做些什么。我需要向展开按钮添加一个方法调用,然后在该方法中将结果设置为expandedSamples并替换item.samples为它。

同时,如果有人有更好的解决方案,我很乐意听到。否则,我会发布我的解决方案,以防其他人尝试尝试类似的方法。

奖金

任何人都知道是否有一种方法可以在不替换默认图标/功能的情况下进入扩展事件,或者在使用时包含原始图标/功能的方法v-slot:item.data-table-expand

目前,当我使用 时v-slot:item.data-table-expand,我必须重新添加按钮,并且丢失了 V 形和动画。

Zim*_*Zim 6

为了将来遇到相同问题的读者的利益,请使用@item-expanded数据表的事件按需延迟加载项目详细信息(或子数据)。将item-expanded事件与加载数据的方法(例如 loadDetails)挂钩,然后将响应合并到原始项目数组中。

这是一个例子...

  <v-data-table
    :headers="headers"
    :items="items"
    show-expand
    single-expand
    item-key="name"
    :search="search"
    @item-expanded="loadDetails">
    <template v-slot:expanded-item="{ headers, item }">
        <td :colspan="headers.length">
           <table v-for="(sample, index) in items.samples" :key="index">
            <tr>
              <th>Sample Acc</th>
              <td>{{ sample.sample_accession }}</td>
            </tr>
           </table>
        </td>
    </template>
  </v-data-table>

  methods: {
    loadDetails({item}) {
        axios.get('http.../' + item.id)
            .then(response => {
              item.samples = response.data[0]
        })
    }
  }
Run Code Online (Sandbox Code Playgroud)

https://codeply.com/p/d5XibmqjUh