2

我想为我的组件添加分页,我创建了一个带有查询的简单模型。我肯定错过了什么。我在这里还需要什么?

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;
}

编辑:

我编辑了一些代码,所以现在它几乎可以正常工作了。按钮显示(要显示的行数)不起作用。我想知道这部分是否可以以不同的方式完成?

    $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);
    }
}
4

2 回答 2

1

离开你的原始代码,而不是编辑后的版本。

getListQuery 方法仅构建您的数据库查询,因此您无需在此处执行查询。使用 com_weblinks 作为构建模型的示例:https ://github.com/joomla/joomla-cms/blob/2.5.x/components/com_weblinks/models/category.php

于 2013-03-24T21:37:54.587 回答
0

正确点击此链接http://docs.joomla.org/J1.5:Using_JPagination_in_your_component。我使用了 Joomla 分页。如果您正确遵循文档,您将能够轻松使用 Joomla 分页。顺便说一句,它非常简单。

于 2013-07-22T11:37:32.040 回答