Forums

Dynamic dropdown filter

FloB 17 Nov, 2015
Hello,

I'm trying to get a listing of the articles with a dropdown to filter them by categorie.
The listing is working except the filtering.

In Models, I have the principal model named 'articles' for the table '#__content' and conditions :
<?php
return array("articles.access"=>1);
?>
.

In Front list -> List Display -> table -> header code I have this code :
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('a.id','a.title'))
    ->from($db->quoteName('#__categories','a'))
    ->where($db->quoteName('a.parent_id').'=92');
$db->setQuery($query);
$categories=$db->loadAssocList();
$k=array_map(function($element){return $element['id'];},$categories);
$v=array_map(function($element){return $element['title'];},$categories);
$options=array_combine($k,$v);
$field = array (
  'name' => 'fltr[articles][catid]',  'id' => '',   'options' =>   array (  ),
  'empty' => '',  'values' =>   array (  ),  'label' =>   array (    'text' => 'Activité', 'position' => 'left',  ),
  'sublabel' => '',  'multiple' => '0',  'size' => '',  'class' => '',  'title' => '',  'style' => '',  'params' => '',  ':data-load-state' => '',
  ':data-tooltip' => '',  'type' => 'dropdown',  'container_id' => '0',);
$field["options"] = $options;
echo \GCore\Helpers\Html::formLine($field["name"], $field);
?>
<div class="form-group gcore-form-row" id="form-row-1"><div class="gcore-input gcore-display-table" id="fin-button">
<input name="button" id="button" type="submit" value="Valider" class="form-control A" style="" data-load-state="" /></div></div>


In Front List -> Settings I have 'articles.catid' in filters.

When I click on the button, the listing refresh but the data is not filtered.

Something is certainly missing, but I'm not a pro in CC neither in php.
Can someone help me please?
GreyHead 18 Nov, 2015
Hi FloB,

If you turn on the CC Debugger you should be able to see the query that is being created - usually that gives some clues about where the problem is.

Bob
FloB 18 Nov, 2015
Hi Bob,

Here the content of the debugger :
Array
(
    [0] => SELECT `Extension`.`id` AS `Extension.id`, `Extension`.`name` AS `Extension.name` FROM `jom343_chronoengine_extensions` AS `Extension` WHERE `Extension`.`enabled` = '1' ORDER BY `Extension`.`ordering` ASC
    [1] => SELECT `Connection`.`id` AS `Connection.id`, `Connection`.`title` AS `Connection.title`, `Connection`.`params` AS `Connection.params`, `Connection`.`extras` AS `Connection.extras`, `Connection`.`published` AS `Connection.published` FROM `jom343_chronoengine_connections` AS `Connection` WHERE `Connection`.`title` = 'ListeComptesRendusFilter' AND `Connection`.`published` = '1'
    [2] => SELECT COUNT(`articles`.`id`) AS `articles.count` FROM `jom343_content` AS `articles` LEFT JOIN `jom343_categories` AS `categories` ON `articles`.`catid` = `categories`.`id` WHERE `articles`.`access` = '1' AND `categories`.`parent_id` = '92'
    [3] => SELECT `articles`.`id` AS `articles.id`, `articles`.`asset_id` AS `articles.asset_id`, `articles`.`title` AS `articles.title`, `articles`.`alias` AS `articles.alias`, `articles`.`introtext` AS `articles.introtext`, `articles`.`fulltext` AS `articles.fulltext`, `articles`.`state` AS `articles.state`, `articles`.`catid` AS `articles.catid`, `articles`.`created` AS `articles.created`, `articles`.`created_by` AS `articles.created_by`, `articles`.`created_by_alias` AS `articles.created_by_alias`, `articles`.`modified` AS `articles.modified`, `articles`.`modified_by` AS `articles.modified_by`, `articles`.`checked_out` AS `articles.checked_out`, `articles`.`checked_out_time` AS `articles.checked_out_time`, `articles`.`publish_up` AS `articles.publish_up`, `articles`.`publish_down` AS `articles.publish_down`, `articles`.`images` AS `articles.images`, `articles`.`urls` AS `articles.urls`, `articles`.`attribs` AS `articles.attribs`, `articles`.`version` AS `articles.version`, `articles`.`ordering` AS `articles.ordering`, `articles`.`metakey` AS `articles.metakey`, `articles`.`metadesc` AS `articles.metadesc`, `articles`.`access` AS `articles.access`, `articles`.`hits` AS `articles.hits`, `articles`.`metadata` AS `articles.metadata`, `articles`.`featured` AS `articles.featured`, `articles`.`language` AS `articles.language`, `articles`.`xreference` AS `articles.xreference`, `categories`.`id` AS `categories.id`, `categories`.`asset_id` AS `categories.asset_id`, `categories`.`parent_id` AS `categories.parent_id`, `categories`.`lft` AS `categories.lft`, `categories`.`rgt` AS `categories.rgt`, `categories`.`level` AS `categories.level`, `categories`.`path` AS `categories.path`, `categories`.`extension` AS `categories.extension`, `categories`.`title` AS `categories.title`, `categories`.`alias` AS `categories.alias`, `categories`.`note` AS `categories.note`, `categories`.`description` AS `categories.description`, `categories`.`published` AS `categories.published`, `categories`.`checked_out` AS `categories.checked_out`, `categories`.`checked_out_time` AS `categories.checked_out_time`, `categories`.`access` AS `categories.access`, `categories`.`params` AS `categories.params`, `categories`.`metadesc` AS `categories.metadesc`, `categories`.`metakey` AS `categories.metakey`, `categories`.`metadata` AS `categories.metadata`, `categories`.`created_user_id` AS `categories.created_user_id`, `categories`.`created_time` AS `categories.created_time`, `categories`.`modified_user_id` AS `categories.modified_user_id`, `categories`.`modified_time` AS `categories.modified_time`, `categories`.`hits` AS `categories.hits`, `categories`.`language` AS `categories.language`, `categories`.`version` AS `categories.version` FROM `jom343_content` AS `articles` LEFT JOIN `jom343_categories` AS `categories` ON `articles`.`catid` = `categories`.`id` WHERE `articles`.`access` = '1' AND `categories`.`parent_id` = '92' ORDER BY `articles`.`publish_up` DESC LIMIT 30
)


