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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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