Giter VIP home page Giter VIP logo

Comments (9)

rcbyr avatar rcbyr commented on July 22, 2024 2

@sanath1188
Thank you. I think there's an error with the initial value. I believe if you set it to 0, it will work as expected. I will fix this in the next days.

from keen-slider.

divinsmathew avatar divinsmathew commented on July 22, 2024 1

Hey @rcbyr, I'm still facing the OP's same issue. From reading the comments I think the issue is supposed to be fixed, but I still can't seem to get it working while using *ngFor. I'm using Angular 10. If you could you please share a stackblitz or codesandbox of this, it'd be great! Thanks.

from keen-slider.

WarayAmine avatar WarayAmine commented on July 22, 2024 1

Hi @rcbyr , i'm having the same issue as @divinsmathew. When using ngFor in angular, the slides aren't showing, they are just blank. The data is fetched through an api, i'd like to know if there is a sample for this use case. Thanks so much for this useful project.

from keen-slider.

rcbyr avatar rcbyr commented on July 22, 2024

Hey @sanath1188

thank you. Currently, the slider does not observe any mutation on its own. So you need to tell the slider that there have been some changes. You could do this by calling the resize() function from the slider instance after the view has been updated.

Code-Example:

...
  doResize: boolean = false

  ngOnChanges(): void{
    this.doResize = true
  }

  ngAfterViewChecked(): void{
    if(this._slider && this.doResize) this._slider.resize()
    this.doResize = false
  }
...

from keen-slider.

sanath1188 avatar sanath1188 commented on July 22, 2024

Interesting. Still doesn't seem to work. Here's my code snippet:

HTML:
<div class="keen-slider" #sliderRef>
<div *ngFor="let number of numbers">
<!--Assume there's HTML enough to show numbers -->
</div>
</div>

TS:

ngAfterViewInit() {
   this.slider = new KeenSlider(this.sliderRef.nativeElement, {
      spacing: 40,
      slidesPerView: 1,
      initial: 4,
     centered: true,
      loop: false,
      mode: "snap",
      breakpoints: {
        "(min-width: 768px)": {
          slidesPerView: 2,
          mode: "snap"
        },
        "(min-width: 1200px)": {
          slidesPerView: 7,
          mode: "snap"
        }
     }
    });
  }

  ngAfterViewChecked(): void{
    if(this.slider && this.dataLoaded) {
      console.log("trying to resize")
      this.slider.resize()
      this.dataLoaded = false;
    } 
  }

The variable dataLoaded is set to true after an API loads the data. The function to call the API happens on ngOnInit()

from keen-slider.

rcbyr avatar rcbyr commented on July 22, 2024

@sanath1188 I just released a new version with a fix for that. Be aware of some style changes.

from keen-slider.

lmartins avatar lmartins commented on July 22, 2024

Still seeing the same problem with version 5.2.1, in this case on a React codebase.

  const [sliderRef, slider] = useKeenSlider({
    loop: true,
    slideChanged(s) {
      setCurrentSlide(s.details().relativeSlide);
    },
  });

  useEffect(() => {
    if (slider) {
      slider.resize();
    }
  }, [images]);

When adding items to the slider (images array) the gallery seems to react correctly, but each time I remove items from the array even though the useEffect runs and resize method too, the gallery does not remove the deleted item.

from keen-slider.

rcbyr avatar rcbyr commented on July 22, 2024

Hello @lmartins

I've created a small example with a button to remove slides and it works as expected. https://codesandbox.io/s/keen-slider-default-react-ts-zrzt1?file=/src/App.tsx

Can you create an example that will show your problem?

from keen-slider.

florian-renfer avatar florian-renfer commented on July 22, 2024

Hi @rcbyr, I was having the same issue and I found a alternative fix. The problem is after fetching data from an api and rendering the data using ngFor, there is a slight offset. So if you call update() immediately after fetching the data, it might not be rendered and the class slider__slide is not applied yet, which results in having the same slides array as before.

To fix this you can use some sort of timeout and call update() then.

   /**
   * Fetch some data from the API.
   */
 public fetchSomeData(): void {
    this.someDataService.getSomeData().subscribe({
      next: (someData: SomeData[]) => {
        this.someData = someData

        if (this.keenSlider) {
          setTimeout(() => {
            this.keenSlider?.update(undefined, 0)

            // Required when using indicator dots below the slides
            this.updateDotHelper()
          }, 1)
        }
      },
    })
  }

   /**
   * Updates the dot helper.
   */
  private updateDotHelper(): void {
    if (this.keenSlider) {
      this.dotHelper = [
        ...Array(this.keenSlider.track.details.slides.length).keys(),
      ]
    }
  }

from keen-slider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.