3

Is it possible to build tableselect form with textfield in each row?

function nowayform_form($form, $form_state) {

      $xsroads = array(
      array('id'=>'PrArcabilPodvoyskaya','xsr_name'=>t('Archabil/Podvoyskiy')),
      array('id'=>'PrArcabilSovetskaya','xsr_name'=>t('Archabil/Sovietskaya')),
      array('id'=>'PrArcabilLenina','xsr_name'=>t('Archabil/Lenina'))
      );

      $header = array(
      'xsr_name'=>t('Crossroad names')
      );

      $options = array();
      foreach ($xsroads as $xsroad){
        $options[$xsroad['id']] = array(
        'xsr_name'=>$xsroad['xsr_name']
        );
      }

      $form['table'] = array(
      '#type'=>'tableselect',
      '#header'=>$header,
      '#options'=>$options
      );

Thanks

2 Answers 2

4

in the options you can put another field like this:

foreach($xsroads as $xsroad){
  $options[$xsroad['id']] = array(
    'xsr_name' => $xsroad['xsr_name'],
    'xsr_input' => array(
      'data' => array(
        '#type' => 'textfield',
        '#value => 'so you wanned a field',
      ),
    ),
  );
}
7
  • 1
    Thanks Cornelius Black. But now I cant get values of textfields after form submit :). Therere only 'id's in debug window
    – Nafscript
    Commented Jun 12, 2013 at 6:42
  • just do a print_r of the $form_state and look through the indexed array for the values Commented Jun 12, 2013 at 6:48
  • 1
    Yes it returned array, but there`re no textfields values.
    – Nafscript
    Commented Jun 12, 2013 at 8:03
  • have you looked at xsr_input fields? Commented Jun 12, 2013 at 15:46
  • 1
    Sorry, maybe I did smthing wrong. In form submit function I use debug($form_state['values']);. It returns:
    – Nafscript
    Commented Jun 12, 2013 at 20:02
3

Out of the box -- no you cannot. It is possible with a bit of playing around though:

  • add the form field definition in the 'data' property of the cell you would like to have the element rendered
  • make sure it has the #name property set, since it wont get a name otherwise
  • add a value element to your form definition with blank value

and you're golden!

An example definition of a form callback ( called by drupal_get_form ) with a two row tableselect with textfields:

 function ds_test_form() {
   $options = array(
     array(
      'title' => 'Tableselect element',
      'content_type' => 'with textfield!',
      'status' => 'great',
      'comment' => array('data'=>array('#type'=>'textfield',
      '#default_value'=>'', '#title' => 'Comment', 
      '#title_display'=>'invisible','#name'=> 'comment[row1]'
     ),
    ),
  ),
  array(
    'title' => 'Enjoy',
    'content_type' => 'the',
    'status' => 'action',
    'comment' => array('data'=>array(
      '#type'=>'textfield',
      '#default_value'=>'', 
      '#title' => 'Comment',
      '#title_display'=>'invisible',
      '#name'=> 'comment[row2]'
        ),
      ),
    ),
  );

  $header = array(
    'title' => t('Column 1'),
    'content_type' => t('Column 2'),
    'status' => t('Column 3'),
    'comment' => t('Fields'),
  );

  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No content available.'),
  );

  $form['comment'] = array(
    '#type' => 'value',
  );

  $form['submit'] = array(
    '#type'=>'submit',
    '#value' => 'Go!',
  );

  return $form;

}

A more detailed walkthrough of the above with some screenshots, and why it works that way is at my blog. The above, when submitted, created the following array structure on $form_state['values'] :

Array(
  'table' => Array(
    '1' => '1',
    '0' => 0,
  ),
  'comment' => Array(
    'row1' => 'Comment field text for row1',
    'row2' => 'Comment field text for row2',
  )

)

0

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.