如何获取Vuetify v-table的滚动位置?

huangapple go评论79阅读模式
英文:

How to get Vuetify v-table scrollposition?

问题

I'm using a Vuetify v-table component with a lot of data. On certain actions I want to read the current scroll position of the table (horizontal and vertical axis). This is what I've tried so far:

Playground

<template>
  <v-table fixed-header height="500px" ref="tableComponent">
    <thead>
      <tr>
        <th v-for="i in 20">
          Col {{ i }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="i in 50" :key="i">
        <td v-for="j in 20" :key="j">
          <v-btn @click="logScrollPosition">X</v-btn>
        </td>
      </tr>
    </tbody>
  </v-table>
</template>

<script setup>
import { ref } from 'vue'

const data = ref([])
const tableComponent = ref()

function logScrollPosition() {
  const tableElement = tableComponent.value?.$el.querySelector("table"); 

  if(!tableElement){
    console.log("table missing");
    return;
  }

  console.log(tableElement.scrollLeft);
  console.log(tableElement.scrollTop);
}
</script>

When scrolling and clicking a button, the function logScrollPosition always logs 0 for both axes. So my approach seems to be lacking something.

Do you have any ideas on how to retrieve the scroll position from tableComponent?

英文:

I'm using a Vuetify v-table component with a lot of data. On certain actions I want to read the current scrollposition of the table ( horizontal and vertical axis ). This is what I've tried so far

Playground

&lt;template&gt;
  &lt;v-table fixed-header height=&quot;500px&quot; ref=&quot;tableComponent&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th v-for=&quot;i in 20&quot;&gt;
          Col {{ i }}
        &lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr v-for=&quot;i in 50&quot; :key=&quot;i&quot;&gt;
        &lt;td v-for=&quot;j in 20&quot; :key=&quot;j&quot;&gt;
        	&lt;v-btn @click=&quot;logScrollPosition&quot;&gt;X&lt;/v-btn&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/v-table&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { ref } from &#39;vue&#39;

const data = ref([])
const tableComponent = ref()

function logScrollPosition() {
	const tableElement = tableComponent.value?.$el.querySelector(&quot;table&quot;); 
  
  if(!tableElement){
    console.log(&quot;table missing&quot;);
    return;
  }
  
  console.log(tableElement.scrollLeft);
  console.log(tableElement.scrollTop);
}
&lt;/script&gt;

When scrolling and clicking a button the function logScrollPosition always logs 0 for both axis. So my approach seems to be lacking something.

Do you have any ideas how to retrieve the scroll position from tableComponent?

答案1

得分: 2

你正试图直接从表格元素获取滚动位置。然而,表格组件的滚动发生在表格的父容器内部。要确定滚动位置,你应该访问表格的父容器而不是表格本身。

function logScrollPosition() {
  const tableElement = tableComponent.value?.$el.querySelector(".v-table__wrapper");

  if(!tableElement){
    console.log("table missing");
    return;
  }
  console.log(tableElement.scrollLeft);
  console.log(tableElement.scrollTop);
}
英文:

You are trying to obtain the scroll position directly from the table element. However, the scrolling of the table component takes place within the parent container of the table. To determine the scroll position, you should access the parent container of the table instead.

function logScrollPosition() {
  const tableElement = tableComponent.value?.$el.querySelector(&quot;.v-table__wrapper&quot;);

  if(!tableElement){
    console.log(&quot;table missing&quot;);
    return;
  }
  console.log(tableElement.scrollLeft);
  console.log(tableElement.scrollTop);
}

huangapple
  • 本文由 发表于 2023年5月10日 20:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定