It seems the filter is not working as there is no condition on the 'articles.catid'.

Florence
GreyHead 30 Nov, 2015
Hi Florence,

That looks like the Joomla! Debugger output, not the CC Debugger output, please see if you can get that to show.

Bob
FloB 30 Nov, 2015
Hi Bob,

It's the debug results I've got when I turn on the CC Debugger in the frontend listing...
Don't know what is the Joomla! Debugger you're talking.

Florence
GreyHead 30 Nov, 2015
HI Florence,

My apologies, you are correct, I was expecting something more like a CF debugger listing with only one query.

I'm not expert in CC but I think that you need to do two things. One is to create the input as in this FAQ; the other is to add an entry in the Filters box - the help there says that this should be in the form fltr[Model][field] - for you I think this would be fltr[articles][catid] and not articles.catid

Bob
FloB 30 Nov, 2015
Hi Bob,

In the FAQ, the input is textbox and I would have a dropdown with the catgeories' titles.

But for the test, I have put this code in the header code :
<input type="text" name="fltr[articles][catid]" class="form-control A" />
<br>
<input type="submit" name="submit" value="Submit" />

The CC Debugger listing is the same as before changes and the filter is not working is I put the id of a categorie.

I don't understand where you want me to put [articles][catid] instead of articles.catid
If it's on the filters box in the settings, I have tried but the results is the same (and the sublabel says Model.field)

Florence
GreyHead 30 Nov, 2015
Hi Florence,

Under the Filters box I have this
[list]Multi line list of fields to be used as filters, e.g:Model.field, filter fields will have to be created by user, fields names should be in this format: "fltr[Model][field]"[/list]

Bob
FloB 30 Nov, 2015
Hi Bob,

As shown before, the fields names are corrects, or is there something I misunderstood?
<input type="text" name="fltr[articles][catid]" class="form-control A" />


Florence
GreyHead 01 Dec, 2015
Hi Florence,

What exactly do you have in the Filters box?

Bob
FloB 01 Dec, 2015
Hi Bob,

In attachement, a backup of my listing.

As I understand the information under the filters box (and the faq), I have put
articles.catid
in the filters box and
<input type="text" name="fltr[articles][catid]" class="form-control A" />
in the header code box.

Thanks
Florence
GreyHead 02 Dec, 2015
Hi Florence,

I got this working on a copy of your listing

Here's the code I used
<?php
$db = \JFactory::getDBO();
$query = "
    SELECT `title`, `id`
        FROM `#__categories`
        WHERE `parent_id` = '13' ;
";
$db->setQuery($query);
$data = $db->loadObjectList();
echo'<div>$data: '.print_r($data, true).'</div>';
$options = array();
  $options[] = "<option value='' >==??==</option>";
foreach ( $data as $d ) {
  $options[] = "<option value='{$d->id}' >{$d->title}</option>";
}
$options = implode ("\n", $options);
$jdoc = \JFactory::getDocument();
$script = "
jQuery(document).ready(function(jQ) {
  jQ('#fltr_catid').on('change', function() {
    jQ('#submit_btn').click();
  });
});
";
$jdoc->addScriptDeclaration($script);
?>
<h2>Liste des comptes rendus de sortie filtrés</h2>

<select name="fltr[articles][catid]" id='fltr_catid' class="form-control A" >
<?php echo $options; ?>
</select>
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" class="form-control A" style='display: none;' />
<hr />
_PAGINATOR_NAV_

The database query gets the categories - I used 13 instead of your 92 - and then builds an options list for the drop-down.

I also added a little bit of JavaScript that submits the form when the drop-down is changed - actually it clicks the hidden submit button as I couldn't get .submit(0 to work for some reason.

Bob

PS There's some debugger code on the listing that you can ignore - and I have no dog articles, only cat ones it seems.
FloB 02 Dec, 2015
Hi Bob,

Thanks for the code.

The dropdown is correctly populated and the page refreshs when I choose some categorie but the listing is not filtered.
I have also found that the toolbar is not working if I follow the FAQ for direct editing (the page refresh but nothing is saved).

Is there something missing in my installation?

Florence
GreyHead 02 Dec, 2015
Hi Florence,

Is the filter showing up in the Query if you check the Debugger output?

Bob
FloB 02 Dec, 2015
Hi Bob,

No, there is no sign of the filter. The debugger output is the same before and after choosing some categorie and is the same as shown in the previous comments.

Florence
This topic is locked and no more replies can be posted.