cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="app">
  <h1>Vue.js Working With a List of Checkboxes</h1>
  
  <p>
    Working with a list of checkboxes, keeping checked state in sync with server data can sometimes be challenging.  
  </p>
  
  
  <p>Here I've tried to distill the logic down to be as simple as possible.</p>
  <p><a href="https://vuejs.org/v2/guide/forms.html#Checkbox" target="_blank">Reference: Official Docs</a></p>
  
  <nav class="mainNav">
    <!-- use router-link component for navigation. -->
<!--     <router-link :to="{ name: 'home'}">Home</router-link> -->
    <!-- specify the link by passing the `to` prop. -->

  </nav>
  <!-- route outlet -->
  <!-- component matched by the route will render here -->
  <section class="mainBody">
    <router-view></router-view>
  </section>
  
</div>
            
          
!
            
              #app {
  max-width: 800px;
  margin: auto;
}

code {
  background: #f2f2f2;
  padding: 0 0.5rem;
}

.mainBody {
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  margin: 1rem 0;
  padding: 1rem 0;
}

nav.mainNav {
  > * {
    padding: 0 0.75rem;
    text-decoration: none;
  }
  
  > *:nth-last-child(n+2) {
    border-right: 1px solid #aaa;
  }
}

.btn {
  background-color: lightgreen;
  border: 1px solid #ddd;
  border-radius: 3px;
  padding: .25rem 2rem;
  
  &:hover,
  &:focus {
    background-color: green;
    color: white;
    cursor: pointer;
  }
}

.wrap {
  display: flex;
  align-items: center;
  padding: 1rem;
}

.left,
.right {
  flex: 1;
}
            
          
!
            
              const Home = {
  template: `
    <section>
      <div>
        <button @click="simulateAjax">Simulate Server Call</button> | <button @click="clear">Clear</button>
      </div>
      <div class="wrap">
        <div class="left">
          <h3>Users:</h3>
          <ul>
            <li v-for="user in users">
              <input type="checkbox" :id="user.name" :value="user.name" v-model="selectedUsers">
              <label :for="user.name">{{user.name}}</label>
            </li>
          </ul>
        </div>
        
        <div class="right">
          <div v-for="user in selectedUsers">{{user.name}}</div>
        </div>
      </div>



      <div class="wrap">
        <div class="left">
          <h3>Tasks:</h3>
          <ul>
            <li v-for="task in tasks">
              <input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks" @change="handleTasks(task)">
              <label :for="task.title">{{task.title}}</label>
            </li>
          </ul>
        </div>
        
        <div class="right">
          <p v-for="task in selectedTasks">{{task.title}}</p>
        </div>
      </div>

    </section>
  `,
  data() {
    return {
      users: [
        { name: 'Patrick' },
        { name: 'Evan' },
        { name: 'Christian' },
        { name: 'Daniel' }
      ],
      selectedUsers: [],
      tasks: [
        { title: 'Go to the store' },
        { title: 'Clean the house' },
        { title: 'Learn Vue.js' }
      ],
      selectedTasks: []
    }
  },
  
  /* 1 - You can 'handle' the changes to selectedUsers with a custom watcher: */
  watch: {
    selectedUsers: function (newVal, oldVal) {
      // Do what you want with the selected objects:
      console.log(newVal)
    }
  },
  methods: {
    /* 2 - or... you can handle the change with the @change handler. */
    handleTasks(task) {
      // Do what you want with the selected objects:
      console.log(this.selectedTasks)
    },
    simulateAjax() {
      this.selectedUsers = [
        { name: 'Patrick' },
        { name: 'Evan' },
      ]
      
      this.selectedTasks = [
        { title: 'Clean the house' },
        { title: 'Learn Vue.js' }
      ]
    },
    clear() {
      this.selectedUsers = []
      this.selectedTasks = []
    }
  }
} 

const routes = [
  { path: '/', name: 'home', component: Home}
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount("#app")

            
          
!
999px
Loading ..................

Console