Forum Discussion

wkennis's avatar
wkennis
Guide
2 years ago

Problem creating pagination with rest2.0 query

I'm trying to create a list of blog items with pagination but this is not working. I don't know if it is because I am doing something wrong or not but i did use a code snippet i found here. The code ...
  • him_varma's avatar
    2 years ago

    Hi wkennis 

    The offset will have to be constructed dynamically. If you pass the hardcoded offset for every page then it will result in same result for every page. I have modified the code you have posted. Please find below:

    <#-- Gets page number from the URL and defaults to 1 -->
    <#assign page_number = webuisupport.path.parameters.name.get("page", 1) />
    <#-- Gets the total number of results -->
    <#assign result_list_size = rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE depth=0 AND board.id ='${coreNode.id}'"?url).data.count![] />
    <#assign offset = (page_number - 1) * 10 />
    
    <#-- gets all the recent messages-->
    <#assign messages = rest("2.0","/search?q=" + "SELECT id,subject,body,view_href,cover_image,author.login,author.id,author.view_href,kudos.sum(weight),metrics.views,post_time, post_time_friendly,search_snippet FROM messages WHERE depth=0 AND board.id ='${coreNode.id}' ORDER BY id LIMIT 10 OFFSET ${offset} "?url).data.items![] />
    
    
    <#-- Your code-->
    <ul class="message-list">
    <#list messages as message>
      <li class="message-item">
       <h2 class="subject"><a href="${message.view_href}">${message.subject}</a></h2>
      </li>
    </#list>
    </ul>
    
    <#-- Pagination Component using the config we defined above -->
    <#assign pageableItem = webuisupport.paging.pageableItem.setCurrentPageNumber(page_number).setItemsPerPage(10).setTotalItems(result_list_size?number).setPagingMode("enumerated").build />
    <@component id="common.widget.pager" pageableItem=pageableItem />

     

    In the above code I have added offest programatically in the query. Also I changed the setPaginMode to enumerated to provide better UI exeprience.


    Kindly check the solution and let me know if this helps 🙂