【问题标题】:Vue.js / Vuetify / v-data-table: item from v-slot seems to be a copy and not a reference for the attributeVue.js / Vuetify / v-data-table: item from v-slot 似乎是副本而不是属性的引用
【发布时间】:2019-05-23 19:10:20
【问题描述】:

我正在创建一个data table with Vuetify 来显示记录列表,其中每条记录都有一个要下载的文件列表。然后,我为每个表格行创建一个按钮,当单击它时,它应该显示带有文件列表的modal

这个列表叫做tableRows,它有几个对象。我在下面提供一个示例。

脚本

export default {
  data () {
    return {
      tableRows: [
        {
            'properties': {
              'date': '2015-12-19',
              'title': 'LC82200632015353LGN01',
              'type': 'IMAGES',
              'showDownloadDialog': false
            },
            'provider': 'DEVELOPMENT_SEED'
         },
         ...
      ],
      showDownloadDialog: false   // example
    }
  }
}

表格构建良好,但我无法为每个表格行使用模态。

网站上的modal example 运行良好,我只使用一个变量(即dialog),我只想显示一个模式,但就我而言,我有一个对象列表,其中每个对象可以打开一个特定的模态。

我尝试将showDownloadDialog 属性放在列表中的每个对象中,并使用v-model (v-model='props.item.properties.showDownloadDialog') 绑定它,但无济于事。模态未打开。

模板

<v-data-table :items='tableRows' item-key='properties.title'>
<template v-slot:items='props'>
  <tr class='tr-or-td-style-border-right'>
    <td class='text-xs-left th-style-height'>
      <div class='text-xs-center'>
        ...
        <!-- Download button -->
        <br>
        title: {{ props.item.properties.title }}
        <br>
        showDownloadDialog: {{ props.item.properties.showDownloadDialog }}
        <br>

        <v-btn @click.stop='props.item.properties.showDownloadDialog = true' title='Show download list'>
          <i class='fas fa-download'></i>
        </v-btn>
        <v-dialog v-model='props.item.properties.showDownloadDialog' persistent scrollable max-width="600">
          <v-card>
            ...
            <v-card-actions>
              <v-btn @click='props.item.properties.showDownloadDialog = false'>
                Close
              </v-btn>
            </v-card-actions>

          </v-card>
        </v-dialog>
      </div>
    </td>
  </tr>
</template>
</v-data-table>

我试图在页面上打印属性props.item.properties.showDownloadDialog,当我单击按钮时它不会改变。我相信这个属性不是反应性的,因此,它的状态不会改变,但我不明白为什么它不是反应性的。数据表中的props 似乎是一个副本,而不是我列表tableRows 中一条记录的引用。

示例

我已经尝试在data () 中添加一个名为showDownloadDialog 的简单标志,而不是使用props.item.properties.showDownloadDialog,它有效,但它同时显示所有模态,不幸的是,不仅仅是与该记录相关的特定模式。

有人知道会发生什么吗?

提前谢谢你。

【问题讨论】:

  • 对于整个列表,您只能使用一种模式而不是重复模式。当您单击记录时,显示模态并传递参数(您单击的记录所需的数据)并填充模态内容。
  • 有了您的提示,我能够解决我的问题。非常感谢 Subash 的帮助。
  • 很高兴听到:)

标签: vue.js modal-dialog vuetify.js v-model


【解决方案1】:

通过使用 Subash 的帮助,我能够解决我的问题。我在下面给出代码。

首先,我在data () 中插入了一个新属性。我将使用这个属性来显示/关闭我的模态并提供信息来填充它。

downloadDialog: {
  show: false
}

在数据表中,我只是按下按钮并创建了一个名为 showDownloadDialog 的方法,我在其中传递了 properties 对象(即信息所在的位置)。

<v-btn flat icon color='black' class='v-btn-style' 
  @click='showDownloadDialog(props.item.properties)' title='Show download list'>
    <i class='fas fa-download'></i>
</v-btn>

在数据表之外,我添加了v-dialog,并绑定了downloadDialog。 除此之外,我还创建了一个关闭对话框的方法。

<v-dialog v-model='downloadDialog.show' persistent scrollable max-width="600">
  <v-card>
    ...
    <v-card-actions>
      <v-btn @click='closeDownloadDialog()'>
        Close
      </v-btn>
    </v-card-actions>
  </v-card>
</v-dialog>

showDownloadDialog 内部,我将“属性”合并到“downloadDialog”并打开模式,而closeDownloadDialog 我只是关闭模式。

showDownloadDialog (properties) {
  // merge 'properties' into 'downloadDialog'
  Object.assign(this.downloadDialog, properties)
  this.downloadDialog.show = true
},
closeDownloadDialog () {
  this.downloadDialog.show = false
}

非常感谢 Subash 的帮助。

【讨论】:

  • 感谢您回复您的问题并使用您的解决方案更新它。
猜你喜欢
  • 2018-01-21
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 2021-07-12
  • 2019-03-21
  • 2020-02-26
  • 2019-11-10
  • 2020-12-13
相关资源
最近更新 更多