Improving symfony backend filters. Taking advantage of the screen using a window
Escrit per RickArt a development, symfony, tags: develop, filters, symfonyWhen we started to use the backend generated by Symfony, the first thing we noticed is a block of filters that reduce the capacity of expansion of primary data.
Solution: Use a window to show the filters.
By doing so we can expand the data horizontally. The filters will be displayed/hidden through a filter button.
Configuring JQuery and JQueryUI is very simple. We only have to:
- download the packages and extract the contents in [symfony_project]/web/js/jq
/js/jq/jquery.js /js/jq/jquery-ui.js /js/jq/theme/*
- configure the view.yml of our app to include the new libraries:
stylesheets: [main, admin, ../js/jq/theme/ui.all.css] javascripts: [main, jq/jquery-ui]
<div id="menuwrapper">
<ul>
<li><?php echo link_to('Píldoras', '@lib_pill'); ?></li>
<li><a href="#">Atributos</a>
...
// Rest of menu
</ul>
<ul style="float:right">
<?php if (sfContext::getInstance()->getActionName() == 'index'): ?>
<li>
<?php echo link_to_function(__('No active filters'),
'showFilters()',
array('id'=>'filters_link'));
?>
</li>
<?php endif; ?>
<li class="dark"><?php echo link_to('Cerrar sesión', '@signout'); ?></li>
</ul>
</div>
<div id="content">
<?php echo $sf_content ?>
</div>
...
It's important to pay attention at the first condition to ensure that this button is showed only when we are in a listing.
The function that is called from the button must be defined in anywhere, for example main.js or admin.js. The code of this function must be: (Please note that we have previously included the jQuery libraries)
function showFilters (module)
{
var action = ($("#sf_admin_bar").dialog('isOpen'))? 'close':'open';
$("#sf_admin_bar").dialog(action);
}
// Menu configured as we have said previously
<div id="content">
<?php echo $sf_content ?>
</div>
<?php if (sfContext::getInstance()->getActionName() == 'index'): ?>
<script type="text/javascript">
$("#sf_admin_bar").dialog({
autoOpen: false,
width: 600,
height: 500,
title: '<?php echo __('Filters') ?>' });
var hasFilter = false;
$("*[id*='lib_<?php echo sfContext::getInstance()->getModuleName(); ?>_filters']").each(function(i) {
if (this.name.indexOf('[is_empty]') != -1) {
if (this.checked) {
hasFilter = true;
}
}
else if (this.value.length > 0) {
hasFilter = true;
}
});
if (hasFilter)
{
$("#filters_link").text('<?php echo __('There are active filters'); ?>');
$("#filters_link").css('background-color','#F1B2B2');
}
</script>
<?php endif; ?>


Entrades (RSS)
Hello, how are you?
I am new in web development and I read the article, it is very nice!!
Unfortunatelly, when I click in “No active filters” the filter form is not showed.
I am using jquery-1.4.2.min.js and jquery-ui-1.8.custom.min.js.
I have created a main.js in \web\js to write the showFilters function, because I don’t have main.js and admin.js in my project.
I have modified the layout.php.
When the page is showed I can see the link “No active filters”, but when I click on it I cannot see the filter form. The url for the link is: http://booking.localhost/backend_dev.php/#, so I think that I did it something in a wrong way (maybe the place where I wrote showFilters function?).
Thanks in advance!!
Guillermo
Hello, me again.
I solve the problem, I have added main.js in javascripts in the view.yml, and now I can see the filter form!!!
But I have a new problem: I don’t know how to set the hasFilter to see the “There are active filters”. My filter form has two date filter (from and to), two text filters, and a display list (combo) filter, I write some data into the filters, press Filter button, but the legend “No active filters” does not change.
Could you give some tips to solve that, please?
Thank you very much!!
Guillermo
Hi Guillermo,
I think that your problem is with the section code below “var hasFilter = false;”
This code pretend to detect if any filter has been checked. I’m not sure, but it’s possible that the backend generated by latest Symfony version doesn’t match with this code.
You should change this code block to know if there are active filters.
Get luck !