How do I ensure that the input field is wide enough to fully display the content?
P粉014218124
2023-09-02 22:36:56
<p>I'm using the Vuetify TextField component in a table with many columns. Maybe the component contains too much content to display, and from a user experience perspective inspecting the content by scrolling within the field would take too much time if there are many cells. </p>
<p>Pure HTML example</p>
<p>Vuetify Example</p>
<p>My first thought (if you have a better idea please let me know) is to show a tooltip to show the full content, but this would be annoying if the component is capable of showing the full content. So I only want to show the tooltip when the content is hidden/truncated. </p>
<p>So is there a way to know if the component is showing the complete content, or if there is content that is hidden/truncated? (Table performance is important, so I don't know if it's worth doing very complex calculations when values change) </p>
<p>I tried</p>
<p>(Playground)</p>
<pre class="brush:php;toolbar:false;"><script setup>
import { ref, watch } from 'vue'
const field = ref()
const msg = ref(
'Hello World! too much content in this text field component to display.'
)
const isCuttingOff = ref(false)
watch(
msg,
() => {
const inputWidth = field.value?.clientWidth
const inputValueWidth = msg.value.length // !!! measure the input value here !!!
console.log({ inputWidth, inputValueWidth })
isCuttingOff.value = inputWidth < inputValueWidth
},
{ immediate: true }
)
</script>
<template>
<v-app>
<div class="text-h3">Is cutting off: {{ isCuttingOff }}</div>
<v-container class="w-25">
<v-text-field ref="field" label="fsfdsf" v-model="msg" />
</v-container>
</v-app>
</template></pre>
<p>But</p>
<ul>
<li>At startup, the variable <code>inputWidth</code> is undefined</li>
<li>I don't know how to calculate the variable <code>inputValueWidth</code></li>
</ul></p>
Use CSS to display overflow of text, such as .... (ellipsis), and use the title attribute to display the complete content on hover, similar to a pop-up window
By modifying your code, I successfully compared the
clientWidth
andscrollWidth
of the textbox.1- Eliminated
field
references.2- Added an id to
v-text-field
.3- Added a
check
function and called it in thewatch
callback function.4- Inside the
check
function, I checked theclientWidth
andscrollWidth
of the input box.5- In order to run it on initial load, I assigned
msg
to an empty string and changed it to the original string at the bottom of the script.View here: here