How to have infinite scroll within ajax loading

梦想的初衷 提交于 2019-12-13 00:50:07

问题


Idea: Load the a div content by ajax then load the next page content within the same div by infinite scroll.

1. Loading it by ajax:

  • Idea is that #dhvc_id is clicked when the page is first loaded, then the id is given to the function.php which locates the appropriate file then parse it on the same div.

demo.php

<?php
/**
* Template Name: Demo
*/

get_header(); ?>

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
        <div class="dhvc_demo" id="dhvc_id"></div>              
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

<script>

 //To trigger initial click
 jQuery(window).load(function() { 
    jQuery("#dhvc_id" ).trigger( 'click' );  
    return false;       
    }); 
 //Ajax loading
 jQuery(document).ready(function() {
    jQuery('#dhvc_id').click(function(e) {
        e.preventDefault();

        var tab_id = jQuery(this).attr('id'); 

        jQuery.ajax({
            type: "GET",
            url: "<?php echo admin_url('admin-ajax.php'); ?>", 
            dataType: 'html',
            data: ({ action: 'royal_front_tab', id: tab_id}),
            success: function(data){
                  jQuery('.dhvc_demo').html(data);


        },
        error: function(data)  
        {  
        alert("Error!");
        return false;
        }  

        }); 

 }); 
 }); 

</script>

Function.php

//Ajax call for front page
function royal_front_tab_callback() {  
$template_part_path = 'page-parts/01_home_' . $_GET['id'];
get_template_part($template_part_path);
exit;
}
add_action('wp_ajax_royal_front_tab', 'royal_front_tab_callback');
add_action('wp_ajax_nopriv_royal_front_tab', 'royal_front_tab_callback');

01_home_dhvc_id.php

<?php echo do_shortcode('[some_shortcode]'); ?>

So, This itself works just fine (Ajax works fine).

2. Loading the next content by infinite scroll:

Function.php

function custom_theme_js(){
wp_register_script( 'infinite_scroll',  get_template_directory_uri() . '/custom_js/jquery.infinitescroll.min.js', array('jquery'),null,false );
wp_enqueue_script('infinite_scroll');

}
add_action('wp_enqueue_scripts', 'custom_theme_js');

function custom_infinite_scroll_js() {
{ ?>
<script>
jQuery('.royal_card.dhvc-woo-row-fluid.dhvc-woo-grid-list').infinitescroll({
    navSelector  : jQuery('.dhvc-woo-pagination'),
    nextSelector : jQuery('.dhvc-woo-pagination > a.next'),
    itemSelector :  '.royal_card.dhvc-woo-row-fluid.dhvc-woo-grid-list >    .royal_card_outer.royal_card_content_grid',
    contentSelector: jQuery('.royal_card.dhvc-woo-row-fluid.dhvc-woo-grid-list'),
msgText: "fdfgdg "  
},function(newElements){
    jQuery('.dhvc-woo-separator').remove();
    jQuery('.royal_card.dhvc-woo-row-fluid.dhvc-woo-grid-list >  .royal_card_outer.royal_card_content_grid').after('');
});
</script>
<?php
}
}
add_action( 'wp_footer', 'custom_infinite_scroll_js',100 );

This works just fine as well, only when the content is not initially loaded by the ajax.

Here is what I mean:

Scenario 1: Just infinite scroll (content is not loaded by ajax)

http://sevek.staging.wpengine.com/dhvc_working-demo/

The pagination that is not loaded by ajax has following href setup:

<a href="http://sevek.staging.wpengine.com/dhvc_working-demo/page/2/">2</a>

Scenario 2: Both Ajax loading and infinite scroll (content is loaded by ajax)<--What I am trying to achieve but the url of the pagination (inspect element) is modified

http://sevek.staging.wpengine.com/dhvc-test/

The pagination that is loaded by ajax has following href setup:

<a href="http://sevek.staging.wpengine.com/wp-admin/admin-ajax.phppage/2/?action=royal_front_tab&amp;id=dhvc_id">2</a>

As you can see, when the content is loaded by ajax, the url becomes somewhat odd, and it now contains "wp-admin/admin-ajax.phppage" in the url, which does not exist and the infinite scroll cannot find it.

So, when I checked the element inspector, I got the following "modified" script .

Modified Script

 //To trigger initial click
 jQuery(window).load(function() { 
    jQuery("#dhvc_id" ).trigger( 'click' );  
    return false;       
    }); 
 //Ajax loading
 jQuery(document).ready(function() {
    jQuery('#dhvc_id').click(function(e) {
        e.preventDefault();

        var tab_id = jQuery(this).attr('id'); 

        jQuery.ajax({
            type: "GET",
            url: "http://sevek.staging.wpengine.com/wp-admin/admin-ajax.php", 
            dataType: 'html',
            data: ({ action: 'royal_front_tab', id: tab_id}),
            success: function(data){
                  jQuery('.dhvc_demo').html(data);

                jQuery('.dhvc-woo-row-fluid.dhvc-woo-grid-list').infinitescroll({
                        navSelector  : jQuery('.dhvc-woo-pagination'),            
                        nextSelector : jQuery('.dhvc-woo-pagination > a.next'),    
                        itemSelector :  '.dhvc-woo-row-fluid.dhvc-woo-grid-list > .dhvc-woo-row-fluid',
                        contentSelector: jQuery('.dhvc-woo-row-fluid.dhvc-woo-grid-list'),
                        msgText: " "
                    },function(newElements){
                        jQuery('.dhvc-woo-separator').remove();
                        jQuery('.dhvc-woo-row-fluid.dhvc-woo-grid-list > .dhvc-woo-row-fluid').after('<div class="dhvc-woo-separator"></div>');
                    });
        },
        error: function(data)  
        {  
        alert("Error!");
        return false;
        }  

        }); 

 }); 
 }); 

What is happening? How can I make it work?

来源:https://stackoverflow.com/questions/32061336/how-to-have-infinite-scroll-within-ajax-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!