tiny-slider icon indicating copy to clipboard operation
tiny-slider copied to clipboard

How to dynamically add a slide?

Open dheerajw opened this issue 6 years ago • 27 comments

Issue description: How to dynamically add a slide?

Demo link/slider setting:

Tiny-slider version:
Browser name && version:
OS name && version:

dheerajw avatar Jul 05 '18 10:07 dheerajw

I know other slider plugins have features of add/remove slides, but it's confusing for me: when/where you gonna use these features? In admin?

ganlanyuan avatar Jul 05 '18 13:07 ganlanyuan

Yes, you guessed it rightly. We have a requirement to let the end users design and add/edit their slides with the help of a visual editor. It would be a great addition.

dheerajw avatar Jul 05 '18 15:07 dheerajw

Okay, then how do you want to pass the mark up of the slide? As a string, or HTML Element?

ganlanyuan avatar Jul 05 '18 15:07 ganlanyuan

We're fine with both the options. Basically, the idea is to have the ability to add, edit and delete any of the slides from the template.

dheerajw avatar Jul 06 '18 02:07 dheerajw

Actually you can do:

  1. slider.destory()
  2. add, edit, delete or whatever you want
  3. slider.rebuild()

ganlanyuan avatar Aug 02 '18 21:08 ganlanyuan

@ganlanyuan, note that in 2.8.5, it seems not possible to

1. slider.destroy()
2. add, edit, delete or whatever you want
3. slider.rebuild()

if the slider has been made with a DOM element passed as container, string returns another issue for me but I cant seem to add new slides after a load more and then rebuild the slider successfully,

Error : TypeError: Cannot read property 'clientWidth' of null

ThaoD5 avatar Aug 22 '18 17:08 ThaoD5

@ThaoD5 destroy() was fixed in v2.8.6

ganlanyuan avatar Aug 28 '18 13:08 ganlanyuan

Great to hear, I did fallback to a previous version in order to fix the project while this got fixed, I will give it a try :-) Thanks for your reactivity !

ThaoD5 avatar Aug 28 '18 13:08 ThaoD5

I want to be able to remove and add slides on the fly. The user selects an image and it gets added to the carousel. If it selects an image from the slider, it gets removed. Are these sort of dynamic changes possible or something that will be supported in the near future?

joeyrubio avatar Sep 08 '18 01:09 joeyrubio

@ganlanyuan for example, i working in a ecommerce and each product has variations, when i select some combination of variations (like color, size), I will want remove actually images and add new images of the new combination, that is a use case. I would be grateful if tns has these features.

aaronetto avatar Jan 08 '19 15:01 aaronetto

@joeyrubio & @aaronnunez95: Wasn't this already answered by @ganlanyuan? I might be wrong, but I think this describes how to do it:

Actually you can do: 1. slider.destory() 2. add, edit, delete or whatever you want 3. slider.rebuild()

Strongground avatar Jan 08 '19 17:01 Strongground

@Strongground exists a delay between destroy, modify the DOM and rebuild the slider.

aaronetto avatar Jan 08 '19 18:01 aaronetto

This doesn't work. After rebuild() every property in the Slider Instance is null. Tiny Slider Version 2.9.2. I really think it's a common use case to add dynamic slides for example via Ajax if there are a lot of slides to load. So native support would be nice.

ico85 avatar Nov 23 '19 11:11 ico85

@ganlanyuan Any updates on this? I'm experiencing the same issue as @ico85 :(

filip-jk avatar Dec 18 '19 17:12 filip-jk

@ganlanyuan Any updates on this? I'm experiencing the same issue as @ico85 :(

I managed to add slides dynamically by first destroying the Slider and setting the instance to null. After that Re instantiate with startindex. Be careful with Dom elements here since the destroy method manipulates the Dom and therefore former queried elements are not the same. Can't supply code now because I'm on mobile ;)

ico85 avatar Dec 18 '19 17:12 ico85

@ico85 I actually achieved it with an almost identical approach yesterday. You're right about the DOM elements: they're getting modified a lot, especially if you use a thumbnail carousel as well as the navigation for the main gallery and that required some extra work but it's fine.

I only wrote my comment because the approach that the plugin author suggests simply does not work (with the .rebuild()).

Cheers!

filip-jk avatar Dec 19 '19 10:12 filip-jk

Can somebody post relevant code to resolve this issue? Would be very much appreciated.

Have a similar problem to users here - need to dynamically add content to the slider through recreating the slider somehow - however I am having issues.

Thanks!

DavidMelnyk avatar Jan 13 '20 20:01 DavidMelnyk

Similar problem. I have a page with let‘s say 10 grid items. Each item has an image slider with on average 7 slides. But currently this causes noticeable performance issues on desktop. Not sure if it‘s caused because of lots of DOM nodes or because it f css painting issue.

Therefore dynamic (virtual) slides would be a great feature.

berndartmueller avatar Feb 03 '20 23:02 berndartmueller

EDIT: I solved my performance issue with having multiple slider instances on one page. It was caused by having the css property filter: drop-shadow(...) on those prev/next controls (to make arrow svg better visible).

Lesson beeing learned: Use css filter drop-shadow very careful!

berndartmueller avatar Feb 04 '20 12:02 berndartmueller

Currently struggling with this and I can't get it to work.

I'm building dynamically by fetching the carousel images in Angular from my backend, then adding the <img> tag after fetching the images.

No matter what I try I get the No slides found in... error and the images are all displayed with no slider effect

mfcodeworks avatar May 11 '20 13:05 mfcodeworks

Currently struggling with this and I can't get it to work.

I'm building dynamically by fetching the carousel images in Angular from my backend, then adding the <img> tag after fetching the images.

No matter what I try I get the No slides found in... error and the images are all displayed with no slider effect

Initiating seems to work using the ngAfterViewChecked hook checking if the *ngFor data exists, but not sure if dynamically adding after instantiation works

mfcodeworks avatar May 11 '20 13:05 mfcodeworks

I am using the tiny-slider 2.8.7 in a react application. I am struggling to find a way to get this destroy > add/remove > rebuild to work as expected. My use-case is that I have a carousel with cards but on desktop, the first slide is a CTA and on mobile, that slide is remove (and a different element appears outside of the carousel). The carousel seems to not accept a change in state that contains the slider items as well as conditional component rendering based on state. The repo owner has indicated this is possible, but are there any real examples of this scenario working?

DaveOdden avatar Sep 25 '20 17:09 DaveOdden

I am using the tiny-slider 2.8.7 in a react application. I am struggling to find a way to get this destroy > add/remove > rebuild to work as expected. My use-case is that I have a carousel with cards but on desktop, the first slide is a CTA and on mobile, that slide is remove (and a different element appears outside of the carousel). The carousel seems to not accept a change in state that contains the slider items as well as conditional component rendering based on state. The repo owner has indicated this is possible, but are there any real examples of this scenario working?

He did it two years ago and it was more like suggestion, not solution. Currently there is no solution.

care4pet avatar Nov 05 '20 02:11 care4pet

@DaveOdden Did you ever find a solution?

Dobby89 avatar Jan 15 '21 17:01 Dobby89

I've been struggling a bit with this, but I've succeed at the end.

My goal was to have a slider, with some filters (chips)

Here's a working example:

https://codepen.io/xanisu/pen/PobOLdg

I'm using the above commented steps:

  1. Destroy
  2. Modify / add / remove DOM
  3. Rebuild

xanisu avatar Mar 09 '21 14:03 xanisu

I've been struggling a bit with this, but I've succeed at the end.

My goal was to have a slider, with some filters (chips)

Here's a working example:

https://codepen.io/xanisu/pen/PobOLdg

I'm using the above commented steps:

  1. Destroy
  2. Modify / add / remove DOM
  3. Rebuild

its works, but the user goes back to the first item.

In my situation - I need to add a special slide, after user action and save the slider position. But I can't.

Tried adding hidden slide, but it breaks slider position.

fdkitamory avatar Apr 16 '21 09:04 fdkitamory

Yes, ideally a solution would need to append slides while retaining the current position... it's possible the user navigated to slide 2 while slide 4 finished loading, so jumping back to slide 1 is not a good experience.

awesomerobot avatar Mar 31 '22 03:03 awesomerobot