VueJS Accordion Table - Appears outside of the table

前端 未结 1 1865
生来不讨喜
生来不讨喜 2021-01-21 20:16

I have a table where the data is fetched using ajax. I\'m trying to have a table where each row has an associated hidden row and clicking on the row toggles the display of the h

相关标签:
1条回答
  • 2021-01-21 20:27

    It sounds like you want an accordion associated with every row, so really, you want two rows for each item of your data.

    You can accomplish that by moving your v-for to a template tag that wraps both of your rows.

    Additionally, you need to control whether content is visible on a row by row basis, so add a contentVisible property to each data item and use it to control whether your second row is visible or not.

    console.clear()
    
    var vm = new Vue({
      el: '#vue-instance',
      data: {
        testing: [{
            id: 1,
            name: "Customer 1",
            contentVisible: false
    
          },
          {
            id: 2,
            name: "Customer 1",
            contentVisible: false
    
          },
          {
            id: 3,
            name: "Customer 3",
            contentVisible: false
    
          },
        ],
        columns: ["id", "name"]
      },
    
      mounted() {
        console.log(this.testing);
      },
    
      methods: {
        showRow(data) {
          this.contentVisible = !this.contentVisible;
    
        }
    
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
    <div id="vue-instance">
      <table class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <th v-for="column in columns">
              {{column}}
            </th>
          </tr>
        </thead>
    
        <tbody>
          <template v-for="row in testing">
            <tr @click="row.contentVisible = !row.contentVisible">
               <td>{{row.id}}</td>
               <td>{{row.name}}</td>
             </tr>
             <tr v-if="row.contentVisible">
               <td :colspan="columns.length" >
                 <div class="accordian-body">
                   afasfafs
                 </div>
               </td>
             </tr>
          </template>
        </tbody>
      </table>
    </div>

    Here is your updated fiddle.

    0 讨论(0)
提交回复
热议问题