1

I'm developing a custom component that displays data from an external API, and in trying to add filtering options to the request, only the limit field will display. The additional fields I'm using are only text and list, so I'm not sure why they wouldn't be displayed also.

As I understand it the files that need updating for filtering are:

models/forms/filter_customers.xml

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="filter">
        <field
            name="username"
            type="text"
            label="username"
            hint="JSEARCH_FILTER"
        />
        <field
            name="company"
            type="text"
            label="company"
            hint="JSEARCH_FILTER"
        />
        <field
            name="status"
            type="list"
            label="COM_SUPERSITE_FILTER_STATUS"
            description="COM_SUPERSITE_FILTER_STATUS_DESC"
            onchange="this.form.submit();"
            >
            <option value="Active">COM_SUPERSITE_FILTER_STATUS_ACTIVE</option>
            <option value="Suspended">COM_SUPERSITE_FILTER_STATUS_SUSPENDED</option>
            <option value="Deleted">COM_SUPERSITE_FILTER_STATUS_DELETED</option>
            <option value="">COM_SUPERSITE_FILTER_STATUS_ALL</option>
        </field>
    </fields>
    <fields name="list">
        <field
            name="limit"
            type="limitbox"
            class="input-mini"
            default="25"
            label="COM_CONTENT_LIST_LIMIT"
            description="COM_CONTENT_LIST_LIMIT_DESC"
            onchange="this.form.submit();"
        />
    </fields>
</form>

models/customers.php

class SupersiteModelCustomers extends JModelList
{

   public function __construct($config = array())
    {
        if (empty($config['filter_fields']))
        {
            $config['filter_fields'] = array(
                    'username',
                    'name',
                    'company',
                    'status'
            );
        }
        JLoader::register('SupersiteHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/supersite.php');

        parent::__construct($config);
    }

    protected function populateState($ordering = null, $direction = null)
    {
        // Initialise variables.
        $app = JFactory::getApplication('administrator');

        $username = $app->getUserStateFromRequest($this->context . 'filter.username', 'filter_username', '', 'string');
        $this->setState('filter.username', $username);

        $name = $app->getUserStateFromRequest($this->context . 'filter.name', 'filter_name', '', 'string');
        $this->setState('filter.name', $name);

        $company = $app->getUserStateFromRequest($this->context . 'filter.company', 'filter_company', '', 'string');
        $this->setState('filter.company', $company);

        $status = $app->getUserStateFromRequest($this->context . 'filter.status', 'filter_status', '', 'string');
        $this->setState('filter.status', $status);

        // List state information.
        parent::populateState();
    }

    public function getItems() 
    {
        $store = $this->getStoreId();

        $username   =   $this->getState('filter.username');
        $name       =   $this->getState('filter.name');
        $company    =   $this->getState('filter.company');
        $status     =   $this->getState('filter.status');

    // Filter search
    if (!empty($username)) {
        SupersiteHelper::add('username', $username);
    }
    if (!empty($name)) {
        SupersiteHelper::add('name', $name);
    }
    if (!empty($company)) {
        SupersiteHelper::add('company', $company);
    }
    if (!empty($status)) {
        SupersiteHelper::add('status', $status);
    }

        if (!isset($this->cache[$store])) {
            $limit = $this->getState('list.limit');
            $start = $this->getStart();
            $start = ($start/$limit)+1;

            SupersiteHelper::add('no-of-records', $limit);
            SupersiteHelper::add('page-no', $start);

            $data = SupersiteHelper::getData('customers/search.json');
            $this->cache[$store] = $data;
        }
        return $this->cache[$store];
    }

    public function getTotal() {
        $store = $this->getStoreId('getTotal');

        if (!isset($this->cache[$store])) {
            SupersiteHelper::add('no-of-records', 1);
            SupersiteHelper::add('page-no', 1);
            $data = SupersiteHelper::getData('customers/search.json');
            $this->cache[$store] = $data["recsindb"];
        }

        return $this->cache[$store];
    }

}

views/customers/view.html.php

class SupersiteViewCustomers extends JViewLegacy
{

    protected $items;
    protected $pagination;
    protected $state;


public $activeFilters = array();

    public function display($tpl = null) {

        $this->pagination       = $this->get('Pagination');
        $this->items            = $this->get('Items');
        $this->state            = $this->get('State');
        $this->filterForm       = $this->get('FilterForm');
        $this->activeFilters    = $this->get('ActiveFilters');

        SupersiteHelper::addSubmenu('customers');

        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }
        $this->addToolbar();
        $this->sidebar = JHtmlSidebar::render();

        parent::display($tpl);
    }

}

views/customers/tmpl/default.php

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

Nevermind, I figured it out. The fields ARE actually displaying, but Joomla's JQuery auto hides them with css:

.js-stools-container-filters {
    display: none;
    margin: 10px 0;
}

Also, it turns out it's required to have a "search" field in order for the submit and clear buttons to show up.

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.