I have a page full of items that I filter by class names. This works great, however, after implementing lazy loading using ajax, I am having difficulty getting it to work well together.
I currently pull out the next page of items without applying the filter to the ajax call and then hide the items that don't match the selected filter. This means that you can select a filter, scroll to the end of the page and then get no new items showing. Or in some cases only one or two show before the lazy load is initiated again. This isnt a great user experience.
The alternative of selecting items from the ajax call with the filter seems difficult to manage. Like how do you keep track of what has already been pulled into the DOM and ensure there are no repeated items?
Can anyone help?
JQuery for filterng divs.
jQuery( function( $ ) {
var $divs = $('.box');
//filter multiple divs on select
var $selects = $('.filter_dd').on('change', function() {
var css_list_array = [];
$selects.each(function(index){
if ($(this).has('option:selected')){
if( $(this).val() !== '' ) {
var css = '.' + $(this).val();
var found = $.inArray(css, css_list_array);
if(found < 0) {
//if not already in array - add it
css_list_array.push(css);
}
}
}
});
var css_string = css_list_array.join('');
console.log(css_string);
var $el = $(css_string); //selected CSS names
console.log($divs); //divs not being selected as this is empty
$divs.removeClass('is-animated').fadeOut().promise().done(function()
{
if(css_string == null || css_string==''){
$divs.addClass('is-animated').fadeIn();
} else {
$el.addClass('is-animated').fadeIn();
}
});
});
});
Ajax code:
jQuery(document).ready(function($) {
$(window).scroll(function() {
var that = $('#loadMore');
var page = $('#loadMore').data('page');
var css_select = $('#loadMore').data('css');
var newPage = page + 1;
var ajaxurl = $('#loadMore').data('url');
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $('#loadMore').offset().top;
var elemBottom = elemTop + $('#loadMore').height();
if( $('#no-more').length == 0) {
//check if scrolled to 'load more' element
if ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)) {
if (typeof loading != 'undefined' && loading) return;
loading = true;
$('#resource_spinner').show();
$.ajax({
url: ajaxurl,
type: 'post',
data: {
page: page,
action: 'resources_load_more'
},
error: function(response) {
console.log(response);
},
success: function(response) {
if (response == 0) {
//check if any more post
if ($("#no-more").length == 0) {
$('#ajax-content').append('<div id="no-more" style="text-align:center;font-size:22px;font-weight:bold"><p>No more posts to load.</p></div>');
}
$('#loadMore').hide();
$('#resource_spinner').hide();
} else {
$('#loadMore').data('page', newPage);
$('#ajax-content').append(response);
}
},
complete: function(){
$('#resource_spinner').hide();
loading = false;
$divs = $('#ajax-content').find(".box");
if(css_select){
console.log('ajax'+ css_select);
$divs.not(css_select).hide();
}
}
});
}
}
});
});