How to reinitialize Owl Carousel after an ajax call?

后端 未结 7 1599
礼貌的吻别
礼貌的吻别 2020-12-20 12:12

I am trying to reinitialize Owl carousel after a successful ajax call. The ajax call will change the data but the view should stay the same.I am having an issue where the vi

相关标签:
7条回答
  • 2020-12-20 12:21

    I went through the same problem and tried reinit() method but it was not working for me, so I tried destroy and init again and it worked.

    $.ajax({
        type: 'get',
        url: '/api/v1/companies',
        ...,
        success: function(data) {
            $("#main-company-carousel").data('owlCarousel').destroy();
            $("#main-company-carousel").owlCarousel({
                items : 3 
            });
        }
    });
    
    0 讨论(0)
  • 2020-12-20 12:28
    $('#owl-demo').data('owlCarousel').reinit();
    
    0 讨论(0)
  • 2020-12-20 12:29

    This one works for me in version 2

    var owl = $('#owl-carousel');
    owl.trigger('destroy.owl.carousel');
    owl.owlCarousel({
       items: 1,
    });
    
    0 讨论(0)
  • 2020-12-20 12:30

    Try $(window).load() instead of reinitialize

    0 讨论(0)
  • 2020-12-20 12:31

    The example below works.

    Initializing the carousel:

    owl = $("#owl-demo");
    
    owl.owlCarousel({
      items: 10,
      autoPlay: 1000,
    });
    

    And when you use the ajax callback, try:

    owl.data('owlCarousel').destroy();
    
    owl.owlCarousel({
      items: 5,
      autoPlay: 1000,
    });
    

    I create a fiddle to explain you how to re-initialize the carousel: http://jsfiddle.net/s10bgckL/959/

    PS: I did not create an array of options just if you want to modify some parameters as speed, quantity of displayed items, etc.

    I hope it helps.

    0 讨论(0)
  • 2020-12-20 12:32

    This should help:

    /*
     reinit() method reinitialize plugin 
    
     Syntax:
     owldata.reinit(newOptions)
    
     Yes! you can reinit plugin with new options. Old options
     will be overwritten if exist or added if new.
    
     You can easly add new content by ajax or change old options with reinit method.
     */
    
     $('.reinit').click(function(e){
     e.preventDefault()
     if(booleanValue === true){
      booleanValue = false;
      } else if(booleanValue === false){
      booleanValue = true;
    }
    
    owl.data('owlCarousel').reinit({
        singleItem : booleanValue
      });
    })
    
    0 讨论(0)
提交回复
热议问题