Joomla 2.5 pagination

2019-07-16 01:59发布

问题:

I want to add pagination to my component, i've created a simple model with query. I must be missing something. What else do I need here ?

MODEL
jimport('joomla.application.component.modellist');
class PaieskaModelPradinis extends JModelList
{
    public function getListQuery() 
    {
    $db = JFactory::getDBO();
    $query = "SELECT * FROM #__content";
    $db->setQuery( $query );
    $db->query( $query );
    $result = $db->LoadObjectList();
    return $result;
    }
}

VIEW

jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
    protected $items;
    protected $pagination;
    function display ($tpl = null)
    {
        $this->items = $this->get('ListQuery');
        $this->pagination = $this->get('Pagination');

        parent::display($tpl);
    }
}
TPL
foreach ($this->items as $item) {
    echo $item->title;
}

EDITED:

I edited a bit code, so now it works fine, almost. Button display(number of rows to display) is not working. And I wonder if this part can be done in a different way ?

    $limit      = JRequest::getVar('limit' , 25);
    $start      = JRequest::getVar('start' , 0);        
    $query      = "SELECT * FROM #__content LIMIT $start, $limit";

-

class PaieskaModelPradinis extends JModelList
{
    public function getItems() 
    {
        $db         = JFactory::getDBO();
        $limit      = JRequest::getVar('limit' , 25);
        $start      = JRequest::getVar('start' , 0);        
        $query      = "SELECT * FROM #__content LIMIT $start, $limit";
        $db->setQuery( $query );
        $db->query( $query );
        $lists = $db->LoadObjectList();

        return $lists;
    }
    function getPagination()
    {
        $main = JFactory::getApplication();
        $db         = JFactory::getDBO();
        $limit      = JRequest::getVar('limit' , 25);
        $limitstart = JRequest::getVar('limitstart', 0);

        $query      = "SELECT count(title) FROM #__content";
        $db->setQuery( $query );
        $total      = $db->loadResult();

                // include a pagination library

        jimport('joomla.html.pagination');
        $pagination = new JPagination($total, $limitstart, $limit);

        return $pagination;
    }
}

VIEW

jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
    function display($tpl = null)
    {
        $this->items  = $this->get('items');
        $this->pagination = $this->get('pagination');

        parent::display($tpl);
    }
}

回答1:

Going off your original code, not the edited version.

The getListQuery method only builds your database query, so you don't execute your query here. Use com_weblinks as an example for building out your model: https://github.com/joomla/joomla-cms/blob/2.5.x/components/com_weblinks/models/category.php



回答2:

follow this link http://docs.joomla.org/J1.5:Using_JPagination_in_your_component properly. I have used Joomla pagination. You'll be able to use Joomla pagination easily, if you follow documentation properly. BTW its very simple.