I’m using Vuetify and have some content above a table. The table should have a fixed-header
but this requires a height
prop. A height of 100%
didn’t work and I can’t use 100vh
because I have some content above.
<template>
<v-app>
<v-main>
<v-alert type="info" title="some other content goes here" />
<v-table :fixed-header="true" height="100%"> <!-- height=100% doesn't work -->
<thead>
<tr>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr v-for="i in 100">
<td>x</td>
</tr>
</tbody>
</v-table>
</v-main>
</v-app>
</template>
Do you have any ideas how to setup a fixed header with a correct height?
I think the answer of Igor Moraru is almost correct. The problem is that it is not able to deal with new table rows joining the table.
2
Answers
You’ll have to compute the table height anyway. One way is to substract the alert height from the container height. What is left is the table height.
Fixed reproduction link
UPDATE:
For a more generic solution (independent of the intermediate elements between the table and the container) you can get the position of the table into the viewport (using
getBoundingClientRect
) and calculate the top and bottom offsets, then subtract them from the viewport height. This way you don’t care about what elements are above or below the table but only about the space that they require.Note: For this solution to work, initially the table should not have a fixed height (or 100% height).
UPDATE:
Refactored the solution to allow recomputing the table height on demand.
The main points to consider: using a
computed
is not applicable for this situation because the css props are not reactive in Vue. Instead make the computing in a dedicated function and call it when needed (you can trigger it even from a window resize watcher).Used the OP setup for a more focused solution.
FIXED reproduction link.
You can simply achieve this by calculating the height on the fly using calc() function. You have to calculate the pixels of the elements that are above the
v-data-table
and then subtract it from 100vh.In Vue – You can get the element height dynamically by attaching a
ref
to it and then access theclientHeight
.This will return
60
as per the alert box component you added.Live Demo :