Answer the question
In order to leave comments, you need to log in
How to optimize the dynamic truncate of the middle of a list?
Good evening, colleagues!
In general, there is one interesting problem - it has been solved, but the optimization leaves much to be desired, which I wanted to consult with you about.
The essence of the problem: there is an array of strings in the angular
["Java", "projects", "dev", "test", "controller", "src", "main", "resources", "application"]
Java projects dev test controller src main resources application
Java projects ... main resources application
public truncateRow() {
if (this.isExcessiveWidth() && this.truncatedItems.length > 3) {
const centralItemIndex = Math.floor((this.truncatedItems.length - 1) / 2);
this.truncatedItems.splice(centralItemIndex, 2, "...");
return setTimeout(() => {
this.truncateRow();
});
}
}
Answer the question
In order to leave comments, you need to log in
Instead of an explicit child li, you create an invisible one with opacity: 0; position: absolute to take it out of the flow and hide it visually. Unlike display: none , the element will be rendered and its geometric dimensions will be available.
Get its length, get the length of the parent, truncate the array until li is shorter than the parent.
And only then do you show the main li with a truncated array.
You can use the same li, just change the class for it.
if you don’t have complex texts there (with all sorts of rtl fragments and other perversions affecting the layout), then you can try to calculate the sizes by hand using measureText from canvas
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question