How to create a component's dynamic filter in the Joomla Backend
I develop a component in the Joomla 1.5 framework and I was looking for a good explanation on how to create a dynamic filter in the MVC style. There are tutorials on how to create a static filter but not for dynamic filters. The difficulty is on the MVC style which is where to put which part of the code. That's why I wrote this small tutorial and I refer to which file the code goes.
Let's say that we have mycomponent which stores items we buy. These must be registered under a type so we must also register types of items On the item's panel we will see all the items and we want to have a filter with the types Each time we choose a type, the items will be filtered (suppose we can't delete types). Items and types may not seem familiar but they are similar to my component.
I didn't create a big tutorial on generally how to create a filter because such tutorials already exist. You probably already have such static filters and you are a little confused on the MVC style. Well we will alike!
First of all I must mention the tree of the files we will use under the ~/administrator/components/com_mycomponent folder.
~/models~/models/mycomponent.php~/models/....
~/views~/views/mycomponent/view.html.php~/views/mycomponent/tmpl/default.php~/views/.....
~/.....(other files like mycomponent.php etc)
First of all we must add the variable for the categories list. So we open the
~/models/mycomponent.php
and inside the class that extends the JModel we add
Usually we have the following function
return $query;}
Some of you may already have a function named _buildQueryOrderBy() which is added after the $this->_buildQueryWhere()
Now we must see how the _buildQueryWhere must be
global $mainframe;$where = array();$db =& JFactory::getDBO();
// get the types filter value$types = $mainframe->getUserStateFromRequest('com_mycomponent.filtertypes', 'filter_types', '', 'int');
// check if we are filtering based on the existing categoriesif ($types!='') {$this->getTypes();foreach ($this->_types as $typesrow) {
if ($types == $typesrow->type) {$where[] = $db->nameQuote('type') . ' = ' .$db->Quote($typesrow->type);}}}
if (count($where)) {// building from array$where = ' WHERE '. implode(' AND ', $where);} else {$where = '';}return $where;}
As you saw we used a function named getTypes. We are still in the class that extends the JModel and now we add the function getTypes
function getTypes(){if (empty( $this->_types )){$db =& JFactory::getDBO();$table = $db->nameQuote('#__mycomponentitems');$typesquery = 'SELECT DISTINCT type ' . "FROM $table ";$this->_types = $this->_getList($typesquery);}return $this->_types;}
This function retrieves the types of the items. As you notice we used the SELECT DISTINCT command which will not retrieve duplicate values.
We created the types list in the JModel's class. Now we must add some code the in the class that extends the JView.
First we open the file
~/views/mycomponent/view.html.php
Inside the display function we add
$types =& $this->get( 'Types');
$options[] = JHTML::_('select.option', '', JText::_('SELECT Question Type'));
foreach ($types as $typesrow) { $options[]= JHTML::_('select.option', $typesrow->type, JText::_('Question Type '.$typesrow->type)); } // You can add the next line for checking. It has value 999 which was not used in the table and you will see that it is not used. You can keep it if you don't have a reset filters button
// $options[]= JHTML::_('select.option', 999, JText::_('Question BAD Type '));
$lists['types'] = JHTML::_('select.genericlist', $options, 'filter_types', 'class="inputbox" onchange="submitform();"', 'value', 'text', $filtertypes);
$this->assignRef('lists', $lists);
....(there may be code here) parent::display($tpl);
And inside the
~/views/mycomponent/tmpl/default.php (HTML part of the view)
inside the adminform form we add
The code I described creates how to create a dynamic list with the types/categories etc that are already used in the database, how to validate the posted value for security reasons and filter the data.
Now if you are still confused with MVC I must say
~/models/mycomponent.php We added the types' list variable and the functions that retrieve the existing types and manipulate the basic query.
~/views/mycomponent/view.html.php We added the J! code that creates the filter's list and handles the form's values
~/views/mycomponent/tmpl/default.php We added the HTML code that shows the filter.
If you have any comments please share with the rest of us.
The Academic Site of Velonis I. Petros


Comments
"This function retrieves the types of the items. As you notice we used the SELECT DISTINCT command which will not retrieve duplicate values."
I could say getCategories or getSomething from another table that the items use.
Daniel
B4Info
Best wishes from Mark Spenser
Funniest Movie Quotes
Online marketing solutions
iPhone Sprint
regards
This is what i need.
Thank you very much
rgds
Online marketing solutions
skin tags at home
Cool post mate.. thanks for the information