link1507 link1508 link1509 link1510 link1511 link1512 link1513 link1514 link1515 link1516 link1517 link1518 link1519 link1520 link1521 link1522 link1523 link1524 link1525 link1526 link1527 link1528 link1529 link1530 link1531 link1532 link1533 link1534 link1535 link1536 link1537 link1538 link1539 link1540 link1541 link1542 link1543 link1544 link1545 link1546 link1547 link1548 link1549 link1550 link1551 link1552 link1553 link1554 link1555 link1556 link1557 link1558 link1559 link1560 link1561 link1562 link1563 link1564 link1565 link1566 link1567 link1568 link1569 link1570 link1571 link1572 link1573 link1574 link1575 link1576 link1577 link1578 link1579 link1580 link1581 link1582 link1583 link1584 link1585 link1586 link1587 link1588 link1589 link1590 link1591 link1592 link1593 link1594 link1595 link1596 link1597 link1598 link1599 link1600 link1601 link1602 link1603 link1604 link1605 link1606 link1607 link1608 link1609 link1610 link1611 link1612 link1613 link1614 link1615 link1616 link1617 link1618 link1619 link1620 link1621 link1622 link1623 link1624 link1625 link1626 link1627 link1628 link1629 link1630 link1631 link1632 link1633 link1634 link1635 link1636 link1637 link1638 link1639 link1640 link1641 link1642 link1643

[Vue.js] Can you pass an element to a function within the template in Vue?

I’m trying to calculate and set an element’s max-height style programmatically based on the number of children it has. there is to do this on four separate elements, each with a different number of children, so I can’t just create a single computed property. I already have the logic to calculate the max-height in the function, but I’m unable to pass an element from the template into a function.

I’ve tried the following solutions with no luck:

<div ref=”div1” :style=”{ maxHeight: getMaxHeight($refs.div1) }”></div>
This didn’t work because $refs is not yet defined at the time I’m passing it into the function.
Trying to pass this or $event.target to getMaxHeight(). This didn’t work either because this doesn’t refer to the current element, and there was no event since I’m not in a v-on event handler.

The only other solution I can think of is creating four computed properties that each call getMaxHeight() with the $ref, but if I can handle it from a single function called with different params, it would be easier to maintain. If possible, I would like to pass the element itself from the template. Does anyone know of a way to do this, or a more elegant approach to solving this problem?

Solution :

Making a custom directive thata operates directly on the div element would probably be the best shot. You could create a directive component like:

export default {
name: ‘maxheight’,
bind(el) {
const numberOfChildren = el.children.length;

// rest of the max height logic here

el.style.maxHeight = ‘100px’;
}
}

Then just make sure to import the directive in the file you plan on using it, and add it to the div element:

<div ref=”div1” maxheight></div>

Solution 2:

A cheap trick I learned with vue.js is that if you require anything in the template that isnt loaded when the template is mounted is to just put a template with a v-if on it:

<template v-if=”$refs”>
<div ref=”div1” :style=”{ maxHeight: getMaxHeight($refs.div1) }”></div>
</template>

around it. This might look dirty at first, but the thing is, it does the job without loads of extra code and time spend and prevents the errors.

Also, a small improvement in code length on the expandable-function:

const expandable = el => el.style.maxHeight =
( el.classList.contains(‘expanded’) ?
el.children.map(c=>c.scrollHeight).reduce((h1,h2)=>h1+h2)
: 0 ) + ‘px’;

Solution 3:

I ended up creating a directive like was suggested. It tries to expand/compress when:

It’s clicked
Its classes change
The element or its children update

vue.js component:

<button @click=”toggleAccordion($event.currentTarget.nextElementSibling)”></button>
<div @click=”toggleAccordion($event.currentTarget)” v-accordion-toggle>
<myComponent v-for=”data in dataList” :data=”data”></myComponent>
</div>

…..

private toggleAccordion(elem: HTMLElement): void {
elem.classList.toggle(“expanded”);
}

Directive: Accordion.ts

const expandable = (el: HTMLElement) => el.style.maxHeight = (el.classList.contains(“expanded”) ?
[…el.children].map(c => c.scrollHeight).reduce((h1, h2) => h1 + h2) : “0”) + “px”;

Vue.directive(“accordion-toggle”, {
bind: (el: HTMLElement, binding: any, vnode: any) => {
el.onclick = ($event: any) => {
expandable($event.currentTarget) ; // When the element is clicked
};

// If the classes on the elem change, like another button adding .expanded class
const observer = new MutationObserver(() => expandable(el));
observer.observe(el, {
attributes: true,
attributeFilter: [“class”],
});
},
componentUpdated: (el: HTMLElement) => {
expandable(el); // When the component (or its children) update
}
});