i am trying to use a slider in combination with knockout.
The pagination just doesn't work. Because the slider is initalized on every ajax complete (see java script code at the bottom).
<div class="swiper-container">
<div class="swiper-wrapper">
<pre class="r space p-ls p-rs no-mt" data-bind="text: Pictures().length"> </pre>
<!-- ko foreach: Pic.List -->
<div class="swiper-slide">
<img data-bind="attr:{src: Image.imageUrl}" />
</div>
<!-- /ko -->
<!-- ko foreach: Pic.List --> Second foreach is needed for the pagination!
Initalize each Container!
I would have to recreate the slider every time the data changes.
<!-- /ko -->
</div>
</div>
Here is the java script code I am using right now.
// This is called every time the ajax is completed.
// I would have to somehow combine it with knockout
2)
<script type="text/javascript">
$(document).ajaxComplete(function() { // this is necessary but takes a long time to load!
jQuery(function($) { initializeContainers(); });
function initializeContainers() {
$('.swiper-container').each(function () {
var config = {
mode: 'horizontal',
calculateHeight: true,
loop: true,
initialSlide: 0,
visibilityFullFit: true
};
if ($(this).find('.swiper-wrapper').children().length > 1) {
if ($(this).find('.pagination').length < 1)
{
var $pager = $('<div class="pagination" />').appendTo(this);
config.pagination = $pager[0];
config.paginationClickable = true;
console.log($(this).find('.pagination').length);
}
}
var swiper = $(this).swiper(config); // Create the slider!
$('.arrow-left').on('click', function (e) {
e.preventDefault();
swiper.swipePrev();
});
$('.arrow-right').on('click', function (e) {
e.preventDefault();
swiper.swipeNext();
});
});
}
});
</script>