0

I am working on this webpage where I want to display table rows based on the checkbox selection. I was able to generate working example using this question: Hide/show table rows based on checkbox

My problem is I am getting array as response instead of formatted rows. Not sure what am I missing here.

Please check the fiddle here: 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>
6
  • have you tried using v-for? Commented Nov 24, 2020 at 23:57
  • Your HTML document is malformed. You are missing <head> and <body> Commented Nov 24, 2020 at 23:58
  • @Bravo Yes, not sure what I keep missing here. I have been staring it for hours. I tried v-for="c in staff_member Commented Nov 25, 2020 at 0:00
  • 2
    <td v-for="({course}, key) in c.courses" :key="key">{{course}}</td> Commented Nov 25, 2020 at 0:01
  • Do you want to display each course in different rows or in different columns? Commented Nov 25, 2020 at 0:37

1 Answer 1

1

As I could understand, you want to show a table of courses depending on the selection of staff members, right?

You are displaying an array of courses because you aren't iterating the staff's courses array, you are only iterating the staffs and displaying the courses raw value.

A quick refactoring in your code, would look something like this:

...
  <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>

Besides iterating over the staffs, you should iterate over the staff's courses.

Something important that you should use is the key attribute whenever you use v-for, take a look at the docs

Sign up to request clarification or add additional context in comments.

1 Comment

Here is the updated jsfiddle link with updates from Giovane's answer : jsfiddle.net/4roe2kjq/2

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.