【问题标题】:Vue.js $.ajax() dynamic table when document ready文档准备好时的 Vue.js $.ajax() 动态表
【发布时间】:2017-10-25 02:57:22
【问题描述】:

我正在尝试为使用 $.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 脚本:

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

【问题讨论】:

    标签: javascript jquery html ajax vue.js


    【解决方案1】:

    像列表一样使用&lt;tr&gt;。添加 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>
    

    【讨论】:

    • 顺便说一句,操作部分有 2 个带有 的按钮。我知道如何将它与 jQuery 一起使用,但要使用 vue.js 放置一个工作按钮?按钮也像数据一样是动态的。当我单击编辑按钮时,它会编辑所选行的内容。
    • 只要每个对象都有所需的动态数据,这并不难。 &lt;td&gt;&lt;img src="action1.png" v-on:click="doFunction1(agr)"&gt;&lt;img src="action2.png" v-on:click="doFunction2(agr)"&gt;&lt;/td&gt;
    • 啊!我现在知道了! :) 另一个问题,经过一番搜索,ajax 函数不应该在mounted() 而不是methods: {} 内吗?
    • 将其保存在methods 并在mounted 中调用它,如果您希望能够更新信息,您可以调用getAll 函数
    • mounted: function() { getAll(); }, ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2021-06-02
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多