I try to get some help from you to spot what I am doing wrong (what I am missing) on applying search tools and filters in the admin of Joomla 3.8 in my own component.
I am pulling my hairs out since I cannot figure out why the search and the filters are not working for me, in other words, the search field is not responding, the search tools/hidden filter does not open.
Only if I click on the clear filters/search button then it refreshes the page with cleared filters. Sorting of fields/columns are working. My only guess is that it is a Jquery/Javascript problem but I do not really know what. Please help me if you can. Thanks in advance!
We are in the view/model called 'products', so my filters' xml:
../models/forms/filter_products.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="filter">
<field
name="search"
type="text"
label="COM_MOREPRODUCTINFO_SEARCH_IN_TITLE"
hint="JSEARCH_FILTER"
class="JSEARCH_FILTER"
/>
<field
name="availability"
type="list"
label="JOPTION_SELECT_AVAILABLE"
description="JOPTION_SELECT_AVAILABLE_DESC"
default=""
onchange="this.form.submit();"
>
<option value="">JOPTION_SELECT_VALUE</option>
<option value="1">Available</option>
<option value="0">Unavailable</option>
</field>
</fields>
<fields name="list">
<field
name="fullordering"
type="list"
label="COM_MOREPRODUCTINFO_LIST_FULL_ORDERING"
description="COM_MOREPRODUCTINFO_LIST_FULL_ORDERING_DESC"
onchange="this.form.submit();"
default="item_id ASC"
>
<option value="">JGLOBAL_SORT_BY</option>
<option value="a.item_id ASC">COM_MOREPRODUCTINFO_ID_ASC</option>
<option value="a.item_id DESC">COM_MOREPRODUCTINFO_ID_DESC</option>
<option value="a.product_name ASC">COM_MOREPRODUCTINFO_PNAME_ASC</option>
<option value="a.product_name DESC">COM_MOREPRODUCTINFO_PNAME_DESC</option>
<option value="a.availability ASC">COM_MOREPRODUCTINFO_AVAILABILITY_ASC</option>
<option value="a.availability DESC">COM_MOREPRODUCTINFO_AVAILABILITY_DESC</option>
</field>
<field
name="limit"
type="limitbox"
class="input-mini"
default="20"
label="COM_CONTENT_LIST_LIMIT"
description="COM_MOREPRODUCTINFO_LIST_LIMIT_DESC"
onchange="this.form.submit();"
/>
</fields>
</form>
../models/products.php
class MoreproductinfoModelProducts extends ListModel
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'item_id', 'a.item_id',
'product_name', 'a.product_name',
'product_id', 'a.product_id',
'variant_id', 'a.variant_id',
'product_sku', 'a.product_sku',
'avalability', 'a.availability',
);
}
JLoader::register('MoreproductinfoHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/moreproductinfo.php');
parent::__construct($config);
}
protected function populateState($ordering = 'a.item_id', $direction = 'asc')
{
// Initialise variables.
$app = JFactory::getApplication();
$search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
$this->setState('filter.search', $search);
$availability = $this->getUserStateFromRequest($this->context . '.filter.availability', 'filter_availability', '', 'string');
$this->setState('filter.availability', $availability);
// List state information.
parent::populateState($ordering, $direction);
}
function getListQuery()
{
// Initialize variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$user = JFactory::getUser();
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.item_id' .
', a.product_name, a.product_id, a.variant_id, a.variation_option_name, a.variation_image_param_url' .
', a.product_sku, a.availability, a.arrival_date, a.params, a.last_check')
);
$query->from('#__moreproductinfo_items AS a');
// Filter: product_name / search
$search = $this->getState('filter.search');
if (!empty($search))
{
$search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
// $like = $db->quote('%' . $search . '%');
$query->where('a.product_name LIKE ' . $search);
}
// Filter by availability state
$available = $this->getState('filter.availability');
// $available = 0; -- debug - filter works if I define value here
if (is_numeric($available))
{
$query->where('availability = ' . (int) $available);
}
elseif ($available === '')
{
$query->where('(availability IN (0, 1))');
}
// Add the list ordering clause.
$orderCol = $this->getState('list.ordering', 'item_id');
$orderDirn = $this->getState('list.direction', 'asc');
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
return $query;
}
function getItems()
{
$items = parent::getItems();
foreach ($items as $item) {
$item->url = JRoute::_('index.php?option=com_moreproductinfo&task=product.edit&item_id=' . $item->item_id);
$item->img_url = JRoute::_(URI::root() . $item->variation_image_param_url);
}
return $items;
}
Then the view.html file: ../views/products/view.html.php
public function display($tpl = null)
{
// to call nessecary JS
HTMLHelper::_('behavior.framework');
// define state and pagination - from model
// define items from model
$this->items = $this->get('Items');
$this->state = $this->get('State');
$this->pagination = $this->get('Pagination');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
// Show the toolbar - set the toolbar
$this->toolbar();
// adding toolbars
$this->addToolbar();
// Show the sidebar
$this->helper = new MoreproductinfoHelper;
$this->helper->addSubmenu('products');
$this->sidebar = JHtmlSidebar::render();
// Display it all
return parent::display($tpl);
}
and finally the tmpl default layout: ../views/products/tmpl/default.php
defined('_JEXEC') or die;
use \Joomla\CMS\language\Text;
use \Joomla\CMS\HTML\HTMLHelper;
use \Joomla\CMS\Layout\LayoutHelper;
HTMLHelper::_('behavior.core');
HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('behavior.multiselect');
HTMLHelper::_('formbehavior.chosen', 'select');
$app = JFactory::getApplication();
$user = JFactory::getUser();
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
?>
<div id="j-sidebar-container" class="span2">
<?php echo $this->sidebar; ?>
</div>
<div id="j-main-container" class="span10">
<div class="alert alert-dark" role="alert"><p><?php echo Text::_('COM_MOREPRODUCTINFO_PRODUCTS_LAST_REFRESHED') . $this->date; ?></p></div>
<div class="row-fluid">
<div class="span10">
<?php echo JText::_('COM_MOREPRODUCTINFO_PRODUCTS_FILTER'); ?>
<?php
echo LayoutHelper::render(
'joomla.searchtools.default',
array('view' => $this)
);
?>
</div>
</div>
<form action="<?php echo JRoute::_('index.php?option=com_moreproductinfo&view=products'); ?>" method="post" name="adminForm" id="adminForm">
<table class="table table-striped table-hover">
<thead>
<tr>...
... // a lot of html and php which shows the items....
</table>
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo HTMLHelper::_('form.token'); ?>
</form>
</div>