【问题标题】:Vuetify Data Tables: Fetch data when expanding a rowVuetify 数据表:扩展行时获取数据
【发布时间】:2020-04-02 14:24:27
【问题描述】:

我有一个带有可扩展行的 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>

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

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

与此同时,如果有人有更好的解决方案,我很想听听。否则,我会发布我的解决方案,以防其他人尝试类似的事情。

奖金

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

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

【问题讨论】:

  • 你在正确的轨道上。您应该捕获扩展事件并获取数据。然后填充项目。
  • 也希望得到关于奖金问题的答案。我只想更改 V 形图标的颜色,但我找不到 v-slot:item.data-table-expand 内部内容的原始代码

标签: vue.js axios vuetify.js expand


【解决方案1】:

为了方便未来读者面临相同问题,请使用数据表的@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]
        })
    }
  }

https://codeply.com/p/d5XibmqjUh

【讨论】:

  • 感谢您的回答。这正是我想要的。
  • 当我打开和关闭展开的面板时,它会调用我挂钩到@item-expanded 的方法。有没有办法只在扩展时调用我的方法?
【解决方案2】:

这是我的问题的基本解决方案。

<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); getSamples(item.id)" 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 expandedOrderDetails" :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>

<script>
methods: {
  getSamples(orderId) {
    // Perform API call
    this.expandedOrderDetails = response.data;
  },
}
</script>

经过数小时的开发,我们的项目负责人决定他希望每一行都与每个样本相关联,而不是与订单相关联。因此不再需要扩展。

【讨论】:

    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2021-01-26
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多