To keep the text always on top of the table, you can adjust the HTML structure and apply CSS positioning. Here's an updated version of the table component with the text always on top:
To dynamically adjust the font size of the text if wrapping is needed, you can leverage Vue's reactivity and CSS classes. Here's an updated version of the code with the font size adjusted based on wrapping:
To use this table component, you can pass the array of objects as the items prop. The component will dynamically generate the table headers based on the object keys, and the table rows will display the corresponding values for each key. For example:
In this example, the table component will display three rows with three columns each: id, name, and age. Each row corresponds to one object in the data array.
Here, we added a computed property called filteredData that filters the props.items array based on the condition item.name === props.one.name. It then slices the resulting array to only include the first three items.
The v-for loop in the tbody section will loop through the filteredData array, which will now contain only three rows with the item[key] values of props.one.name.
In this updated code, the table and the text are wrapped in a div container with the class table-wrapper. We then use CSS positioning to make the text container (div.text-container) overlap the table by setting it to position: absolute. The top and left properties position the text at the top-left corner of the container. We also set z-index: 1 to ensure that the text appears on top of the table.
Adjust the margin-top value in the table styles as needed to provide spacing between the text and the table rows.
<template>
<div class="ps-2 mx-1 w-100" :class="{'fs-2': !isTextOverflow, 'fs-1': isTextOverflow}" style="position: relative; z-index: 99;">
{{ props.one.name }}
<span class="lead mb-4 mx-1 position-absolute">({{ props.one.iso }})</span>
</div>
</template>
<script>
import { computed, watch, ref } from 'vue'
export default {
props: {
one: {
type: Object,
required: true
}
},
setup(props) {
const isTextOverflow = ref(false)
const checkTextOverflow = () => {
const textContainer = document.querySelector('.ps-2')
if (textContainer) {
isTextOverflow.value = textContainer.scrollWidth > textContainer.clientWidth
}
}
watch(
() => props.one,
() => {
isTextOverflow.value = false
// Call nextTick to wait for the DOM to finish rendering
// before checking if the text overflows
// You can remove the nextTick if this component only renders once
// and doesn't receive updates to props.one
window.setTimeout(() => {
checkTextOverflow()
}, 0)
},
{ immediate: true }
)
// Check if the text overflows when the component first renders
// You can remove this if statement if this component only renders once
if (props.one) {
checkTextOverflow()
}
return {
isTextOverflow
}
}
}
</script>
<style scoped>
.fs-1 {
font-size: 1rem;
}
.fs-2 {
font-size: 2rem;
}
</style>
In this updated version, we use a reactive variable isTextOverflow to determine if the text overflows its container. Initially, we set it to false. We then use a watcher to listen for changes to the props.one object. When it changes, we reset isTextOverflow to false and then check if the text overflows the container using scrollWidth > clientWidth. If it does, isTextOverflow is set to true.
Based on the value of isTextOverflow, we conditionally apply the CSS classes fs-2 or fs-1 to adjust the font size, giving a smaller font size with the class fs-1 when there is text overflow.
Feel free to adjust the fs-2 and fs-1 classes' font size values in the CSS to fit your specific needs.