简体   繁体   English

Vuetify单个select数据表逐行

[英]Vuetify single select data table by row

I'm trying to get a Vuetify single select data table to be selectable by the row instead of by a checkbox.我正在尝试让 Vuetify 单个 select 数据表可以通过行而不是复选框来选择。

The current example I see on Vuetify's documentation is: Ex.我在 Vuetify 的文档中看到的当前示例是:Ex。 1 https://vuetifyjs.com/en/components/data-tables#api . 1 https://vuetifyjs.com/en/components/data-tables#api That's generally what I want but I want to remove the checkbox and just select by row.这通常是我想要的,但我想逐行删除复选框和 select。

I saw a previous version was able to accomplish something close with slots: Ex.我看到以前的版本能够完成与插槽接近的事情:例如。 2 https://v15.vuetifyjs.com/en/components/data-tables . 2 https://v15.vuetifyjs.com/en/components/data-tables

I've attempted a few different approaches to use slots in the 2.1.3 release but I seem to be missing something since I haven't been able to get it to work.我尝试了几种不同的方法来使用 2.1.3 版本中的插槽,但我似乎遗漏了一些东西,因为我无法让它工作。 I'm current what I want implemented using a button, but I seem to have trouble with getting the row selectable.我目前是我想要使用按钮实现的,但我似乎无法选择行。

<v-data-table
    v-model="selected"
    :headers="headers"
    :items="desserts"
    :single-select="singleSelect"
    item-key="name"
    show-select
    class="elevation-1"
    >
        <template v-slot:item.data-table-select="{ isSelected, select }">
            <v-btn color="green" :value="isSelected = !isSelected" @click="select($event)"></v-btn>
        </template> 
</v-data-table>

EDIT: I tried to implement a selectable data table with slots but it doesn't seem like item.selected works?编辑:我试图用插槽实现一个可选择的数据表,但它似乎不像 item.selected 作品? Is this still the correct method to select a row item?这仍然是 select 行项目的正确方法吗?

<v-data-table
    v-model="selected"
    :headers="headers"
    :items="desserts"
    :single-select="singleSelect"
    item-key="name"
    show-select
    class="elevation-1"
    >
        <template v-slot:body="{ items }">
            <tbody>           
                <tr v-for="item in items" :key="item.name" :active="item.selected" @click="item.selected = !item.selected">
                    <td>{{ item.name }}</td>
                    <td>CONTENT</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.calories }}</td>
                    <td>{{ item.fat }}</td>
                    <td>{{ item.carbs }}</td>
                    <td>CONTENT</td>
                </tr>
           </tbody> 
      </template>
</v-data-table>

if you want to make single data select you have to make selected item key stored in another variable like "selectedItem", you should change your code like this:如果您想制作单个数据 select 您必须将所选项目键存储在另一个变量中,例如“selectedItem”,您应该像这样更改代码:

<tr v-for="item in items" :key="item.name" 
  :active="selectedItem == item.name" @click="selectedItem = item.name">

PS: PS:

  • make sure to use unique key for your items确保为您的项目使用唯一密钥
  • selectedItem is a variable in the data object selectedItem 是数据 object 中的变量

I think your problem is your props我认为你的问题是你的道具

 single-select="true"

in your template u used single-select="true" and vuetify is looking for data that named true ex.在您的模板中,您使用了single-select="true"并且 vuetify 正在寻找名为 true ex 的数据。

    data () {
        return {
                true:true
               }
          }

if you have data name true I there will be no problem I think the right way to do that is如果您的数据名称为真,我将没有问题,我认为正确的方法是

 single-select

that means that you set the value for props single-select = true you can check documentation here这意味着您设置了 props single-select = true 的值,您可以在此处查看文档

I tried syntax :single-select=true and it worked fine.我尝试了语法:single-select=true并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM