16

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 view (the carousel structure) will not reinitialize. Everything is fine upon page load.

I'm using version 1.3.3

$(document).ready(function() {
 $(".owl-carousel").owlCarousel({
   items : 3
 });
});

Ajax call

$.ajax({
    type: 'get',
    url: '/public/index',
    dataType: 'script',
    data: data_send,
      success: function(data) {
       $(".owl-carousel").owlCarousel({
         items: 3
       });
      }
   });
}

Am I missing something that I need to do? I have looked at this issue on the github page and tried the suggestions but to no avail.

Edit

From the advice given, I have created these two functions

function owlCarousel() {
  var owl = $(".owl-carousel"); 
  //init carousel
  owl.owlCarousel();
    owl.data('owlCarousel').reinit({
     items : 3
    });
}

function destroyOwlCarousel() {
  var owl = $(".owl-carousel");
  //init carousel
  owl.owlCarousel();
    owl.data('owlCarousel').destroy();
  }
}

It seems to work, but wondering if this is the correct way to be doing this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • Here's the Owl Carousel page that might help - http://owlgraphic.com/owlcarousel/demos/manipulations.html Have you tried using addItem() or reinit() – Mathias Rechtzigel Jan 06 '15 at 15:50
  • thanks for the link..trying to put something together using their examples, but not working so far – Richlewis Jan 06 '15 at 16:00

8 Answers8

7

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.

Eduardo
  • 508
  • 6
  • 18
1

This one works for me in version 2

var owl = $('#owl-carousel');
owl.trigger('destroy.owl.carousel');
owl.owlCarousel({
   items: 1,
});
0

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
  });
})
Chris
  • 453
  • 3
  • 15
  • thanks, forgive me for being dumb here but if i wanted to create a function that gets run within the ajax success callback how would i construct that? as i cant really see how the above is working. – Richlewis Jan 06 '15 at 18:04
  • think i got it, will post my example up – Richlewis Jan 06 '15 at 18:13
0

Try it , its exist in owl documention :

//Initialize Plugin
    $(".owl-carousel").owlCarousel()

    //get carousel instance data and store it in variable owl
    var owl = $(".owl-carousel").data('owlCarousel');

    owl.reinit(options)
Rob
  • 26,989
  • 16
  • 82
  • 98
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
0

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 
        });
    }
});
Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39
0

Check out this:

$.ajax({
            
            type: 'POST',
            url: '/wp-admin/admin-ajax.php',
            dataType: 'html',
            data: {
              category: catName,
            },    
            success: function(response) {
                    
                    $('.show_best_top_popular').html(response);
                    
                    var owl = $('.owl-carousal');
                    
                    owl.trigger('destroy.owl.carousel');
                    
                    owl.owlCarousel({
                        loop: true,
                        margin: 10,
                        nav: true,
                    });
                }
});

After lots of try, I did it. Thanks.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
-2

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

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • This could have ripple effects outside of just owl-carousel - many things listen for load that may not be aware of this narrow ajax call. Not wrong, just a huge sledgehammer. – allicarn Jul 12 '17 at 17:37
-3
$('#owl-demo').data('owlCarousel').reinit();
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
venkat
  • 73
  • 2
  • 14