8

I want to create a view with filters and pagination for Joomla 3.x, but I'm not sure about what I have to include and where.

For now, my model extends JModelList and I started using the getListQuery() method to fetch the data:

<?php
defined('_JEXEC') or die;

class smartModelProducts extends JModelList{    

    protected function getListQuery(){
        // Initialize variables.
        $db    = JFactory::getDbo();
        $query = $db->getQuery(true);

        // Create the base select statement.
        $query->select('*')
        ->from($db->quoteName('#__smart_products'));

        return $query;
    }

}

My view.html.php looks like this:

<?php
defined('_JEXEC') or die;

class smartViewProducts extends JViewLegacy{

    function display($tpl=null){
        $app=JFactory::getApplication();
        $jinput = $app->input;
        $option = $jinput->get('option', null, null);
        $user=JFactory::getUser();

        // Get data from the model
        $this->state = $this->get('State');
        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');

        parent::display($tpl);      
    }
}

What do I have to add to my model and my view? What is it that I have to include in my default.php so that both filters and pagination work?

8

Follow the below steps:

Filters:

1) Make sure to add all your filterable fields in your model constructor

public function __construct ($config = array())
{
    if (empty($config['filter_fields']))
    {
        $config['filter_fields'] = array(
           'id', 'a.id',
           'catid', 'a.catid',
           ....
           ....
        );
    }

    parent::__construct($config);
}

2) Populate your filter values in your model (products.php) for use like below

protected function populateState ($ordering = null, $direction = null)
{
    $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
    $this->setState('filter.search', $search);

    $authorId = $app->getUserStateFromRequest($this->context . '.filter.author_id', 'filter_author_id');
    $this->setState('filter.author_id', $authorId);

    $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '');
    $this->setState('filter.published', $published);

    $categoryId = $this->getUserStateFromRequest($this->context . '.filter.category_id', 'filter_category_id');
    $this->setState('filter.category_id', $categoryId);

    // and so on .....
}

3) Add your filter xml file with required filter fields defined in models/forms/filter_products.xml

See administrator/components/com_content/models/forms/filter_articles.xml

4) Get and set filters in your view.html.php

$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');

5) Display filters in list view views/products/tmpl/default.php

<?php echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));?>

Pagination:

1) Get pagination from model in your view.html.php

$this->pagination = $this->get('Pagination');

2) Display in your views/products/tmpl/default.php

<?php echo $this->pagination->getListFooter(); ?>

Notes:

  1. I suggest you to refer Joomla Articles (com_content) component for any Joomla MVC component development. It is the best resource/documentation available to-date.

  2. The above codes are meant for admin component. For front-end component, most of the steps are more or less same but you need to tweak them as per your requirement.

| improve this answer | |
  • I don't know how to create the xml for the filters. Could you please point me in the right direction? I thought it was possible to add them it in the model directly. – mattosmat Jun 7 '15 at 16:30
  • The code for displaying the filters and pagination must go within a form, right? – mattosmat Jun 7 '15 at 16:31
  • updated my answer – Nagarjun Jun 7 '15 at 16:31
  • For pagination you don't need any xml file. JModelList defines required functions (getPagination), so it is readily available in your model. – Nagarjun Jun 7 '15 at 16:37
  • What do I need the xml for? I want to know so that I can find out how to make one. – mattosmat Jun 7 '15 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.