Forums

RE: How to pre-filter the initial presentation of a listing

MainsailSoftware 30 Jun, 2015
The following is a response to http://www.chronoengine.com/forums/posts/t20361/p61908/how-to-pre-filter-the-initial-presentation-of-a-listing.html#p61908 which is currently locked.

After investigating the cegcore code and some experimentation I found the following code to work well and be more consistent with how I think ChronoConnection filter fields work.

I placed the following code with in the main model's Conditions box:
<?php

$session =& JFactory::getSession();
$selectList = $session->get('selectList');

// Check selectList has been created and create if needed
if(empty($selectList)) {

    // Make the database call to dynamically retrieve select list items
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select(array(-List of fields-))
               ->from($db->quoteName('#__sourceTable'))
               ->order($db->quoteName('orderField'));
    $db->setQuery($query);
    $selectList = $db->loadAssocList();

    // Save select list in session
    $session->set('selectList', $selectList);

    // Set the selected item based on condition
    $selected = "";
    foreach($selectList as $event) {
        if($event['event_date'] > date('Y-m-d')) {
            $selected = $event['id'];
            break;
        }
    }

    // Set fltr value in Request - used by CCv5 to control retrieved results
    $filter = array('model' => array('field' => $selected));
    \GCore\Libs\Request::set('fltr', $filter);
}

// place your normal WHERE clause code here

?>

I found that messing with the actual conditions seemed to result in some unpredictable results.

The example above creates a select list array for creating a dropdown used as the fltr field to control results presentation. It is saved in session to avoid multiple retrievals.

This approach directly addresses the issue that the initial page load show all results regardless to the initially selected item in the dropdown.

I placed the code to create the filter dropdown in the List display | Table | Header code box, which gets the select list data from session. This header code contains the HTML <select> and jQuery that performs a submit.

This is working well; reliable and consistent results.
This topic is locked and no more replies can be posted.