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形式获取选定的表行值。在这个问题上需要帮助。任何帮助将不胜感激。
答案 0 :(得分:0)
您在$中选择的内容是您在表格中选择的$ rows的索引。要获取$ rows中的值,您需要使用$ 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']);
}
}