Comments (12)
You should change the ref
key.
For example, you have 2 Lightbox A and Lightbox B
<LightBox
:images="imagesA"
ref="lightboxA"
:show-light-box="false"
></LightBox>
<LightBox
:images="imagesB"
ref="lightboxB"
:show-light-box="false"
></LightBox>
To open lightbox A, just do this.$refs.lightboxA.showImage(0)
. Similarly, to open lightbox B, this.$refs.lightboxB.showImage(0)
from vue-image-lightbox.
Aha, let me show a part of code:
<div v-for="message in messages" :key="message.id" :class="'row '+((message.from.charAt(0) == 'm') ? 'justify-content-start' : 'justify-content-end')">
<b-card-body class="col-12 col-sm-8">
<b-card :bg-variant="((message.from.charAt(0) == 'm') ? 'primary' : 'warning')" :header="getDate(message.date) + ' | # '+message.id + '| attachments = '+message.attachments.length">
<p class="card-text" v-html="message.content">
</p>
<em v-if="message.attachments.length > 0" slot="footer">
<div class="row" >
<b-img v-for="(image, index) in pics[message.id]" rounded v-lazy="image.src" width="75" height="75" blank-color="#777" alt="img" class="m-1" @click="openGallery(message.id, index)" :key="index"/>
</div>
<LightBox v-if="message.attachments.length > 0"
:images="pics[message.id]"
:ref="'lightbox-'+message.id"
:show-caption="true"
:show-light-box="false"
></LightBox>
</em>
</b-card>
</b-card-body>
</div>
As you see it's in loop, so I've tried as you said before, but I've got this insted one instance if lightbox:
0: VueComponent {_uid: 15, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} 1: VueComponent {_uid: 19, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} 2: VueComponent {_uid: 23, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} 3: VueComponent {_uid: 27, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} 4: VueComponent {_uid: 31, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …}
from vue-image-lightbox.
I have {lightbox-24183: Array(5)} and 5 instances inside it with the same values... I guess this happens for every iteration
from vue-image-lightbox.
I see. In this case, it is unnecessary to add lightbox to every step of the loop. Instead of that, you should use only 1 instance of lightbox and change your code a little bit.
<div v-for="message, messageIndex in messages" :key="message.id">
<b-card-body class="col-12 col-sm-8">
<p class="card-text" v-html="message.content">
</p>
<em v-if="message.attachments.length > 0" slot="footer">
<div class="row" >
<b-img
v-for="(image, imageIndex) in pics[message.id]"
v-lazy="image.src"
@click="openGallery(messageIndex, imageIndex)"
:key="index"
/>
</div>
</em>
</b-card>
</b-card-body>
</div>
<LightBox v-if="messages[currentIndex].attachments.length > 0"
:images="pics[messages[currentIndex].id]"
:ref="lightbox"
:show-caption="true"
:show-light-box="false"
></LightBox>
The variable currentIndex
is the index of messages
that you select. Therefore, you should change your openGallery
method a little bit:
data() {
return {
currentIndex: null,
}
},
methods: {
openGallery(messageIndex, imageIndex) {
this.$set(this, 'currentIndex', messageIndex)
... // Your old method
}
}
You would need to render only 1 instance of lightbox in this case and the rendering performance would be much better
from vue-image-lightbox.
Got it, let me try
from vue-image-lightbox.
Hmm, I 'v got weird situation, in openGallery:
console.log(this.$refs); console.log(this.$refs.lightbox);
shows:
{} lightbox: VueComponent {_uid: 25, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} __proto__: Object
and
undefined
from vue-image-lightbox.
I think you should remove v-if
in lightbox or change it to v-show
from vue-image-lightbox.
OMG, right, thanx a lot, worked!
from vue-image-lightbox.
No problem. I am not sure what the idea of v-if
in this case. I think you should remove it because changing to v-show
doesn't mean anything.
from vue-image-lightbox.
Let me explain:
I 'v got list of messages, every message could have an attachments or not.
When I fetching messages from server - I put all attachments to pics list : pics[message.id] = [array of objects for light box]
Then on click I need to activate your component and point images property to related pics[message.id].
And now I see errors in console because I changing dynamically currentGalleryInex and it = null from the start...
from vue-image-lightbox.
My code sample is just a demo. I think you should check the currentIndex
more to make sure everything work. For example, you can change currentIndex
to 0 at the beginning.
from vue-image-lightbox.
In my case I've used message Id as index and I've used v=if or v-show to activate component if I have attachments, because sometimes I haven't, and I don't want to initiate component.
from vue-image-lightbox.
Related Issues (20)
- Multi video playing. HOT 1
- Possible to set target of lightbox to be the parent of an iframe? HOT 3
- Use lightbox with Nuxt.js HOT 2
- Swipe actions don't initialize when media is initially empty HOT 1
- Clicking on the image causes the entire parent element to turn into a black screen. HOT 3
- How to custom current image display in light-box by using slot? HOT 3
- Why remove slot footer when "media.length = 1"? HOT 4
- Base64 image HOT 1
- VueJS3 support
- "Cannot read property 'type' of undefined" error when media array is empty HOT 11
- Missing type definition for TypeScript HOT 2
- onClosed not working
- SVG not load according to insinsic size
- Thumbnail not load
- Thumbnails should scroll horizontally
- Multiple videos are not re-rendering the video component when switching between them HOT 1
- Add pdf support HOT 2
- showImage flashes first image before showing desired image HOT 3
- how to close the light box when click outside of the image?
- Multi Video play problem HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-image-lightbox.