I created a filter for the backend of my component like this:
<field name = "category_id"
type = "category"
label = "JOPTION_FILTER_CATEGORY"
extension = "com_bestia.items"
description = "JOPTION_FILTER_CATEGORY_DESC"
onchange = "this.form.submit();" >
<option value = "">JOPTION_SELECT_CATEGORY</option>
</field>
I added the filter to my view by adding this to my view.html.php
$this->filterForm = $this->get('FilterForm');
$this->activeFilters= $this->get('ActiveFilters');
and this to my layout:
echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));
Well, this works great, but if I change the state of the item by clicking on the little button next to each entry, which is defined like this:
<td class="small">
<?php echo JHtml::_('jgrid.published', $item->state, $i, 'items.', $canChange, 'cb'); ?>
</td>
the filter is not working anymore. It is still checked but the view shows results of all components.
Any ideas how to fix that?
Edit: This is the contractor of the model:
public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'id', 'a.id',
'title', 'a.title',
'catid', 'nc.catid', 'category_title',
'state', 'a.state',
'created_by', 'a.created_by'
);
}
parent::__construct($config);
}
This is the populateState-method of the model:
/**
* populateState function.
*
* @access protected
* @param mixed $ordering (default: null)
* @param mixed $direction (default: null)
* @return void
*/
protected function populateState($ordering = null, $direction = null)
{
// Initialise variables.
$app = JFactory::getApplication('administrator');
// Load the filter state.
$search = $app->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
$this->setState('filter.search', $search);
$published = $app->getUserStateFromRequest($this->context . '.filter.state', 'filter_published', '', 'string');
$this->setState('filter.state', $published);
$categoryId = $this->getUserStateFromRequest($this->context . '.filter.catid', 'filter_category_id', '');
$this->setState('filter.catid', $categoryId);
// Load the parameters.
$params = JComponentHelper::getParams('com_bestia');
$this->setState('params', $params);
// List state information.
parent::populateState('a.id', 'asc');
}
Another interesting fact: In the getListQuery I'm getting the catid by using
// Filter by category.
$categoryId = $this->getState('filter.catid');
this has a value if I filter by category - but is has no value if I click on one of the publishing-buttons you can see on the screenshot: