Forum Discussion

Gursimrat's avatar
Gursimrat
Leader
11 years ago

Implement custom pagination

I have created a custom component of threads listed inside the board page, and need to implement the pagination. I don't know where to get started with this, can anyone let me know how to proceed on this.

  • Gursimrat's avatar
    Gursimrat
    11 years ago

    Thanks, I wanted to use it for forums page.

     

    Following snippet worked

     

    <#assign itemPerPage = settings.name.get("layout.messages_per_page_linear")?number!10 />

  • REST API calls to retrieve threads return the data in 'pages'. The page size and page number can be specified when making the call:

    http://lithosphere.lithium.com/t5/rest-api/bd-p/developers-rest-api?leaf-id=Board.threads#Board.threads

     

    You can determine the total number of threads using /threads/count under the appropriate node. Simple maths will tell you how many pages you need based on the total count and the page size.

     

    Custom components can take parameters:

    http://lithosphere.lithium.com/t5/developers-knowledge-base/Passing-Parameters-to-a-Custom-Component/ta-p/88704

     

    You can retrieve parameters in the URL from within your custom component:

    https://lithosphere.lithium.com/t5/developers-discussion/Component-and-parameters/m-p/42432/highlight/true#M1141

     

    This means you could create your own page number links, which include a page number parameter in the URL. That can be read by the custom component on the page in order to retrieve the corresponding data page from the REST API.

    • Gursimrat's avatar
      Gursimrat
      Leader

      I had the similar requirement as in thread http://lithosphere.lithium.com/t5/developers-discussion/Freemarker-Pagination/td-p/57090 and I have got the pagination working as per this comment and what i am doing is as below:

       

      I have set the itemPerPage = 6 and I had to change the admin setting(from backend) in Discussion Styles > Post and Topics > Topics Display > Topics per page to 6 also so that the pagination appears correctly.

       

       

      Issues: The pagination always shows upto 3 pages, irrespective of what I set at the admin end, it is becuase of the user prefrences(which of-course overrides the admin settings for the particular logged in user) in the My Settings, when I changed the Topics per page from the users My Setting to 6, than it worked fine and displayed the proper pagination.

       

      I have deduced that the pagination is build around the Users My Settings(In case user is logged in) but the list of topics which are being displayed on my Forums Page is as per my variable defined in the Custom Component. So is there any way i can override the users topic page per setting and display my pagination flawlessely.

       

       

       

      • Inactive User's avatar
        Inactive User

        I didn't have a ton of time to clean this up super well but this should get you started on some pagination in a custom component... It was originally built for a blog (hence why it pulls the pagenumber settings for that) but you could customize it however you see fit. 

         

        <#-- Gets page size from settings (You could hard code your value instead) or defaults to 10 -->
        <#assign page_size = settings.name.get("blog.entries_per_page_num")?number!10 />

        <#-- Gets page number from the URL and defaults to 1 --> <#assign page_number = webuisupport.path.parameters.name.get("page", 1) /> <#-- gets all the recent messages--> <#assign rest_call_url_string = "REST_CALL"/> <#assign messages = rest(rest_call_url_string+"?restapi.response_style=view&page_size=" + page_size +"&page="+ page_number).messages.message /> <#-- Gets the total number of results --> <#assign result_list_size = rest(rest_call_url_string + "/count").value />
        <#-- This contains the pagination configuration options --> <#assign pageable_item = webuisupport.paging.pageableItem.setCurrentPageNumber(page_number).setItemsPerPage(page_size).setTotalItems(result_list_size?number).setPagingMode("trivial").build /> <#-- the setPagingMode has a few different options -->
        <#-- Your code--> <ul class="message-list"> <#list messages as message> <li class="message-item"> <#-- Message Content --> </li> </#list> </ul>
        <#-- Pagination Component using the config we defined above --> <@component id="common.widget.pager" pageableItem=pageable_item />

         

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)

    Hi Gursimrat,

     

    not sure what your specific requirements are but you can easily find out your "boundaries" by determining the total number of threads for that board ( threads/count ) and then divide that by the number of threads you want to show for each page. That will give you the maximum number of pages (be careful of rounding) . Then you can simply use the page_size and page parameters of the threads call to pick a particular page according to your pagination logic.

     

    Hope this will help you in getting things started