2015-07-11 81 views
0
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; 
} 

如何在Drupal表單中獲取所選表格的行值。在這個問題上需要幫助。任何幫助,將不勝感激。如何獲取drupal中tableselect的提交值7

回答

0

您在$選擇中得到的是您在表格中選擇的$行的索引。要獲取$行中的值,您需要使用$ selected中的索引。

我創建了一個簡單的例子,如何在這裏做到這一點:

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']); 
    } 
}