IT이야기

v-for를 사용하여 vue.js를 사용하여 테이블을 렌더링하는 방법

cyworld 2022. 4. 21. 21:26
반응형

v-for를 사용하여 vue.js를 사용하여 테이블을 렌더링하는 방법

이렇게 생긴 배열이 있는데

sales = [
 [{'Year': 2018, 'Month': 01, 'Sale'; 512}, {'Year': 2018, 'Month': 02, 'Sale'; 1025}, ....],
 [{'Year': 2017, 'Month': 01, 'Sale'; 155}, {'Year': 2017, 'Month': 02, 'Sale'; 12}, ....]
]

vue를 사용하여 테이블에 표시:

<table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>2018</th>
      <th>2017</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="(sale,i) in sales" :key="i">
       <th scope="row">{{ ??? }}</th> //Month
       <td>{{ ??? }}</td> //currentYear.Sale
       <td>{{ ??? }}</td> //previousYear.Sale
    </tr>
   </tbody>
</table>

불행히도 나는 내 판매 배열을 통해 현재 연도 및 전년도 모든 테이블 행 판매에 보여주기 위해 반복하는 방법을 알지 못한다.

<div id="app">
  <table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>2018</th>
      <th>2017</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="(sale,i) in sales[0]" :key="i">
       <th scope="row">{{ sale.Month  }}</th>  
       <td>{{ sale.Sale }}</td> 
       <td>{{ sales[1][i].Sale }}</td>  
    </tr>
   </tbody>
</table>
</div>

new Vue({
  el: "#app",
  data: {
    sales: [
        [{'Year': 2018, 'Month': 01, 'Sale': 512}, {'Year': 2018, 'Month': 02, 'Sale': 1025}],
            [{'Year': 2017, 'Month': 01, 'Sale': 155}, {'Year': 2017, 'Month': 02, 'Sale': 12}]
    ]
  } 
})

예: https://jsfiddle.net/mcqwtdgr/

참조URL: https://stackoverflow.com/questions/51963481/how-to-use-v-for-to-render-a-table-using-vue-js

반응형