2016-11-23 38 views
2

当添加新行或删除行时,我的动画工作在表格上。Vue - 如何动画表格行更改

但我想动画行更改 - 它们不被添加或删除,但绑定到该行的数据更改。

这可能吗?

+0

你的意思,你想到[过渡状态](https://vuejs.org/v2/guide/transitioning-state.html)? –

回答

2

您可以看看将@craig_h建议的状态转换或者可能只是设置一个常规的javascript事件来监视动画结束。

要利用第二种方法,您可以为每行数据添加一个新的参数changed: false,然后在更改时将其设置为true。然后这可以将一个类添加到'changed'行。然后,当该行具有“更改”类时,让您的CSS触发动画。现在,您只需在该行上侦听“animationend”事件并将更改后的参数重置为false即可。喜欢的东西:

HTML - 行元素

<table> 
    <tr 
    ref="rows" 
    :class="{ changed: row.changed }" 
    v-for="(row, index) in rows"> 
    <td><input v-model="row.title" type="text"></td> 
    <td> 
     <button @click="saveRowEdits(index)">save</button> 
    </td> 
    ... 

组件

data() { 
    return { 
    rows: [ 
     { title: 'foo', changed: false }, 
     { title: 'bar', changed: false }, 
    ], 
    ... 
    } 
}, 
methods: { 
    saveRowEdits (index) { 

    // get the rows DOM el 
    const row = this.$refs.rows[index] 

    // watch for animationend 
    const callback =() => { 
     row.removeEventListener("animationend", callback); 
     this.rows[index].changed = false 
    } 

    row.addEventListener("animationend", callback, false) 

    // update param 
    this.rows[index].changed = true 

    }, 
    ... 

CSS

row.changed { 
    animation: changed-row 1s ... 
+0

这听起来很不错。这些行实际上是从一个充满对象的巨大对象中渲染出来的。我可能做的是有一个简单的“changedRows”数组,并在计算的类上检查该行是否在该数组中。谢谢您的帮助。 – daninthemix

+0

是的,当然要适应你的需求。乐意效劳 – GuyC