We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35756
    • 166 Posts
    Hi there!

    Is it possible to implement Infinite Scroll on a getResources call? I tried several ways, but nothing worked for me...

    This is the getResources-call:

    [[!getResources? &parents=`2,3,4,5,6,7,8` &limit=`5` &includeTVs=`1` &processTVs=`1` &tpl=`BLOCK` &includeContent=`1` &sortby=`{"publishedon":"DESC","createdon":"DESC"}`]]


    This is the BLOCK-chunk:

    <div class="infinite">
    <strong><b>[[+longtitle]]</b> <span style="font-family:Arial; font-weight:bold;">//</span></strong> [[+menutitle]]
    [[!getImageList? &tpl=`fancybox` &tvname=`fancyboxTv` &limit=`1` &docid=`[[+id]]`]]
    [[+img.1]]
    </div>


    The div's are inside another div called content

    And here's the javascript-call:

    	    <script type="text/javascript">   
    	        $('.content').infinitescroll({
    	            navSelector  : "a.more",           
    	            nextSelector : "a.more",   
    	            itemSelector : "div.infinite",         
    	            debug        : false,
    	            loadingImg   : "./includes/infinite-scroll/ajax-loader.gif"
    	        });
    	    </script>


    I also tried to use it with getPage:

    [[!getPage? &element=`getResources` &parents=`2,3,4,5,6,7,8` &tpl=`BLOCK` &limit=`5` &includeTVs=`1` &processTVs=`1` &includeContent=`1` ]]



    This thread I found, but this doesn't work for me too:

    https://forums.modx.com/thread/26847/pagination-without-refreshing-the-page?page=2#dis-post-138105


    How can I get this to work? [ed. note: profilneurotiker last edited this post 13 years, 11 months ago.]
      • 35756
      • 166 Posts
      So far it's working after hours of trying and researching, but only with three "static" resources (index.html, index2.html, index3.html).

      Snippet-Call via context in index.html

      [[!getResources? 
      &parents=`2,3,4,5,6,7,8` 
      &limit=`6` 
      &includeTVs=`1` 
      &processTVs=`1` 
      &tpl=`BLOCK` 
      &includeContent=`1` 
      &sortby=`{"publishedon":"DESC","createdon":"DESC"}`
      ]]
      <a class="mehr" href="index2.html">MEHR LADEN</a>



      Snippet-Call in index2.html

      [[!getResources? 
      &parents=`2,3,4,5,6,7,8` 
      &limit=`6` 
      &offset=`6` 
      &includeTVs=`1` 
      &processTVs=`1` 
      &tpl=`BLOCK` 
      &includeContent=`1` 
      &sortby=`{"publishedon":"DESC","createdon":"DESC"}`
      ]]



      Snippet-Call in index3.html

      [[!getResources? 
      &parents=`2,3,4,5,6,7,8` 
      &limit=`6` 
      &offset=`12` 
      &includeTVs=`1` 
      &processTVs=`1` 
      &tpl=`BLOCK` 
      &includeContent=`1` 
      &sortby=`{"publishedon":"DESC","createdon":"DESC"}`
      ]]
      



      BLOCK-Chunk

      <div class="infinite">
      <a onclick="return get_position();" href="[[~[[+id]]]]">
      <strong><b>[[+longtitle]]</b> <span style="font-family:Arial; font-weight:bold;">//</span></strong> [[+menutitle]]
      [[!getImageList? &tpl=`fancybox` &tvname=`fancyboxTv` &limit=`1` &docid=`[[+id]]`]]
      [[+img.1]]
      </a>
      </div>



      Script-Calls in the Template

      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
      <script type="text/javascript" src="./includes/jquery.cookie.js"></script>
      
      <script type="text/javascript">
      if (!window.console) console = {log: function() {}}; // IE8 Fix for Infinite Scroll
      
      // set Cookie for top-position
      $(document).ready(function () {
      function get_position(){
       var top_position = document.documentElement.scrollTop;
       $.cookie('pos', top_position);
      };
      function set_position(){
       var top_position = $.cookie('pos');
       window.scrollTo(0,top_position)
       $.cookie('pos', null);
      }; 
      });
      </script>
      // end of Cookie-Setting for top-position
      // works in Opera and Safari, not in IE, FF and Chrome
      
      
      <script src="./includes/infinite-scroll/jquery.infinitescroll.min.js"></script>
      <script type="text/javascript">
      $(document).ready(function(){
      
          // set up infinite scroll
          $('.inhalt').infinitescroll({
              // insert your options here
        navSelector   : "a.mehr:last",
        nextSelector  : "a.mehr:last",
        itemSelector  : "div.infinite",
        debug           :  false,
        animate         :  true,  
        loading         : { finishedMsg: "<em>Keine weiteren Inhalte vorhanden.</em>",
                               msgText: "<em>Lade weitere Inhalte</em>" }
          });
      
          // unbind infinite scroll from automatically fetching the next page
          jQuery(window).unbind('.infscr');
          
          // when the a tag (class 'mehr') is clicked, fetch the next page
          $('.mehr').click(function(e){        
              e.preventDefault();
              $('.inhalt').infinitescroll('retrieve');
              // the plugin currently sets 'display:none' on your nav selector
              // this is probably going to change, but for the moment, just do this.
              $(this).css('display','block');
                              // remove the paginator when we are done.
                              $(document).ajaxError(function(e,xhr,opt){
                                  if (xhr.status == 404) $(".mehr-start").remove();
                              });        
          });    
      });
      </script>


      So now I have the function of a triggered infinite scroll with a working back-button in Safari and Opera, but this function doesn't work in IE, FF and Chrome (when pushing the back button I get redirected to the top of the resource, the opened "pages"/fetches from before are closed).

      Because I don't want to create always a new resource with the same Snippet-call except the changing offset-value, how can I manage it dynamically with the getResources call? The other options of the getResources-Call remain the same...
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Maybe you should be using getPage instead?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 35756
          • 166 Posts
          Hi Susan!

          Thx for your reply!

          I changed the getResources-Call to the above posted getPage-Call...but this seems to me the same in two different ways...I would have to create a new resource for each new fetch with each time a modified value for the offset. So that's not the dynamic way, as I would like it to have...

          Is there a way to alter the offset-value - maybe by regarding the amount of clicks on the "mehr"(more)-button? If clicked once, then set offset to 12, if clicked twice set offset to 18 and so on in each new Snippet-Call (which I would call once in the template?)...but this seems like an unusual way for me?!?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I would think that the getPage snippet would be on a resource with no template, to act as the AJAX processor, and you could use another snippet to read the params sent and use that snippet to set the properties for the getPage snippet. The getPage snippet would be configured to generate whatever code the scroller wants.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 35756
              • 166 Posts
              I finally got it working with the infos in this thread:

              https://forums.modx.com/thread/?thread=26847&page=2