1

I am using a Vuetify checkbox component and I am trying to bind it's value to the output of my method but it is not working at all. I have tried it with a computed property but I could not pass and argument to it. And it is also not working if I use a method. Is there a way to dynamically assign a value to an input like this?

<div v-for="row in rows">
    <v-checkbox :value="isSelected(row.id)"></v-checkbox>
</div>

data()
{
    return {
        rows: [{id: 22546}, {id: 3521}, {id: 15698}],
        selected: [1259, 1898, 3521]
    }
},

methods:
{
    isSelected(id)
    {
        if (this.selected.indexOf(id) > -1) {
            return true
        } else {
            return false
        }
    }
}

When I tried with v-model instead if :value it gave me this error:

<v-checkbox v-model="isSelected(row.id)"></v-checkbox>

isSelected(id)
{
  return true
},

error

[Vue warn]: Failed to generate render function:

SyntaxError: missing ) after argument list in
8
  • did you try with v-model? Commented Jul 22, 2019 at 15:30
  • Yes, that also did not work, it was giving me this error: [Vue warn]: Failed to generate render function: SyntaxError: missing ) after argument list in Commented Jul 22, 2019 at 15:31
  • can you provide the code you tried v-model with ? Commented Jul 22, 2019 at 15:35
  • @ConstantinTrepadus yes, I just updated the question with code Commented Jul 22, 2019 at 15:38
  • Can you try to use v-bind:value="isSelected(row.id)"? Commented Jul 22, 2019 at 15:40

4 Answers 4

3

Try with:

<v-checkbox :input-value="isSelected(row.id)"></v-checkbox>
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me! Thank you very much @Constantin Trepadus
1

You can use v-model with getter that encapsulates the logic that decides if checkbox needs to be checked:

let id = 1898;

new Vue({
  el: '#app',
  data() {
    return {
      rows: [{id: 22546}, {id: 3521}, {id: 15698}],
      selected: [1259, 1898, 3521]
    }
  },
  computed: {
    val: {
      get: function() {
        return this.selected.indexOf(id) > -1;
      },
      set: function(newValue) {
        console.log(newValue);
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.js"></script>
<link href="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css" rel="stylesheet">
<div id='app'>
<div v-for="row in rows">
    <v-checkbox v-model="val"></v-checkbox>
</div>
</div>

Comments

0

It works fine with v-model make sure your app is nested inside v-app tag :

Vue.config.productionTip = false;
Vue.config.devtools = false;

new Vue({ el: '#app',
data()
{
    return {
        rows: [{id: 22546}, {id: 3521}, {id: 15698}],
        selected: [1259, 1898, 3521]
    }
},

})
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
  <div id="app">
    <v-app>
      <div v-for="(row,i) in rows">
         <v-checkbox 
          v-model="selected[i]" 
         :label="`${selected[i] > -1 ? true : false} `"
          ></v-checkbox>
      </div>
    </v-app>
   </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.js"></script>

</body>
</html>

Comments

0

Ok, I figure it out,

v-model is 2-way binding, you can only bind it to variable or computed property with getter and setter. value provided by vue-checkbox component is not the value of checkbox checked/unchecked state. You can set initial state with :input-value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.