1

我是 Vue 的新手。我无法在 Vue.js Javascript 文件中从 DOM 中删除项目。我成功地发出了一个 ajax 发布请求,该请求从我的数据库中删除了一条特定记录。

一旦它被删除,我需要将它从 DOM 中删除,所以它不会在不需要重新加载同一页面的情况下购买 - 我想你知道我的意思。

我可以在 jQuery 中完成,但我想知道实际上应该如何在 Vue.js 中完成?

这是我的部分代码:

// SOME CODE BEFORE ...

onSubmit: function(e){
    e.preventDefault();

    $tablerow = this.el.parentNode.parentNode;

    // Assign a correct submit request type and url
    this.vm
        .$http[this.getRequestType()](this.el.action)
        .then(this.onComplete.bind(this))
        .catch(this.onError.bind(this))
    ;
},

onComplete: function(){

    // Remove the item from a DOM  <<< I NEED TO REMOVE PARENT <tr/> ELEMENT
    $(this.el).closest('tr').fadeOut(300, function(){
        this.remove();
    });

    // If complete message exists, use it as a feedback
    if(this.params.complete){
        alert(this.params.complete);
    }
},

// SOME CODE AFTER ...

请问有什么建议吗?对不起,如果我的问题很愚蠢,但我没有最好的编程知识。

4

1 回答 1

3

通常,您将在存储在数组中的表格行中使用 v-for 显示项目列表。在您的 ajax 调用之后,您可以使用 this.items.$remove(item) 删除此项目,它将自动从它在 DOM 中显示的位置删除。

由于您没有显示您的模板,我将尝试重新创建我认为您正在尝试做的类似场景

data: function(){
   return {
       items: ['item1','item2','item3']
   }
},

methods: {
    remove: function(item){
        ajaxCall.then(function(){
            this.items.$remove(item);//will remove the <tr> from the DOM 
        });
    }
}

你的模板可以像

<tbody>
  <tr v-for="item in items" v-on:click="remove(item)">{{ item }}</tr>
</tbody>
于 2016-03-16T19:47:43.983 回答