Drupal 7 如何在Drupal7中获取tableselect的提交值

Drupal 7 如何在Drupal7中获取tableselect的提交值,drupal-7,Drupal 7,如何以Drupal形式获取所选表行值。在这个问题上需要帮助。任何帮助都将不胜感激。您在$selected中得到的是您在表中选择的$rows的索引。要获取$rows中的值,需要使用$selected中的索引 我在这里创建了一个简单的示例: function test($form, &$form_state){ $form = array(); $header = array(.............); $values = array(.............);

如何以Drupal形式获取所选表行值。在这个问题上需要帮助。任何帮助都将不胜感激。

您在$selected中得到的是您在表中选择的$rows的索引。要获取$rows中的值,需要使用$selected中的索引

我在这里创建了一个简单的示例:

function test($form, &$form_state){
   $form = array();

   $header = array(.............);

   $values = array(.............);

   $form['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
      '#multiple' => $IsCheckbox,
      '#empty' => t('No users found'),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
  return $form; 
} // end of function test()

function test_submit($form, &$form_state){

$selected = $form_state['values']['table'];

drupal_set_message($selected)  // displays array index (0,1,2 etc)

return;
}
function test($form, &$form_state)
{
    $form = array();

    $header = array(
        'first_name' => t('First Name'),
        'last_name'  => t('Last Name'),
    );

    $rows = array(
        // These are the index you get in submit function. The index could be some unique $key in database.
        '1' => array('first_name' => 'Mario',          'last_name' => 'Mario'),
        '2' => array('first_name' => 'Luigi',          'last_name' => 'Mario'),
        '3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
    );

    $form['table'] = array(
       '#type' => 'tableselect',
       '#header' => $header,
       '#options' => $rows,
       '#multiple' => true,
       '#empty' => t('No users found'),
    );

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

    return $form;
} // end of function test()

function test_submit($form, &$form_state)
{
    // This function should not be duplicated like this but It was easier to do.
    $rows = array(
        '1' => array('first_name' => 'Mario',          'last_name' => 'Mario'),
        '2' => array('first_name' => 'Luigi',          'last_name' => 'Mario'),
        '3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
    );

    $names = array();
    // Remove the names that has not been checked
    $selected_names = array_filter($form_state['values']['table']);

    // Iterate over the indexes that was selected to get the data from original array
    foreach ($selected_names as $index ) {
        array_push($names, $rows[$index]);
    }

    foreach($names as $name) {
        drupal_set_message($name['first_name'] . ' ' . $name['last_name']);
    }
}