We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 69
    • 22 Posts
    Hello there,

    I’m new to MODx and am working on a MODx Revolution project right now. So far so good, really like it, but I can’t seem to figure out pagination.

    You can see what i’m trying to achieve in the attachment image. 3 projects on a row and a previous and next button to show the next 3 or previous 3. Also when you see the first 3, the previous button should be gone and when you see the last 3 the next button should be gone.

    I’m currently using the following code to display the 3 building projects:

    !getResources?
    &parents=`[[*id]]`
    &limit=`3`
    &tpl=`products`
    &sortbyTV=`opgeleverd`
    &sortdir=`ASC`

    How could I achieve this pagination? I tried to look into getResources as well as getPage, but i’m not getting anywhere.
      • 9102 ☆ A M B ☆
      • 318 Posts
      You’re on the right track. You want to use getPage, something like

      [[!getPage? 
      &element=`getResources` 
      &parents=`[[*id]]`
      &limit=`3`
      &tpl=`products`
      &sortbyTV=`opgeleverd`
      &sortdir=`ASC`
      ]] 
      
      <div class="pageNav">[[!+page.nav]]</div> 
      


      The trick is going to be to get the page nav to appear the way you want it to. You’ll want to set templates for pagePrevTpl and pageNextTpl that contain your arrow graphics, and then use CSS to move one of them to the position before the getPage call. You could probably accomplish that with relative positioning, as long as the images are always the same size.
        • 69
        • 22 Posts
        How do I set a template for pageNextTpl and pagePrevTpl?

        I tried to create a chunk called next and then use:

        [[!getPage?
        &pageNextTpl=`next`
        ]]
        


        That isn’t working.. it returns ’next’ instead of the content of the chunk.
          • 69
          • 22 Posts
          Nevermind I just found out how to that..

          According to the getPage documentation you have to do this by creating a new property set.. I created a new property set for the getPage snippet and named it projects.

          Now my getPage call starts with [[!getPage@projects?

          I changed the pageNextTpl property in my projects property set and now it works smiley
            • 69
            • 22 Posts
            I have another question about this...

            The getPage module produces links with: ?page=number

            How can I get this to change to just the number, like ’www.mydomain.com/projects/1’ for page 1.
              • 69
              • 22 Posts
              Ok I made that working too by writing a plugin and adding some lines to the .htaccess..

              Now my final pagination question for the day:

              When you click on a project (see the attachment in my first post) you get to a page with more information about that project.
              For instance: if you click on the image with Autobedrijf Vogel, you go to www.mydomain.com/projects/autobedrijf-vogel and get to see more information.

              That information page will also have a next and previous button, where you have to be able to go to the next project or the previous one (with the same order ofcourse as the project overview as seen in the attachment).
              The difficulty is that it can’t be linked by numbers or anything, but has to know the alias of the next and previous project in order to maintain using friendly URL’s.

              Is this possible with MODx or should I give up? grin
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                Would you mind sharing your solution for the pagination for others who might come across this topic some day?



                As for your question.. Anything’s possible. That last question is slightly more difficult than basic pagination though tongue


                As you’ve been messing around with plugins and htaccess I take it you don’t mind diving into some PHP/MODX/xPDO...

                You could start with something like this:
                $current = $modx->resource;
                $cid = $modx->resource->get('id');
                $parent = $current->getOne('Parent');
                $children = $parent->getMany('Children');
                
                $last = ''; $cur = ''; $next = ''; $gotit = 0;
                foreach ($children as $child) {
                  if ($gotit == 1) { 
                    $next == $child->get('id');
                    return json_encode(array($last,$next));
                  }
                  $last = $cur;
                  $cur = $child->get('id');
                  if ($cur == $cid) {
                    $gotit = 1;
                  }
                }


                I hope that makes sense. You first get the parent to get all children (perhaps there’s also a sibblings class, not sure about that atm), then loop through all children while saving the last one and comparing the ID of the child you’re looking at with the ID of the current resource. If you got a match, you still need one more time to find the next link and then break out of the foreach() or return something which you can then use to build the data.


                Just an idea - there’s probably a better way (or existing addon) than this one.
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 69
                  • 22 Posts
                  @Mark H

                  Thanks for your reply. I will write a full tutorial on how I achieved my pagination on the website as soon as I have it up and running (probably tomorrow or the day after) so people can see the pagination live.

                  About your code, that might work indeed, but the pagination should be in the same order as the getPage output on the project overview page.

                  Isn’t there a more simple way by using getpage/getresources to output an array?

                  If I could output the results into a simple array with just key = array index and value = resource id, then it would be easy to create the previous next buttons.

                  All I have to do then is look for a match between the current page id and the values in the array, get the corresponding array index and do a +1 or -1 to get the value of the next or previous resource id. Linking could then easily be achieved by [[~idnumber]] and it would maintain the alias/user-friendly URL’s.
                    • 69
                    • 22 Posts
                    At the moment i’m writing a plugin (based on getResources) that can be used for the simple next/previous function. You can style the next/previous buttons anyway you’d like by using html/css.
                      • 69
                      • 22 Posts
                      Ok I figured it out and it is working laugh

                      I now use getPage to generate the project-overview with pagination.. and when you click on a project and get to a detail page, that one also has pagination to go the next or previous project (while maintaining the same order).

                      ------------

                      I wrote a snippet called getPrevNext that handles a lot of the same properties that getResources uses, like the parent id, the sorting order, sorting by TV’s, depth etc.. It could be used for anything that needs a link to the next or previous resource, with orderby-control (and it is standalone, no need for getResources or getPage).. It also works perfectly with aliases aka friendly urls.

                      As soon as my website is completed (tomorrow or the day after) I’ll write a full tutorial on how I achieved both types of pagination in this topic.. Plus i’ll make my getPrevNext snippet more user-friendly (for people who don’t know php) and i’ll try to make it a downloadable add-on, or post the snippet here.