HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to implement a Pager

Hi,

I have a custom plugin that creates a custom content. I already have pages that implements add, edit, delete and view( single record only) functionalities . Now I want to implement another page that will show all records with a pager. Can someone point me into a tutorial or documentation on how to do this properly.

Thanks

Comments

  • the code works, I replaced $page = $args[0] ?? 'p1' with  $page = $_GET["Page"] ?? 'p1'.

    What Is best way to hide the next button at the end of the last record when pager reach the last page?

    Thanks

  • I found out that I need to set the options of PagerModule::write() in the view to make the url route work properly and the show the pager buttons with numbers

    $PagerOptions =

       ['Wrapper' => '<span class="PagerNub">&#160;</span><div %1$s>%2$s</div>', 'RecordCount' => $this->data('count'), 'CurrentRecords' => $this->data('count')];

    if ($this->data('_PagerUrl')){

    $PagerOptions['Url'] = $this->data('_PagerUrl');}

    PagerModule::write($PagerOptions);

    What is the best way to add dropdown filter with this?

    Thanks

  • Instead of having [previous] 1...7 [next] you want to have [__3__|\/]? Then the pager module isn't suitable. The good thing is that you now already know what you need!

    • offset
    • limit
    • total record count
    • link format

    This is the base info needed to build a pager, no matter what html you choose.

    Vanilla has a Form class which can help you build dropdowns. It should look similar to this:

           // $page = Gdn::request()->get('page');
           // $recordCount = (new Gdn_Model('yourTable')->getCount());
    
           // Set example values.
           $page = 'p2';
           $recordCount = 30;
    
           // Get offset and limit
           list($offset, $limit) = offsetLimit($page, 12);
           // Calculate number of pages
           $pageCount = ceil($recordCount / $limit);
           // Build the array for the dropdown element
           // Text: "Page X", value: "pX"
           $pagerContent = [];
           for ($p = 1; $p <= $pageCount; $p++) {
               $pagerContent["p{$p}"] = sprintf(Gdn::translate('Page %s'), $p);
           }
           // Calculate current page for setting the current value in the dropdown
           $currentPage = $offset / $limit + 1;
    
           $form = new Gdn_Form();
           echo '<div class="PageControls MyPager">';
           echo $form->open(['action'=>'/plugin/foo/','id'=>'MyPagerForm']);
           echo $form->dropDown(
               'MyPager', // The form elements name
               $pagerContent, // The array for value and text in dropdown
               ['Value' => "p{$currentPage}"] // The current value
           );
           echo $form->button('GO!',['type' => 'submit']);
           echo $form->close();
           echo '</div>';
    

    You would need to add some js that will catch click events on the GO! button (or directly track changing the dropdown) and reload the current page with the appropriate page. I would try to change the forms action attribute accordingly whenever the dropdown changes.

Sign In or Register to comment.