Using searchtools in component front end problems

254 views
Skip to first unread message

Roger at Gmail

unread,
Jul 27, 2020, 6:15:53 AM7/27/20
to Joomla! General Development
Hi all,
In my component front-end I have a list view and I want to include the joomla.searchtools to allow a visitor to search and filter the list

in the view default.php:
echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));

in view.html.php display():
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->state = $this->get('State');
$this->params = $this->state->get('params');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');

in form filter_viewname.xml defining the search and filter inputs with each field having an action:
onchange="this.form.submit();"

in the model appropriate code in getListQuery() to apply any applicable filters:
$pfilt = $this->getState('filter.perfilt');
if (is_numeric($pfilt)) {
$query->where('p.person_id = '.$pfilt);
}
etc etc

This all the same as in a backend list view and works but I have two problems:

1. When you perform a filter action on the front end the filter tools bar gets closed as the view refreshes and not reopened by the state activeFilter.
In the backend if a filter is applied the form stays open so you can see what the filter is. In the frontend it doesn’t.

This seems to be controlled by the $this->activeFilters . In the backend this is an array that is automatically populated with all the filter fields that have a value, in the front end it remains an empty array.

activeFilters is used in the layout file layouts/joomla.searchtools to determine if the filter form should be shown. It is set by /libraries/src/MVC/ListModel.php getActiveFilters() as far as I can see, but this doesn’t seem to be called from the front end.

How can I make this work the same as in the backend.

2. This is a more trivial annoyance. SearchTools doesn’t use the labels and descriptions for the form elements. (except in the case of radio buttons if you shown them plain rather than using the btn-group class to make them look good). I can fix this by providing an override for the layout in the file /components/mycomponent/layouts/joomla/searchtools/default/filters.php but is there a simpler way?

RogerCO

Glenn Arkell

unread,
Jul 27, 2020, 6:31:43 AM7/27/20
to Joomla! General Development
Hi Roger,

I have this in the populateState method within the model. . .

        $context = $this->getUserStateFromRequest($this->context.'.filter.perfilt', 'filter_perfilt');
        $this
->setState('filter.perfilt', $context);

There may be some more required.  Hope this helps.  Cheers.
Glenn

Viper

unread,
Jul 27, 2020, 6:32:57 AM7/27/20
to Joomla! General Development
As I remember variables from seachrtools stored in session. Try to add $context into model.

protected $context = 'com_mycomponentname.modelname';

And change context in populateState() method. See Glenn's answer.

Roger at Gmail

unread,
Jul 27, 2020, 6:41:10 AM7/27/20
to joomla-de...@googlegroups.com
Thanks both Viper and Glenn. I’ll try that. The odd thing is that in the backend it all works without setting $context or even having a populateState() method in the model, so I guess those things are being handled by the system in administrator but not in site (which I find confusing)

RogerCO

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/4a2445d5-72e4-4e7f-ae3a-662071b023f1o%40googlegroups.com.

Roger Creagh

unread,
Jul 27, 2020, 2:23:53 PM7/27/20
to joomla-de...@googlegroups.com
Nope, that's not it, thanks Glenn & Viper

In fact it seems that in J3.9 in both front-end and backend it is not necessary to have a populateState method in the listmodel - it is all done for you.

I think you'll find that if you delete your populateState method (or the bits of it to do with search and filtering and pagination) and also the associated protected variables it will all still work by the magic of Joomla.

The problem appears to be a difference between the front and back end handling of activeFilters which is used by the layout to check whether there is an active filter present and if it is to add the visible class to the div that wraps the filter layout - possibly.

I find the logic is confusing in the file layouts/joomla/searchtools/default.php which seems to use activeFilters in combination with some options to decide whether to display the filter bar our not.

The strange thing is if you simply override the code at line 98 in that file to make it so that div around the filter bar always has the "js-stools-container-filters-visible" class assigned which makes it visible then something else is adding an explict "style="display:none;" to the div after the form is rendered.

Most odd, it doesn't seem to happen in the backend - it must be inserted by a bit of javascript somewhere.

I can't find any example of a front end view that uses the searchtools filter bar and keeps it hidden until you press the search button if there is no filter set, but always displays it if there is a filter set.

Suggestions anyone?

RogerCO
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Roger Creagh

unread,
Jul 27, 2020, 4:39:43 PM7/27/20
to joomla-de...@googlegroups.com
DOH!
My bad
Ignore previous messages, it turns out I had failed to add the filter
fields to the model __construct() function (I thought I had done that,
but turned out not in the version I was testing with, just in the dev
copy)

if (empty($config['filterfileds'])) {
$config['filter_fields'] = array ('id', 'title',
'catid', 'category_title',
'category_id','subcats','perfilt','prole',
'tagfilt','taglogic' );
}

Schoolboy error :-( sorry about that.

But it is true that you no longer need to have populateState() in your
model just to handle the search, filter and pagination stuff.

RogerCO

Reply all
Reply to author
Forward
0 new messages