我尝试在使用Vuetify的数据表中创建复选框单元格,但它不起作用。

huangapple go评论65阅读模式
英文:

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?

&lt;template v-slot:item.admin=&quot;{value}&quot;&gt;
        &lt;v-checkbox&gt;
                readonly
				:value=&quot;value==1&quot;
		&gt;&lt;/v-checkbox&gt;
	    {{value==1}}
&lt;/template&gt;

答案1

得分: 1

使用 v-model 来设置复选框的状态。

我认为您可能在设置 v-slot 用于 template 时存在问题。通常情况下,item 被传递给 v-slot 值,然后在引用时使用 item[key] 来获取键的值,例如 item.admin,假设 admin 的值可能是 01truefalsenullundefined

对您提供的代码的重构如下:

<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:

&lt;template v-slot:item.admin=&quot;{item}&quot;&gt;
  &lt;v-checkbox&gt;
    v-model=&quot;item.admin&quot;
    :ripple=&quot;false&quot;
    readonly
  &gt;&lt;/v-checkbox&gt;
&lt;/template&gt;
&lt;!-- :ripple=&quot;false&quot; is to remove the animation on click --&gt;

Working example:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

Vue.config.productionTip = false;
new Vue({
  el: &#39;#app&#39;,
  vuetify: new Vuetify(),
  data () {
    return {
      headers: [
        {
          text: &#39;Name&#39;,
          width: &#39;9rem&#39;,
          value: &#39;name&#39;,
        },
        { 
          text: &#39;Is Admin&#39;,
          value: &#39;admin&#39;
        }
      ],
      users: [
        {
          name: &#39;Person A&#39;,
          admin: 1
        },
        {
          name: &#39;Person B&#39;,
          admin: 0
        }
      ],
    }
  }
})

<!-- language: lang-css -->

.v-data-table {
  width: 16rem;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://fonts.googleapis.com/css?family=Material+Icons&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.1.14/dist/vuetify.min.css&quot; rel=&quot;stylesheet&quot;/&gt;


&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;
  &lt;v-app id=&quot;inspire&quot;&gt;
    &lt;v-data-table
      :headers=&quot;headers&quot;
      :items=&quot;users&quot;
      class=&quot;elevation-1&quot;
      hide-default-footer
    &gt;
      &lt;template v-slot:item.admin=&quot;{ item }&quot;&gt;
        &lt;v-checkbox v-model=&quot;item.admin&quot; readonly :ripple=&quot;false&quot;&gt;&lt;/v-checkbox&gt;
      &lt;/template&gt;
    &lt;/v-data-table&gt;
  &lt;/v-app&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 19:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定