【问题标题】:Get row element form row.clicked event从 row.clicked 事件中获取行元素
【发布时间】:2019-01-21 00:02:36
【问题描述】:

我正在使用 Bootstrap Vue 中的表格,我试图在单击一行时显示行详细信息。

正如文档所说,我使用了row-clicked 事件,但我没有找到任何使用toggleDetails 方法的行详细信息。 所以我什至不知道如何打开它以及toggleDetails来自哪里。

有一种方法可以使用row.clicked 事件打开此详细信息行吗?

或者我应该使用其他方法吗?

你有什么线索吗?

编辑

有一些代码可以更详细地说明我的问题,我的表格中有详细信息行。

<b-table
  v-if="items"
  :items="items"
  :fields="fields"
  show-empty
  striped
  hover
  responsive
  empty-text="There is no messages to show"
  class="col-sm-12 col-md-10"
  @row-clicked="test"
>
  <template
    slot="message"
    slot-scope="row"
  >
    {{ row.item.message }}
  </template>
  <template
    slot="row-details"
    slot-scope="row"
  >
    <code>{{ row.item.message }}</code>
  </template>
</b-table>

您可以看到我在表格上使用的row.clicked 事件,然后是我尝试打开行详细信息的方法。这是一个带有一些console.log的简单方法

methods: {
  test(item, index, event) {
    console.log(item, index, event);
  },
},

【问题讨论】:

  • 显示更多代码,以便我们更好地理解问题。
  • @skribe 我添加了一些代码来说明。
  • 取消标记 D 因为它似乎与 D 编程语言没有任何关系...
  • 不错不错,这是一个失败标签。

标签: vue.js vuejs2 bootstrap-vue


【解决方案1】:

您所要做的就是在“基本”行的某个位置(可能在名为actions的行单元格中)放置一个按钮,该按钮为该特定行调用toggleDetails 函数。

这个按钮和详情行的代码应该是这样的:

<template slot="actions" slot-scope="row">
    <!-- We use @click.stop here to prevent a 'row-clicked' event from also happening -->
    <b-button size="sm" @click.stop="row.toggleDetails">
        {{ row.detailsShowing ? 'Hide' : 'Show' }} Details
    </b-button>
</template>
<template slot="row-details" slot-scope="row">
    <!-- Your row details' content here -->
</template>

您可以在documentation中找到更多示例和解释

如果您想通过单击行中的任意位置来显示行详细信息,首先您应该为每个项目对象添加一个_showDetails 变量:

items: [
    { foo: true, bar: 40, _showDetails: false },
    ...
    { foo: true, bar: 100, _showDetails: false }
]

然后你只需要为行点击事件添加适当的功能:

<b-table @row-clicked="onRowClicked" ...></b-table>

在你的组件方法中:

methods: {
    onRowClicked (item, index, event) {
        item._showDetails = !item._showDetails;
    }
}

【讨论】:

  • 是的,我知道我可以在您编写时使用按钮触发详细信息打开。但是当我单击一行上的任意位置时,我试图打开此行详细信息。
  • 我编辑了我的帖子以回答您的具体问题。
  • 请确保在创建时在每个项目对象上添加“_showDetails”变量,否则请注意 Vue 反应性警告vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats
  • 是的,我就是这样做的,效果很好,谢谢
  • 此解决方案适用于什么版本的 BV?它不适用于 v2.2。
猜你喜欢
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2023-01-05
  • 2018-10-25
  • 2021-02-24
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多