/ / як отримати подане значення tableselect в drupal 7 - drupal-7

як отримати подане значення табличного вибору в друпалі 7 - drupal-7

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 для відповіді № 1

Що ви отримуєте у вибраному вами $ - це індекс $ рядків, який ви вибрали у своїй таблиці. Для отримання значень у рядках $ вам потрібно використовувати індекс, який ви вибрали у $.

Я створив простий приклад, як це зробити тут:

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