英文:
I've tried to make a checkbox-cell in datatatable with vuetify, but it doesn't work
问题
我想在一个datatable
中创建一个复选框,当用户(datatable
包含用户)是管理员时,复选框被选中。
我尝试了这段代码,但它不起作用。 {{value==1}}
是有效的,所以值是true/false。但复选框不会改变。
我该怎么办?
<template v-slot:item.admin="{value}">
<v-checkbox>
readonly
:value="value==1"
></v-checkbox>
{{value==1}}
</template>
英文:
I'd like to make a checkbox in a datatable
, where the checkbox is ticked when the user (datatable
contains users) is an admin.
I tried this code, but it doesn't work. {{value==1}}
works, so the value is true/false. But the checkbox doesn't change.
What can I do?
<template v-slot:item.admin="{value}">
<v-checkbox>
readonly
:value="value==1"
></v-checkbox>
{{value==1}}
</template>
答案1
得分: 1
使用 v-model
来设置复选框的状态。
我认为您可能在设置 v-slot
用于 template
时存在问题。通常情况下,item
被传递给 v-slot
值,然后在引用时使用 item[key]
来获取键的值,例如 item.admin
,假设 admin
的值可能是 0
、1
、true
、false
、null
或 undefined
。
对您提供的代码的重构如下:
<template v-slot:item.admin="{ item }">
<v-checkbox
v-model="item.admin"
:ripple="false"
readonly
></v-checkbox>
</template>
<!-- :ripple="false" 用于移除点击时的动画效果 -->
工作示例:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
Vue.config.productionTip = false;
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
headers: [
{
text: 'Name',
width: '9rem',
value: 'name',
},
{
text: 'Is Admin',
value: 'admin'
}
],
users: [
{
name: 'Person A',
admin: 1
},
{
name: 'Person B',
admin: 0
}
],
}
}
})
<!-- language: lang-css -->
.v-data-table {
width: 16rem;
}
<!-- language: lang-html -->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="users"
class="elevation-1"
hide-default-footer
>
<template v-slot:item.admin="{ item }">
<v-checkbox v-model="item.admin" readonly :ripple="false"></v-checkbox>
</template>
</v-data-table>
</v-app>
</div>
<!-- end snippet -->
英文:
Use v-model
to set the checkbox state.
I think you may have an issue setting the v-slot
for the template
. Typically, item
is passed to the v-slot
value and then when referenced to get a key's value use item[key]
, e.g item.admin
, assuming admin
is going to be 0
,1
,true
,false
, null
, or undefined
.
The refactor to your provided code would be:
<template v-slot:item.admin="{item}">
<v-checkbox>
v-model="item.admin"
:ripple="false"
readonly
></v-checkbox>
</template>
<!-- :ripple="false" is to remove the animation on click -->
Working example:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
Vue.config.productionTip = false;
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
headers: [
{
text: 'Name',
width: '9rem',
value: 'name',
},
{
text: 'Is Admin',
value: 'admin'
}
],
users: [
{
name: 'Person A',
admin: 1
},
{
name: 'Person B',
admin: 0
}
],
}
}
})
<!-- language: lang-css -->
.v-data-table {
width: 16rem;
}
<!-- language: lang-html -->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="users"
class="elevation-1"
hide-default-footer
>
<template v-slot:item.admin="{ item }">
<v-checkbox v-model="item.admin" readonly :ripple="false"></v-checkbox>
</template>
</v-data-table>
</v-app>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论