vue-masonry-css icon indicating copy to clipboard operation
vue-masonry-css copied to clipboard

Manually calling the reCalculate() function

Open robinscholz opened this issue 7 years ago • 7 comments

Is there a way to manually call the reCalculate() function? I'm trying to recalculate the column amount in a component based on the amount of images. Since the same component is used for different routes I need to somehow recalculate the columns on RouteBeforeUpdate().

Any way this is possible?

robinscholz avatar Dec 19 '17 12:12 robinscholz

Hi,

I also want to know how to achieve this @paulcollett

I have 2 cols and elements are added dynamically but when ajax finnish if it returns false the element get removed. I need to recalculate so that I dont end up with more elements on one side

Cheers, David

drosendo avatar Dec 21 '17 22:12 drosendo

I'm of the understanding you can access the methods via a reference:

<masonry ref="my-masonry"></masonry>

In your app / parent component:

if(this.$refs['my-masonry']) {
   this.$refs['my-masonry'].reCalculate();
}

This is untested so I would like to know if this is a quick solution! Otherwise, I'll look at other ways of exposing this.

paulcollett avatar Dec 29 '17 00:12 paulcollett

seems to work for me, i needed to wrap in a $nextTick() to "see" the update in the same method.

cybercode avatar Jan 11 '18 18:01 cybercode

@paulcollett works for me and I needed to wrap it in a $nextTick() as well. Thanks @cybercode for the tip

checkthemethod avatar May 05 '18 16:05 checkthemethod

Just to note the plugin from version 1.0.2 now automatically updates on child item & prop changes

paulcollett avatar Jun 04 '18 05:06 paulcollett

won't work for me. having <masonry ref="masonry" :cols="masonryCols">...</masonry> and

  get masonryCols() {// computed{masonryCols() {...}}
    return (this.view === 'masonry') ? { default: 3, 900: 2, 400: 1 } : 1;
  }

Also tried using this.$nextTick(() => {(this.$refs.masonry as any).reCalculate();}); on this.view change. It wont update.

nexorianus avatar Jul 04 '19 12:07 nexorianus

simplest fix for me was to add the key attribute to the -Tag

<masonry
            :key="view"
            ref="masonry"
            :cols="masonryCols"
            :gutter="{default: '2rem'}"
            >

So when the view variable changes it forces a re-render of the masonry component

nexorianus avatar Jul 04 '19 15:07 nexorianus