Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

16

Jul

Pagination in Joomla Component

Pagination in Joomla Component

Posted On : July 16, 2013

| No Comment

This Joomla Component Tutorial will guide you how to use pagination in Joomla Component using only view part.Pagination Object can be created with calling the constructor of class JPagination (in libraries\joomla\html\pagination.php) like shown below.

1
2
3
4
$this->_pagination = new JPagination($total, JRequest::getVar('limitstart'), JRequest::getVar('limit') );
Where,
$this->_pagination=pagination object
$total = total number of records.


Pagination in Joomla can be done in model or view part of the component.

Using View Part
In this part we will not use model part here.
Changes in the view part (view.html.php).
As we will not use model here we have to put all the code of model in view.html.php.

Getting the Data
Here we will not use setState() and getState() function we will use here setVar() and getVar() function to get and set the variable.

1
2
3
4
5
6
7
8
9
10
11
$this->msg = array();
 for($s=0;$s<20;$s++)
 {
 $this->msg[$s]->id = $s+1;
 $this->msg[$s]->greeting = 'Hello '.$s;
 }
 $total = count($this->msg);
 if (JRequest::getVar('limit') > 0) {
 $this->msg    = array_splice($this->msg, JRequest::getVar('limitstart'), JRequest::getVar('limit'));
 }
 $this->items = $this->msg;

Getting the Pagination

1
2
3
jimport('joomla.html.pagination');
 $this->_pagination = new JPagination($total, JRequest::getVar('limitstart'), JRequest::getVar('limit') );
 $this->pagination = $this->_pagination;

Setting the limit and limitstart in the view’s class constructor.

1
2
JRequest::setVar('limit', JRequest::getVar('limit', 5, '', 'int'));
 JRequest::setVar('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));

The complete code of view.html.php would look like this:

//No direct access to this file should be called by Joomla

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 defined('_JEXEC') or die('Restricted Access');
 
 //import joomla controller library
 jimport('joomla.application.component.view');
 
 class HelloViewHello extends JView
 {
     function __construct()
     {
     parent::__construct();
     // Set the pagination request variables
     JRequest::setVar('limit', JRequest::getVar('limit', 5, '', 'int'));
     JRequest::setVar('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
     }
     //Overrite JView display method
     function display($tpl = null)
     {
     // Assign data to the view
     $this->msg = array();
     for($s=0;$s<20;$s++)
     {
     $this->msg[$s]->id = $s+1;
     $this->msg[$s]->greeting = 'Hello '.$s;
     }
     $total = count($this->msg);
     if (JRequest::getVar('limit') > 0) {
     $this->msg    = array_splice($this->msg, JRequest::getVar('limitstart'), JRequest::getVar('limit'));
     }
 
     jimport('joomla.html.pagination');
     $this->_pagination = new JPagination($total, JRequest::getVar('limitstart'), JRequest::getVar('limit') );
 
     $this->items = $this->msg;
     $this->pagination = $this->_pagination; 
 
 // Check for errors.
     if (count($errors = $this->get('Errors')))
     {
         JError::raiseError(500, implode('', $errors));
         return false;
     }
 
     // Display the template
     parent::display($tpl);
     }
 }
 ?>

Changes in the template Part (default.php)

To display the button for start,previous,next and End we will use getListFooter() function of JPagination class as shown below

1
<!--?php echo $this--->pagination-&gt;getListFooter(); ?&gt;

The complete code of default.php would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!--?php  // No direct access to this file  defined('_JEXEC') or die('Restricted Access');  // load tooltip behavior  JHtml::_('behavior.tooltip');  ?-->
 
 
<form action="&lt;?php echo JRoute::_('index.php?option=com_helloworld'); ?&gt;" method="post">
items as $i =&gt; $item): ?&gt;
 
<!--?php endforeach; ?-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<table>
<thead>
<tr>
<th width="5">
             <!--?php echo JText::_(' COM_HELLO_WORLD_HEADING_ID'); ?--></th>
<th width="20">
<input onclick="checkAll(&lt;?php echo count($this-&gt;items); ?&gt;);" name="toggle" type="checkbox" /></th>
<th>
             <!--?php echo JText::_('COM_HELLO_WORLD_HEADING_TEXT'); ?--></th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3"><!--?php echo $this--->pagination-&gt;getListFooter(); ?&gt;</td>
</tr>
</tfoot>
<tbody>
         <!--?php foreach($this--->
<tr class="row&lt;?php echo $i % 2; ?&gt;">
<td>
             <!--?php echo $item--->id; ?&gt;</td>
<td>
             <!--?php echo JHtml::_('grid.id', $i, $item--->id); ?&gt;</td>
<td>
             <!--?php echo $item--->greeting; ?&gt;</td>
</tr>
</tbody>
</table>
 
 
</form>
  • Tags:

Comment