Form API: display a group of fields as a table?

usonian - December 8, 2007 - 20:59

I've done a fair amount of poking around d.o. forums and handbooks, and looked through all of the contrib modules and I've still come up empty - this one of those problem-solving questions I'm a little surprised no one else seems to have encountered (or at least, gotten stuck enough on to bother posting or contributing a module for.)

Basically, I have a bunch of related fields that I want to display in table format; a header row containing titles and each subsequent row containing a textfield (although really, the field type doesn't matter). Sort of like this:

First Name  | Last Name  | Age
-----------------------------------
foo1_first  | foo1_last  | foo1_age
-----------------------------------
foo2_first  | foo2_last  | foo2_age
-----------------------------------
foo3_first  | foo3_last  | foo3_age
-----------------------------------

I know there's the CCK Matrix module but, in my particular case I'm not using CCK - and it seems like displaying fields in a table could be useful for general UI-building apart from CCK.

So, my question is this - is there some quick, no-brainer way to do this with stock Form element types that should be staring me right in the face, or is a module warranted? And if so, is there one already out there that I'm just not finding? I don't mind writing the module, I just don't want to reinvent the wheel, duplicate effort or clutter up the contrib directory.

I'm imagining a module that implements two new form element types:

'rowset' - behaves more or less like a 'fieldset', but wraps its children in a <tr> tag.
'table' - also behaves like 'fieldset', containing 'rowset' elements. Also receives a '#header' attribute containing an array of column headers.

So to build the matrix above, the code might look something like

$form = array();
$form['mytable'] = array(
  '#type'     => 'table',
  '#header' => array(t('First Name'), t('Last Name'), t('Age'))
);

$form['mytable']['row1'] = array(
  '#type' => 'rowset'
);

$form['mytable']['row1']['foo1_first'] => array(
  '#type' => 'textfield',
  '#prefix' => '<td>',
  '#suffix' => '</td>'
);

//Et cetera

If anyone has any thoughts/insight, they would be much appreciated!

prefix and suffix

scrypter - December 8, 2007 - 22:03

will give you what you want. Just add the <table><tr><td> for the first field and </tr></table> for the last suffix and voila!

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

Yep, it was staring me in the face. :)

usonian - December 8, 2007 - 22:35

Thanks scrypter, that should have occurred to me once I got as far as using the <td> prefix and suffixes. :)

In the meantime I already went ahead and wrote a module to add new 'formtable' and 'formrow' element types, and that solution also works like a charm.

same problem here

SuperScientist - January 22, 2008 - 14:58

Can you please share this with me, because I have the same problem and I am looking for it already quite some time. Maybe your code can be also very useful to me.

Thanks.

Better late than never, I hope

usonian - March 1, 2008 - 17:36

I finally cleaned up the code a bit and released the formtable module.

 
 

Drupal is a registered trademark of Dries Buytaert.