I'm trying to load data into owl carousel on each click request. It works fine on first call but when i second call it disturbs the owl carousel structure and shows the items vertically stacked!
No errors in console, ajax is getting data successfully but owl carousel isn't initializing. Slider Works fine on first ajax call but it doesn't work on second third and so on
Js Code:
$('.ajax-cate').click(function(e){
e.preventDefault();
$(this).style="border-bottom:1px solid #197B81";
var _loader = '<div class="ajax-loader"><img src="/images/ajax-loader.gif"></div>';
$('#videoList').empty().html(_loader);
var cate_slug = $(this).attr('data-slug');
params.cate_slug = cate_slug;
ajaxLoadVideo(params);
});
function ajaxLoadVideo(params)
{
$('#categoryDataList').show();
$('#blogIndex').hide();
$('#videoList').removeClass('blog-slider');
$.ajax({
url: '/api/test',
method: 'GET',
data: params,
dataType: 'json',
success: function(res) {
$('#categoryDataList .group-heading h3 a').empty().html(res.category.name);
$('#categoryDataList .group-desc').empty().html(res.category.desc);
var _listHTML = '';
videos = res.videos;
for (i = 0; i < videos.length; i++) {
_listHTML += '<div class="blog-item">';
_listHTML += '<div class="blog-image">';
_listHTML += '<a href="/blog/' + videos[i].slug + '" title="' + videos[i].video_title + '">';
_listHTML += '<img alt="' + videos[i].video_title + '" src="/uploads/thumbnail/320_' + videos[i].video_picture + '">';
_listHTML += '</a>';
_listHTML += '</div>';
_listHTML += '<div class="caption">';
_listHTML += '<a class="blog-list-video-title" href="/blog/' + videos[i].slug + '" title="' + videos[i].video_title + '">' + videos[i].video_title;
_listHTML += '</a>';
_listHTML += '</div>';
_listHTML += '<div class="blog-metas">';
_listHTML += '<span class="blog-views">' + addCommas(videos[i].video_koview) + ' views</span>';
_listHTML += '</div>';
_listHTML += '</div>';
}
$('#videoList').empty().html(_listHTML);
var owl = $("#videoList");
owl.owlCarousel({
loop:false,
navRewind:false,
margin:10,
nav:true,
dots:false,
navText: '',
responsive:{
0:{
items:1
},
600:{
items:3
},
1000:{
items:5
}
}
});
$('#videoList').addClass('blog-slider');
}
});
}
HTML:
<a class="ajax-cate" data-slug="most-popular" href="/test/category/most-popular" title="">
Most Popular
</a>
HTML where ajax data is populated/inserted:
<div class="blog-category-items" id="categoryDataList" style="display: none;">
<div class="container">
<div class="blog-groups">
<div class="group-heading">
<h3><a></a></h3>
</div>
<p class="group-desc"></p>
<div class="owl-lg-dot mb-none owl-theme owl-loaded" id="videoList"></div>
</div>
</div>
</div>
I'm using owl carousel 2.0 version
EDIT:
I already have tried these tricks but none works for me:
// destroy and init in success of ajax
$('#videoList').trigger('destroy.owl.carousel').removeClass('owl-carousel owl-loaded');
$('#videoList').find('.owl-stage-outer').children().unwrap();
$('#videoList').owlCarousel({
loop:false,
navRewind:false,
margin:10,
nav:true,
dots:false,
navText: '',
responsive:{
0:{
items:1
},
600:{
items:3
},
1000:{
items:5
}
}
});