재우니의 블로그

vue.js 로 행추가 및 삭제하기

 

실행화면 미리보기

 

소스 구경하기

<html>
   <head></head>
   <div id="app">
      <table class="table">
         <thead>
            <tr>
               <td><strong>Name</strong></td>
               <td><strong>Job</strong></td>
               <td></td>
            </tr>
         </thead>
         <tbody>
            <tr v-for="row in rows">
               <td><input type="text" v-model="row.name"></td>
               <td><input type="text" v-model="row.job"></td>
               <td><a @click="removeRow(row)">Remove</a></td>
            </tr>
         </tbody>
      </table>
      <div>
         <button class="button btn-primary" @click="addRow">Add row</button>
      </div>
   </div>
</html>

 

<script>
var app  = new Vue({
  el: "#app",
  data: {
    rows: [
      {name: "홍길동",job: "직장인"},
      {name: "김아무", job: "사무직"}
    ]
  },
  methods:{
    addRow: function(){
      this.rows.push({name:"",job:""});
    },
    removeRow: function(row){
      //console.log(row);
      this.rows.$remove(row);
    }
  }
});

</script>

 

 

실행해 보기

 

https://jsfiddle.net/13rqnvdL/