Hi
This is something I think many would find useful, so I thought I'd share my solution. I needed a solution to filter my list of items without having the user involved - eg. to select a category to show items from.
So beyond the "obvious" of creating the relations between the tables, and adding the category to a filter in the list view, I had to do some custom coding. I'm not really a very skilled coder, so my solution my not be the best, but it works.
I needed to filter on 3 tables, so my code below is based on that - you would need to modify the code to your component
First:
in my default.xml file for the view in question I added this after layout:
<!-- Add fields to the request variables for the layout. -->
<fields name="params">
<fieldset name="request">
<field name="category"
type="text"
description="Category"
label="Category"
size="5"
/>
<field name="network_carrier"
type="text"
description="Carrier"
label="Carrier"
size="5"
/>
<field name="brand_model"
type="text"
description="Model"
label="Model"
size="5"
/>
</fieldset>
</fields>
in components/com_[componentname]/models/[viewname].php I modified the WHERE - FILER:
// Get filter fra menu params
$app = JFactory::getApplication();
$params = $app->getParams();
//WHERE - FILTER : Network carrier
if($this->getState('filter.network_carrier')) {
$query->where('a.network_carrier = '. (int)$this->getState('filter.network_carrier'));
} elseif($params->get('network_carrier')) {
$query->where('a.network_carrier = '. (int)$params->get('network_carrier'));
}
//WHERE - FILTER : Brand Model
if($this->getState('filter.brand_model')) {
$query->where('a.brand_model = '. (int)$this->getState('filter.brand_model'));
} elseif($params->get('brand_model')) {
$query->where('a.brand_model = '. (int)$params->get('brand_model'));
}
//WHERE - FILTER : Category
if($this->getState('filter.category')) {
$query->where('a.category = '. (int)$this->getState('filter.category'));
} elseif($params->get('category')) {
$query->where('a.category = '. (int)$params->get('category'));
}
in components/com_[componentname]/views/[viewname].php I modified the code after //Filters:
// Get filter fra menu params
$app = JFactory::getApplication();
$params = $app->getParams();
//Network carrier > Title
$model_network_carrier = JModel::getInstance('networkcarriers', 'ComparemobileModel');
$this->filters['network_carrier'] = new stdClass();
$this->filters['network_carrier']->list = $model_network_carrier->getItems();
if ($model->getState("filter.network_carrier")){
$this->filters['network_carrier']->value = $model->getState("filter.network_carrier");
} else {
$this->filters['network_carrier']->value = $params->get('network_carrier');
}
//Brand Model > Title
$model_brand_model = JModel::getInstance('brandmodels', 'ComparemobileModel');
$model_brand_model->addJoin("LEFT JOIN #__comparemobile_brands AS _brand_ ON _brand_.id = a.brand");
$model_brand_model->addSelect("_brand_.title AS _brand_title");
$model_brand_model->addGroupBy("_brand_.title");
$this->filters['brand_model'] = new stdClass();
$this->filters['brand_model']->list = $model_brand_model->getItems();
if ($model->getState("filter.brand_model")){
$this->filters['brand_model']->value = $model->getState("filter.brand_model");
} else {
$this->filters['brand_model']->value = $params->get('brand_model');
}
//Category > Title
$model_category = JModel::getInstance('categories', 'ComparemobileModel');
$this->filters['category'] = new stdClass();
$this->filters['category']->list = $model_category->getItems();
$this->filters['category']->list = $model_category->getItems();
if ($model->getState("filter.category")){
$this->filters['category']->value = $model->getState("filter.category");
} else {
$this->filters['category']->value = $params->get('category');
}
last I modified default_filters.php template for the view:
<?php
defined('_JEXEC') or die('Restricted access');
// Get filter fra menu params
$app = JFactory::getApplication();
$params = $app->getParams();
$category = $params->get('category');
$brand_model = $params->get('brand_model');
$network_carrier = $params->get('network_carrier');
?>
<?php if(!$category || !$brand_model || !$network_carrier) { ?>
<script language="javascript" type="text/javascript">
<!--
function resetFilters()
{
if (typeof(jQuery) != 'undefined')
{
jQuery('.filters :input').val('');
/* TODO : Uncomment this if you want that the reset action proccess also on sorting values
jQuery('#filter_order').val('');
jQuery('#filter_orderDir').val('');
*/
document.adminForm.submit();
return;
}
//Deprecated
if ($('filter_network_carrier') != null)
$('filter_network_carrier').value='';
if ($('filter_brand_model') != null)
$('filter_brand_model').value='';
if ($('filter_category') != null)
$('filter_category').value='';
/* TODO : Uncomment this if you want that the reset action proccess also on sorting values
if ($('filter_order') != null)
$('filter_order').value='';
if ($('filter_orderDir') != null)
$('filter_orderDir').value='';
*/
document.adminForm.submit();
}
-->
</script>
<fieldset id="filters" class="filters">
<legend><?php echo JText::_( "JSEARCH_FILTER_LABEL" ); ?></legend>
<div style="float:right;">
<div style="float:left">
<div class="filter filter_buttons">
<button onclick="this.form.submit();"><?php echo(JText::_("JSEARCH_FILTER_SUBMIT")); ?></button>
<button onclick="resetFilters()"><?php echo(JText::_("JSEARCH_FILTER_CLEAR")); ?></button>
</div>
</div>
</div>
<div>
<?php if (!$category) { ?>
<div style="float:left">
<!-- SELECT : Category -->
<div class='filter filter_category'>
<?php echo JDom::_('html.form.input.select', array(
'dataKey' => 'filter_category',
'dataValue' => $this->filters['category']->value,
'list' => $this->filters['category']->list,
'labelKey' => 'title',
'nullLabel' => "COMPAREMOBILE_FILTER_NULL_CARRIER",
'submitEventName' => 'onchange'
));
?>
</div>
</div>
<?php } ?>
<?php if (!$network_carrier) { ?>
<div style="float:left">
<!-- SELECT : Network carrier > Title -->
<div class='filter filter_network_carrier'>
<?php echo JDom::_('html.form.input.select', array(
'dataKey' => 'filter_network_carrier',
'dataValue' => $this->filters['network_carrier']->value,
'list' => $this->filters['network_carrier']->list,
'labelKey' => 'title',
'nullLabel' => "COMPAREMOBILE_FILTER_NULL_CARRIER",
'submitEventName' => 'onchange'
));
?>
</div>
</div>
<?php } ?>
<?php if (!$brand_model) { ?>
<div style="float:left">
<!-- SELECT : Brand Model > Title -->
<div class='filter filter_brand_model'>
<?php echo JDom::_('html.form.input.select', array(
'dataKey' => 'filter_brand_model',
'dataValue' => $this->filters['brand_model']->value,
'list' => $this->filters['brand_model']->list,
'labelKey' => 'title',
'nullLabel' => "COMPAREMOBILE_FILTER_NULL_MODEL",
'submitEventName' => 'onchange',
'groupBy' => array(
'brand' => '_brand_title'
)
));
?>
</div>
</div>
<?php } ?>
</div>
<div clear='all'></div>
</fieldset>
<?php } ?>
So now, my filters will only show up if the filter is not pre filled in menu and if all filters is pre filled, the filters option will not show at all in my view.
I hope this helps some of you creating better components.
Cheers...