【问题标题】:How to scroll to specific data value in Vue?如何滚动到Vue中的特定数据值?
【发布时间】:2018-01-08 09:07:06
【问题描述】:

我在 Vue 中有一个简单的表格:

<tbody>
    <tr v-for="(item, index) in items">
        <td>
            ...
        </td>       
    </tr>
</tbody>

items 是通过unshift 动态添加的。主要的是我需要通过updown 键在表格中导航,所以我这样做了:

jQuery(document).keydown((e) => {
    const which = e.which;

    if ([38, 40].indexOf(which) != -1) {
        e.preventDefault();

        if (this.active_index == null) {
            this.active_index = 0;
        } else {
            if (e.which == 38) {
                // up
                this.active_index--;
            } else {
                // down
                this.active_index++;
            }
        }

        console.log(this.items[this.active_index])
    }
});

是否有可能以某种方式获取this.items[this.active_index] 元素或其位置以滚动到它。

【问题讨论】:

    标签: javascript jquery scroll vue.js vuejs2


    【解决方案1】:
    <tbody>
       <tr v-for="(item, index) in items" ref="'dom'+index">
           <td>
               ...
           </td>       
       </tr>
    

    绑定refdata,当你需要获取dom时,你可以使用this.$refs[dom${this.active_index}]

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2020-09-24
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 2019-10-08
      • 2013-02-28
      相关资源
      最近更新 更多