4

I want to organize my data in table rows. How can I user Forms API to do it?

EDIT:

I want to show the data in the following way in multiple rows:

row1 checkbox info1 textfield1 info2 textfield2 ....

....

rowN ....

row 1 [add a new row fields here]

...

row 5

One should be able to cancel existing records and in the bottom fill in fields to add new records.

And I finding the solutions in this link or in this really difficult to understand.

4
  • Found similar question here: drupal.stackexchange.com/questions/5374/…
    – AgA
    Commented Dec 16, 2011 at 11:48
  • Please provide more information. What do you mean with "data"? Input Fields, just some tabular content, something else?
    – BetaRide
    Commented Dec 16, 2011 at 21:46
  • Also, there are other ways to display the data in a table, why use a form, do you want the user to edit the content?
    – Ashlar
    Commented Dec 17, 2011 at 2:41
  • I've updated the question ( after EDIT)
    – AgA
    Commented Dec 17, 2011 at 5:51

1 Answer 1

2

The simplest way to do this (on small forms) that I have found is by taking advantage of the #prefix and #suffix elements in the render array. Here's an example:

$form['foo_fieldset'] = array(
  '#type' => 'fieldset',
  '#prefix' => '<table>',
  '#suffix' => '</table>',
  '#tree' => TRUE,
);
$form['foo_fieldset']['header'] = array(
  '#markup' => '<thead>
    <tr>
      <th>A checkbox</th>
      <th>A text input</th>
      <th>A text input</th>
    </tr>
  </thead>',
);

for ($i=0;$i<10;$i++) {
  $form['foo_fieldset']['row_' . $i] = array(
    '#prefix' => '<tr>',
    '#suffix' => '</tr>',
  );
  $form['foo_fieldset']['row_' . $i]['checkbox'] = array(
    '#type' => 'checkbox',
    '#prefix' => '<td>',
    '#suffix' => '</td>',
  );
  $form['foo_fieldset']['row_' . $i]['text_1'] = array(
    '#type' => 'textfield',
    '#prefix' => '<td>',
    '#suffix' => '</td>',
  );
  $form['foo_fieldset']['row_' . $i]['text_2'] = array(
    '#type' => 'textfield',
    '#prefix' => '<td>',
    '#suffix' => '</td>',
  );
}

The other way to do it (maybe known as the "right" way) involves building a theming function to render your form in a table. Take a look at this page (ignoring the tablesort options as you don't care about that) to see how it's done: http://coffeeshopped.com/2010/09/drupal-drag-and-drop-tables-in-custom-modules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.