P粉4733635272023-09-03 19:48:23
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
<script setup> import { ref } from 'vue' const msg = ref('Hello World! too much content in this text field component to display') </script> <template> <h1 :title=msg>{{ msg }}</h1> <input v-model="msg"> </template> <style> h1{ max-width: 15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style>
P粉3421016522023-09-03 09:39:26
By modifying your code, I successfully compared the clientWidth
and scrollWidth
of the textbox.
1- Eliminated field
references.
2- Added an id to v-text-field
.
3- Added a check
function and called it in the watch
callback function.
4- Inside the check
function, I checked the clientWidth
and scrollWidth
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
<script setup> import { ref, watch } from 'vue' const msg = ref("") const isCuttingOff = ref(false) function check() { const elm = document.querySelector('#txt') isCuttingOff.value = elm.clientWidth < elm.scrollWidth; // todo : custom tooltip or any other solution for long texts } watch( msg, (currentMsg, oldMessage, callback) => { callback(check) }, { immediate: true } ) msg.value = 'Hello World! too much content in this text cfield component to display.' </script> <script></script> <template> <v-app> <div class="text-h3">Is cutting off: {{ isCuttingOff }}</div> <v-container class="w-25"> <v-text-field id="txt" v-model="msg" /> </v-container> </v-app> </template>