Tips for utilizing the latest Vue features within a single file component

Currently in the process of learning Vue.js, I've encountered a small problem: I have a single file component containing an array of checkboxes. While referring to the documentation on working with multiple checkboxes, I found that the provided example requires me to declare:

new Vue({
  el: '#example-3',
  data: {
    checkedNames: []
  }
})

Update: Despite setting this up within the <script> tag of my single file component using data(), it only checks/unchecks all boxes at once (though showing correct true/false feedback):

<script>
  export default {
    name: 'PhrasesDetail',
    data () {
      return {
        game: '',
        language: '',
        checkedPhrasesArr: {
             el: '#selection',
             data: {
                 checkedPhrasesArr: []
             }
          }
      }
    },
... 
</script>

The issue is, how and where should I declare the checkbox array so that it recognizes/reacts to the individual elements?

These are the checkboxes I'm working with:

 <tr v-for="(keyTransPair, index) in this.language.data">
      <td id="selection"><input type="checkbox" :id="index" v-model="checkedPhrasesArr"></td>
      <td>{{index}}</td>
 ...
 </tr>

Answer №1

Here is a complete example that I put together. Please note that I am using fake data since I don't have information about your language.data object.

<template>
  <div>
    <!-- Checkboxes -->
    <table>
      <tr v-for="(keyTransPair, index) in language.data">
        <td class="selection"><input type="checkbox" :value="keyTransPair" :id="index" v-model="checkedPhrasesArr"></td>
        <td>{{index}}</td>
      </tr>
    </table>

    <!-- Display selected boxes -->
    {{ checkedPhrasesArr }}
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      // Your language data
      language: {
        data: {
          keyOne: "one",
          keyTwo: "two",
          keyThree: "three"
        }
      },

      // Checkbox data
      checkedPhrasesArr: []
    }
  }
}
</script>

Remember: The checkbox values are linked to :value="keyTransPair". You can modify the value as needed. If the checkbox is checked, this value will be added to the array.


On a side note, avoid using <td id="selection"> within the v-for loop as the id "selection" won't be unique that way. It's better to use a class instead of an id in this case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing HTML documents instead of images in Owl Carousel 2

I am currently utilizing owl carousel 2 to construct a basic sliding carousel. However, I am only using images at the moment and would like to incorporate HTML files instead. These HTML files contain multiple divs where images can be inserted, and instead ...

Generating an array by iterating through each object within an array of objects

I am working with a JSON structure and I need to separate it into two arrays. The first array should include all the values from the key "employee only" to "Annual OOP max / entire family" from each object in the JSON array. The second array should contain ...

Step-by-step guide on incorporating Google Fonts and Material Icons into Vue 3 custom web components

While developing custom web components with Vue 3 for use in various web applications, I encountered an issue related to importing fonts and Google material icons due to the shadow-root. Currently, my workaround involves adding the stylesheet link tag to t ...

Executing a JavaScript function within Python using Selenium: A beginner's guide

Within my JavaScript, there is a function called 'checkdata(code)' which requires an argument named 'code' to execute and returns a 15-character string. I recently discovered how to call functions without arguments in JavaScript. Howev ...

"Injecting the value of a jQuery variable into a PHP variable

<script type="text/javascript"> $(document).ready(function(){ $("#t_select").change(function(){ var table_name = $("#t_select").val(); $.ajax({ type: 'POST', ...

Is it possible for my website to send an email without the need for a script to be executed?

Looking to include a contact page on my website, but struggling with limitations imposed due to hosting on a secure school server that restricts script execution. Wondering if there's a workaround that would allow for email sending on the client side ...

Customizing React components based on API data

const LinkList = () => { const [links, setLinks] = useState([]); const url = 'http://localhost:5000/xyz'; const hook = () => { console.log('effect'); axios .get(url) .then(respo ...

Strategies for handling asynchronous requests and effectively presenting the retrieved data

On my HTML page, I have a badge component that shows the number of unread messages. Here is the HTML code: <button class="font" mat-menu-item routerLink="/message"> <mat-icon>notifications</mat-icon> <span [matBadgeHidden]="newM ...

How can ReactJS continuously dispatch promises after a set interval?

Within my React component, I am invoking an action in ComponentDidMount() as shown below: componentDidMount() { const { actions } = this.props function save_project_continuously() { console.log("inside") actions.sa ...

What is the best method for uploading images and form content simultaneously in a Vue application?

How can I simultaneously upload images and form content? Is it possible to upload both to the client first and then to the server together with the form content? I'm looking to submit the form content along with the image to the server in one go when ...

Generate a Safari-compatible Bookmark URL that is functional

I've attempted to use the JavaScript "AddFavorite" function in my code, but unfortunately, it's not functioning properly in Safari. It seems to work in IE and perhaps Firefox from what I can recall, but all my efforts to make it work in Safari ha ...

The evaluation of jQuery did not function properly on the less.css file

I have a jQuery function that I am using as shown below @winheight:`$(window).height()` within my less file. However, when I compile it to CSS, I encounter a compiler error I am currently using a software called Crunch Compiler Errors You are usin ...

Angular - the offspring of another element

I'm currently exploring the possibilities of identifying if a clicked element is a child of another using Angular. In jQuery, I would typically use has() for this task, but I'm unsure of the equivalent method in Angular aside from iterating throu ...

Designing Interactive Circular Dates on Website

Currently, I am working on a webpage using HTML, CSS, JavaScript, and PHP. The goal is to design a page that features 7 circles representing the current date and the next 6 days. Users should be able to click an arrow to navigate to the following 7 days. ...

Tips for showcasing the stored value from the database in a table

This problematic table was created: http://jsfiddle.net/ofd3nox3/ I am currently facing an issue with displaying the values stored in the database for this table per user. For example, a user has specified availability on Thursdays in the morning, Friday ...

What is the best way to switch the visibility of a div on click from another div?

My goal is to make a div toggle visible or hidden when another div is clicked. The only connection between the two divs is that they are both nested within the same parent div. There's a DIV element with the class of "comment" which contains a DIV ele ...

Is there a way to access and parse a CSV file from a URL within a Next.js project?

Currently working on a Next.js application available here. The task at hand requires reading a CSV file from a specific URL within the same repository in multiple instances. Unfortunately, I am encountering difficulties retrieving this data. You can locate ...

How can you retrieve the current user in express.js when incorporating socket.io?

I have a web app using the express framework and socket.io for real-time chat. I am trying to get the current user's username and create a room with them in it, but I am struggling to retrieve the user info when socket.on('connection') is ca ...

Rendering Facebook tags on NextJs via server-side rendering

Currently, I am developing a straightforward Facebook quiz application to gain proficiency in React and NextJs. One challenge I am encountering pertains to the Facebook tags. When I attempt to share the user's results, the meta tags for the quiz ques ...

Redirecting in Next.js without the use of a React component on the page

I need to redirect a page using HTTP programmatically only. The following code achieves this: export const getServerSideProps: GetServerSideProps = async (context) => { return { redirect: { destination: '/', permanent: false, ...