2017-05-24 91 views
0

我試圖找到一個與$ .ajax()填充的表的解決方案,但我無法弄清楚我可以如何做到這一點與Vue.js.我怎樣才能做到這一點?我需要一個Vue組件嗎?

HTML:

<div class="row"> 
<div class="col-md-12 overflow-table"> 
     <table class="table" id="table"> 
     <thead class="head-color thead-inverse"> 
      <tr> 
       <th style="border-top-left-radius: 10px; border-left:1px solid transparent;">NAME</th> 
       <th>CLIENT-ID</th> 
       <th>URL</th> 
       <th style="border-top-right-radius: 10px; border-right:1px solid transparent;">ACTIONS</th> 
      </tr> 
     </thead> 

     <tbody id='table-redirect'> 
      <tr class='lightgrey'> 
      </tr> 
      <tr class='lightgrey'> 
      </tr> 
      <tr class='lightgrey'> 
      </tr> 
      <tr class='lightgrey'> 
      </tr> 
     </tbody> 
    </table> 
</div> 

VUE.JS SCRIPT:

//VUE.JS REDIRECT PAGE 

//VARIABLES 
var url = "http://mobisteinlp.com/redirect/redirect"; 
agr = 0; 

//VUE.JS REDIRECT VIEW MODEL 
var app = new Vue({ 
    el: '#redirect', 
    delimiters:['{', '}'], 

    data: { 
    agr1:[] 
    }, 

    methods: { 

    //FUNCTION TO DISPLAY TABLE ON PAGE (RE)LOAD 
     getAll: function() { 
     console.log('teste'); 
     $.ajax({ 
      url: url + "/getAll", 
      type: "POST", 
      dataType: "json", 
      success:function(response){ 
       console.log(response);// 
       this.agr1=response; 
       console.log(this.agr1); 
       console.log('success!'); 
      }, 
      error:function(){ 
       console.log('error'); 
      }//end error function 
     });//end $.ajax() request 
     },//end getAll function 
    }//end methods 
})//end vue.js instance 

回答

2

使用<tr>像一個列表。添加一個v-for="agr in agr1"然後你可以迭代你想要的屬性。當agr1得到更新時,它將呈現一個新的行列表。你也可以使用v-bind:key="agr.property"來讓Vue高效地呈現被重用的元素。

<tbody id='table-redirect'> 
     <tr 
      v-for="agr in agr1" 
      v-bind:key="agr.id" 
      class='lightgrey' 
     > 
      <td>{{ agr.name }}</td> 
      <td>{{ agr.client_id }}</td> 
      <td>{{ agr.url }}</td> 
      <td>{{ agr.actions }}</td> 
     </tr> 
    </tbody> 
+0

順便說一下,操作部分有2個按鈕,其中。我知道我怎麼能用它與jQuery,但把一個工作按鈕與vue.js?按鈕也像數據一樣動態。當我點擊編輯按鈕時,它會編輯選定行的內容。 – Timmy

+0

只要每個對象具有所需的動態數據,就不會很難。 '​​' –

+0

啊!我現在知道了! :)另一個問題,經過一些搜索,不應該在'mounted()'而不是'methods:{}'裏面加入ajax函數。 – Timmy