Best practice for displaying arrays in rows using Vue.js

I'm currently working on a webpage where I need to dynamically display table rows based on checkbox selections. After researching, I found a helpful example on Stack Overflow: Hide/show table rows based on checkbox

However, my issue lies in receiving an array instead of formatted rows as a response. I'm not sure what step I might be missing here.

If possible, please review the code snippet below for further clarification: https://jsfiddle.net/4roe2kjq/1/

<html>
<style>

    td {
border: thin solid black;
padding-right: 10px;
padding-bottom: 5px;
padding-left: 10px;
padding-top: 5px
    }

</style>

<script src="/javascript/vue.js"></script>


 <div id="app">
      <div v-for="val in cbValues">
        <label>
          <input type='checkbox' :value='val' v-model="selectedType"> 
          {{val}}
        </label>
      </div>
      <table>
      
      
        <template v-for="c in staff_member">
    


              <tr v-if="isVisible(c)">
               <td>{{c.courses}}</td>
            </tr>


  
        </template>
        
        </table>

<script>


   new Vue({
      el: '#app',


      data: {

        selectedType: [],
           staff_member:[{
   
      description :'Non-lab Staff Member',
    courses:[
      {course :'first'},
      {course :'second'}

    ]

  }],
  
  
  
        cbValues: [
        'Non-lab Staff Member'
        ]
      },
      methods: {
        isVisible(row) {
           const matchedValue = this.cbValues.find(v => row.description.indexOf(v) >= 0);
          if (!matchedValue) {
            return true;
          }
          return this.selectedType.includes(matchedValue);
        }
      }
    });

</script>

Answer №1

It appears that you are looking to display a table of courses based on the selection of staff members, is that correct?

The issue seems to be that you are displaying an array of courses without iterating through the staff's courses array. Instead, you are only iterating through the staffs and displaying the raw course values.

A simple code refactoring could solve this problem, as shown below:

...
  <table>
    <template v-for="(staff, key2) in staff_member" :key="key2">
      <template v-if="isVisible(staff)">
        <tr v-for="(course, key3) in staff.courses" :key="key3">
          <td>{{course.course}}</td>
        </tr>
      </template>
    </template>
  </table>

In addition to iterating over the staff members, make sure to iterate over each staff member's courses as well.

Don't forget to include the key attribute when using v-for, for more information refer to the official documentation.

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

A dynamic dropdown list in asp.net with conditional binding

I have a string formatted like this: string LogInIDs = 124,345, 876 | 765,322, 98 | 565,99 Each number in the string represents a unique LogIn ID. The grouping of IDs is done using the Pipe symbol. For example, if the LogInID is 345, then I need to retri ...

Checking Form Entries with the Power of FOR Loop in JavaScript

Can someone assist me with implementing JavaScript for form validation using a for loop within the JavaScript code? I have a form with 5 fields for usernames and passwords, and I need to use JavaScript to validate the form before submitting it to the data ...

Extract the query string from the URL using jQuery and perform corresponding actions based on its contents

There is a link in an email that directs the user to a webpage. On this webpage, there is a hidden pop-up that can only be displayed if a button is clicked. jQuery handles the display of this div. I have been trying to figure out how to extract the query ...

Branding image from Bootstrap overlapped with a container situated below the navigation bar

I'm having trouble adding a 400 x 100 png logo as a Navbar Brand image in Bootstrap 5. The logo is appearing too large, and even when I try to resize it, it still overlaps the black container and text input box below the Navbar in desktop view. On mob ...

Is there a way for me to acquire the Amazon Fire TV Series?

I'm currently working on developing a web app for Amazon Fire TV, and I require individual authorization for each app. My plan is to utilize the Amazon Fire TV serial number for this purpose. However, I am unsure how to retrieve this serial using Jav ...

Issue with OpenLayers Icon not appearing on screen

I just finished creating a SpringBoot app using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and packaging it as an executable JAR file. Within this app, I have integrated OpenLayers 4 library to display a map with an Icon. Howeve ...

What is the best way to showcase response information on the website interface?

Recently, I have been utilizing GET requests to the github API: axios.get('https://api.github.com/users/roadtocode822') .then(function (response) { console.log(response.data); }) Successfully retrieving the response data. This particula ...

retrieving form data from a submit button using objects in PHP

I am using objects to fetch a form (including a submit button) from another page. However, I am struggling to extract the POSTED information from that submit button and believe that AJAX might be necessary. Here is an example: Page 1 initiates a call to ...

Does the AngularJS inject function operate synchronously?

Does the AngularJS inject method work synchronously? Here is an example: inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; rootScope = _$rootScope_.$new(); }); ...

Remove the border of the icon within the input field in Bootstrap

Currently, I am working on integrating Bootstrap with React and faced a challenge while trying to add a password field with an icon that allows users to toggle between displaying text and hiding it inside the input field. While I have successfully implemen ...

Dockerfile unable to recognize environment variable

I'm currently working on a Vue application running with Docker, and I want to use environment variables to target specific project repos. However, when I try setting the env variables, they don't seem to be recognized in the Dockerfile. Can you ...

What is the proper way to implement /deep/, >>>, or ::v-deep in Vue.js?

After doing some research, I came across this article explaining how in Vue.js, you can use either /deep/ or >>> in a selector to apply style rules to elements inside child components. However, when trying to implement this in my styles using SCSS ...

Ajax: What could be causing the post request to be triggered twice?

I am puzzled by the fact that my request is being sent twice, without any clear reason. Here is the code for my simple form: <form method="POST" class="mb-4" autocomplete="off" action="/recipe/add" novalidate id="form"> <div class="form-grou ...

How can I establish a scope variable through client-side JavaScript in XPages?

My goal is to update an XPages scope variable using Client-side JavaScript. I have a complex XPage with multiple sections that are toggled on and off using Dojo. Within this XPage, there is a button that triggers some Server-side JavaScript. However, after ...

Discover the method of connecting to a unique JavaScript trigger while utilizing IJavaScriptExecutor

In our web application, we have an event called timelineEventClicked that is created by a custom trigger. canvas.addEventListener('click', function (evt) { evt.stopImmediatePropagation(); var mousePos = ge ...

Retrieve a single document using Angularfire2 without setting up a listener

I'm facing an issue with angularfire2 v6 and angular 11. Specifically, I am attempting to retrieve a single document from the users collection based on their email without utilizing valueChanges() or snapshotChanges(). Realtime updates are not necessa ...

ASP.NET repeater causing jQuery scrollTop to malfunction after first use

I am facing a peculiar issue where I need to scroll through repeater items in a RadWindow using arrow keys in an ASP.NET application. Below is the code snippet: function isScrolledIntoView(elem) { var docViewTop; var docViewBottom ...

"Encountering a lack of information when attempting to retrieve data from a

Is there a way to access specific data from this JSON without encountering the "undefined" issue? var myObj = '{"isTrue":"true","id":"1"}'; var theKey = 'isTrue'; alert(myObj[theKey]); //When attempting to access the key, I keep gettin ...

Troubleshooting the issue of React forms hook not functioning properly with Material UI Select input

How the Textfield below should load: https://i.sstatic.net/29Sz4.png How it actually loads: https://i.sstatic.net/TdPYM.png My Select component, created using Material UI and React form hook, is not loading the default value as expected. The component ...

What could be the reason for my PHP files not appearing in my CSS navigation tabs?

In my PHP document, I am using CSS navigation tabs to display three different files. <div id="Tum" class="w3-container training"> <h2>TUM</h2> <?php include("tum.php"); ?> </div> <div id="Atol" class="w3-container traini ...