How would YOU implement checkboxes within a table?

carteriii - July 24, 2008 - 19:59

Drupal doesn't seem to support the combined use of tables and forms very well, so I'm looking for advice. I'm not stuck, and in fact I've actually implemented two versions so far, but I'm simply not happy with the result so I'm looking for some "best practices" advice from those of you with more experience. If I've simply missed some great feature, please tell me.

My module is very simple to understand. It presents a table to the user with checkboxes in the left-most column. The user can select certain rows using the checkboxes, and then submit a form with those selections.

I've come up with two ways to do this, neither of which are ideal. The first is to simply build the html table and form/checkbox elements myself. I don't like this because it doesn't take advantage of Drupals form submission which includes things like tokens to prevent cross-site forgery requests, etc.

The second version uses Drupal's form definition and describes the rest of the table using the #prefix and #suffix keys on the checkbox elements as described in this old post: http://drupal.org/node/80270. This seems to be the better approach, but still prevents proper theming of the table, using a function call like theme('table', ...).

So how would YOU implement checkboxes (or other form elements) within a table? Thanks in advance.

You can theme the form which

nevets - July 24, 2008 - 21:43

You can theme the form which is how I would do it.

I would download the

Heine - July 24, 2008 - 21:54

I would download the elements module (http://drupal.org/project/elements) and use the provided tableselect element type.

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

Themed table with checkboxes within Drupal form

geo_ego - August 4, 2008 - 10:01

I found a trick how to insert table with checkboxes into Drupal API form (works in Drupal 5).

In a snippet below, function module_get_my_table($params) returns a fully-themed table with checkboxes in one column added as a plain HTML element:

foreach($list as $row_index => $row) {
      ... // (other columns)
      $opt      = '<input type = "checkbox" name="opt_' . $row_index . '" >';
  }

All those checkboxes will be named 'opt_0' ... 'opt_.X.'

This is a snippet from module_form hook that shows that table inside the form:

  ...
// Complex fully-themed table with checkboxes in one column
// is being added as a form markup element:

  $form['tbl_chk'] = array(
     '#value' => module_get_my_table($list)
);

// ** But those checkboxes in the table do not become a part of the form
// ** so they are not accessible through form_submit hook

// ** Here is the trick:  for each 'opt_..' checkbox in the table above
// ** we add 'opt_..' element into $form[] array with the SAME name
// ** and make it hidden using #prefix and #suffix:

  foreach($list as $list_index => $row) {
    $form['opt_' . $list_index] = array(
                    '#type' => 'checkbox',
                    '#prefix' => '<!-- ',
                    '#suffix' => ' -->'  
                );
  }

In order this to work, naming of checkboxes in a table and in $form[] array must be the same.
Now, all checkeboxes in a table that are checked by user, are passed to form_submit under those names:

Array
(
//checkbox values:
[opt_0] => 0
[opt_1] => 1
[opt_2] => 0
[opt_3] => 1

//... (other values)
)

A NOT ugly option

caramelson - September 29, 2008 - 03:51

The formtable module (5.x).

--
Fredric
http://brightplum.com

I'd copy the way it is done in core.

InternetPro - October 23, 2008 - 10:09

If you roll your own modules, you might take some inspiration from the way the put checkboxes into tables in core.

This is the code that builds the admin/user/user page.

http://api.drupal.org/api/function/user_admin_account/6

There's also a few other examples of how to put checkboxes into tables in other core modules as well.

Hope that helps!

and also the theme function

alpapan - August 6, 2009 - 12:30

and also the theme function for this form

http://api.drupal.org/api/function/theme_user_admin_account/6

 
 

Drupal is a registered trademark of Dries Buytaert.