Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax call, Isotope, and Infinite Scroll (WordPress Search Filter) #1626

Open
jessk4code opened this issue Feb 22, 2022 · 2 comments
Open

Ajax call, Isotope, and Infinite Scroll (WordPress Search Filter) #1626

jessk4code opened this issue Feb 22, 2022 · 2 comments

Comments

@jessk4code
Copy link

I am trying to use Isotope and Infinite Scroll in an Ajax call.

I applied somewhat of the answer from these issues:
#1481
#1045

I understand about using appended to add the new items to the Isotope instance. As @desandro explained, but...
it still does not work with how I implemented it.

FYI: In my StackExchange answer, the attempted solution below is not featured.

Here is the full code:

jQuery(window).on('load', function () {

	let currentLocation = window.location.href;
	const ptParams = new Proxy(new URLSearchParams(window.location.search), {get: (searchParams, prop) => searchParams.get(prop),});

	let post_type_value = ptParams.post_type;
	let $scroll_container = jQuery('.scroll-content');
    
    $scroll_container.isotope({
    	layoutMode: 'fitRows',
    	itemSelector: '.scroll-post'
    }); //isotope

	jQuery('#filter').submit(function () {
		var filter = jQuery('#filter');
		jQuery.ajax({
			url: filter.attr('action'),
			data: filter.serialize(),
			type: filter.attr('method'), // POST
			beforeSend: function (xhr) {
				filter.find('button').text('Processing...');
			},
			success: function (data) {
				filter.find('button').text('Apply filter');
				//$scroll_container.html(data);
                                $scroll_container.append(data); 
                                let $items = jQuery( data ).find('.scroll-post');
  				$scroll_container.isotope( 'appended', $items );
			} //success
		}); // jQuery ajax
		return false;
	}); //submit function

    let fhsFit = $scroll_container.data('isotope');
    
    if ( post_type_value === 'opone' || post_type_value === 'optwo' || post_type_value === 'opthree' ) {
        $scroll_container.infiniteScroll({
            path: 'page/{{#}}/?s=' + searchParam.search_term + '&post_type=' + post_type_value,
            append: '.scroll-post',
            button: '.btn-scroll',
            outlayer: fhsFit,
            loadOnScroll: false,
            scrollThreshold: 300,
            status: '.page-load-status',
            hideNav: '.pagination'
        }); //infinite scroll
    } else {
        $scroll_container.infiniteScroll({
            path: 'page/{{#}}/?s=' + searchParam.search_term + '&post_type=any',
            append: '.scroll-post',
            button: '.btn-scroll',
            outlayer: fhsFit,
            loadOnScroll: false,
            scrollThreshold: 300,
            status: '.page-load-status',
            hideNav: '.pagination'
        }); //infinite scroll
    } //if statement

    jQuery('.btn-scroll').on('click', function () {
        $scroll_container.on('load.infiniteScroll', function (event) {
            $scroll_container.isotope('layout');
            jQuery('.page-load-status').detach().appendTo(jQuery('.scroll-content'));
        }); //on load function
    }); // on click function
}); // window on load

Test case: https://bit.ly/3JNz6FI
Full Issue on WordPress Stack Exchange: https://wordpress.stackexchange.com/questions/402963/wordpress-search-ajax-isotope-infinitescroll

@jessk4code
Copy link
Author

I was able to come up with a solution and posted it on WordPress Stackexchange.

@jessk4code
Copy link
Author

jessk4code commented Feb 25, 2022

@desandro @ryanza @Dachande663 @DrorCohenCC Bad news... $container.data('infniteScroll'); does not work on v3 or v4. I was able to retrieve data with isotope $container.data('isotope'); but not with infiniteScroll.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant