Thursday, June 26, 2008

Displaying actions in a html select object

Here is an interesting example of how to use actions in an html select from object.

In our actions.ini file (delegate or conf.ini) we have a set of actions each with the category attribute set to bd_company_filters. Like this:


[show_only_approved_news]
;; Show only companies that have been approved by this broker dealer
url="{$this->url('approval_status=2')}"
condition="$query['-table'] == 'news' and isBrokerDealer()"
label="Approved News"
category=bd_news_filters


In our delegate class, we can access these actions like this:


$at =& Dataface_ActionTool::getInstance();
$actions = $at->getActions(array('category'=>'bd_company_filters'));


The first line retrieves the Dataface_ActionTool object which we then use to return an array of actions underneath the category bd_company_filters. Each action in the array of actions will contain the attributes specified in the actions.ini and can be accessed by simply using the normal array key like this:


foreach ($actions as $action){
echo action['url']
}


Here we are accessing the url attribute of each action under this category.

So putting it all together, we can do something like this (NOTE: this code is in the block__before_result_list() function):


$at =& Dataface_ActionTool::getInstance();

$actions = $at->getActions(array('category'=>'bd_company_filters'));
echo '<select onchange="window.location=this.options[this.selectedIndex].value" class="company_filters">';

echo '<option value="#">Filter Resources in this Category</option>';

foreach ($actions as $action){
echo '<option value="'.$action['url'].'">'.$action['label'].'</option>';
}

echo '</select>';


Here we are creating a html select object and then we are placing the different actions as options for the select valuelist. The labels of the actions act as the labels in the select list, and the urls are used as the values of the options.

A little simple javascript is used to change the window.location to the value of the selected item in the list on change.

No comments: