phpdrupaldrupal-6drupal-fapi

Drupal: how to show a form (select) acting as filter options, then show a table of stuff from the database?


I want a simple filter form, and a table below it. When the user changes the option on the select form, the table automaticaly changes. I think thats done with ahah.

I want this (some things can change, like the fieldset containing the table, and other stuff):

But working.. of course..

I'm currently showing that page using one function. It's a complete mess and something like "NEVER DO THIS", but i'm researching and trying some stuff as i'm a drupal learner. This is the relevant code:

    form = array();  
    ahah_helper_register($form, $form_state);  
    //query here, build $options for the select

$form['listar_veics'] = array(  
        '#type'   => 'fieldset',  
    '#prefix' => '<div id="listar-veics-wrapper">',   
    '#suffix' => '</div>',  
    '#tree'   => TRUE,
    );

if (!isset($form_state['values']['listar_veics']['filial']))
    $choice = 1;
else 
    $choice = $form_state['values']['listar_veics']['filial'];


$form['listar_veics']['filial'] = array(
    '#type' => 'select', 
    '#title' => "Listar veículos da filial", 
    '#options' => $filiais,
    '#default_value' => $choice,
    '#ahah' => array(
        'event'     => 'change',
        'path'      => ahah_helper_path(array('listar_veics')),
        'wrapper'   => 'listar-veics-wrapper',
        'method'    => 'replace',
        ),
);

//query for the rows i wanna show

//building $data array, the rows array

//building $header, as an array of strings

$table = theme_table($header, $data);

$page = drupal_render($form);
$page .= $table;
return $page;

So in this code, drupal will only replace the form itself, when i change the option on the select, it shows the new value on the select, but the table isnt rendered again thus not changing.

Thanks, apreciate every suggestion.


Solution

  • I recommend looking at the views module.

    http://drupal.org/project/views

    With that installed, you can create a custom query.

    1. Add all the fields that you would like displayed on your table. (Placa, Marca)
    2. Then add any filters that you would like to limit your query by. (Listar veiculos da filial)
    3. When adding the filter you "expose" it so that when looking at the query, you can change the options on the page.
    4. Then if you want to view what you just made, you need to add a display. If you make it a page display, you can set the path directly to the table.

    For more information might I recommend http://gotdrupal.com/videos/drupal-views-tutorial.

    If you want neater exposed fields I might also recommend http://drupal.org/project/better_exposed_filters