带收音机的 Drupal 表

发布于 2024-10-26 04:09:26 字数 222 浏览 2 评论 0原文

如何将“收音机”添加到围绕 theme_table 构建的表单中?

使用单个“复选框”或单个“单选”似乎工作正常,但一旦我使用“单选”,就根本不会呈现单选按钮。

从另一个堆栈溢出问题中,我看到提到了 form_process_radios() ,并且使用它实际上显示了单选按钮。但它们不再捆绑在一起,并且所有它们都可以立即进入“开启”状态。

有什么想法吗?

How can I add 'radios' into a form built around theme_table?

Using a single 'checkbox' or single 'radio' seems to work fine, but as soon as I use 'radios' no radio buttons render at all.

From another Stack Overflow question, I've seen form_process_radios() mentioned, and using this actually shows the radio buttons. But they are no longer tied together, and all of them can be put into an 'on' state at once.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜无邪 2024-11-02 04:09:26

简单的回答:你不能轻易做到。 (尽管您可能会成功地定义自己的处理器,使用expand_radios,硬核的东西!)。

更长的答案:radios 使用 主题无线电。正如您所看到的,它使用单个 DIV 包装器,这使得不可能将无线电分布在桌子上。

您最好做的是创建一个分层表单,每个选项都有一个radio。避免使用无线电。通过按层次结构对它们进行分组,名称将是相同的,这就是无线电已分组

# from install.php:
  foreach ($names as $profile => $name) {
    $form['profile'][$name] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $profile,
      '#title' => $name,
      '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
      '#parents' => array('profile'),
    );
  }

然后,在围绕表单构建表格的主题功能中,在适当的表格单元格中呈现每个单选按钮。

Simple answer: you cannot easily. (Allthough you may have success with defining your own processor, using expand_radios, hardcore stuff!).

Longer answer: radios uses theme_radios. And as you can see, that uses a single DIV-wrapper, which makes it impossible to spread out radios over a table.

What you could best do, is create a hierarchical form, whith one radio per option. Avoid radios. By grouping them hierarchically the name will be the same, which is the way radios are grouped.

# from install.php:
  foreach ($names as $profile => $name) {
    $form['profile'][$name] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $profile,
      '#title' => $name,
      '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
      '#parents' => array('profile'),
    );
  }

Then, in the theme-function where you build the table around the form, you render each radio in the appropriate table-cell.

汐鸠 2024-11-02 04:09:26

因此,要使用 Berkes 建议,您必须使用“#atributes”属性,对同一组的所有无线电元素使用相同的名称。

像这样:

'#attributes' => array('name' => array('somename'))

这是一个更完整的示例:

for ($i = 0; $i < $num; $i++){
  $element[$i]['answer'] = array(
    '#type' => 'textfield',
    '#title' => t('Answer'),
  );
  $element[$i]['correct'] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $i,
      '#title' => t('Correct answer'),
      '#attributes' => array('name' => array('correct-answer')),
  );
}

So to use Berkes advice you would have to use the '#atributes' property, with the same name for all your radio elements of the same group.

like this:

'#attributes' => array('name' => array('somename'))

This is a more complete example:

for ($i = 0; $i < $num; $i++){
  $element[$i]['answer'] = array(
    '#type' => 'textfield',
    '#title' => t('Answer'),
  );
  $element[$i]['correct'] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $i,
      '#title' => t('Correct answer'),
      '#attributes' => array('name' => array('correct-answer')),
  );
}
瑾兮 2024-11-02 04:09:26

下面是一个 Drupal 7 示例,展示了如何在表格内添加单选按钮,以便轻松使用它们:

$form['item']['table_start'] = array(
  '#markup' => '<table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody>'
);
for ($i = 1; $i < 3; $i++) {
  $form['item']['tr_start_'.$i] = array('#markup' => '<tr>');
  $form['item'][$i]['item'] = array(
    '#type' => 'radio',
    '#title' => t('Title'),
    '#return_value' => $i,
    '#prefix' => '<td>',
    '#suffix' => '</td>',
  );
  $form['item']['item_'.$i] = array('#markup' => '<td>'. $i .'</td>');
  $form['item']['tr_end_'.$i] = array('#markup' => '</tr>');
}
$form['item']['table_end'] = array('#markup' => '</tbody></table>');

这使我能够使用 并在验证中获取选定的单选按钮值,并使用 $form_state['values']['item'] 提交处理程序。单选按钮已经具有相同的名称属性,因此无需手动指定即可链接在一起。

Here's a Drupal 7 example of how I got radio buttons inside a table so that they were easy to work with:

$form['item']['table_start'] = array(
  '#markup' => '<table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody>'
);
for ($i = 1; $i < 3; $i++) {
  $form['item']['tr_start_'.$i] = array('#markup' => '<tr>');
  $form['item'][$i]['item'] = array(
    '#type' => 'radio',
    '#title' => t('Title'),
    '#return_value' => $i,
    '#prefix' => '<td>',
    '#suffix' => '</td>',
  );
  $form['item']['item_'.$i] = array('#markup' => '<td>'. $i .'</td>');
  $form['item']['tr_end_'.$i] = array('#markup' => '</tr>');
}
$form['item']['table_end'] = array('#markup' => '</tbody></table>');

This allowed me to render the whole table part of the form in my template file with <?php print drupal_render($form['item']); ?> and get the selected radio button value in the validation and submit handlers with $form_state['values']['item']. The radio buttons already had the same name attribute and were thus linked together without the need to manually specify it.

┾廆蒐ゝ 2024-11-02 04:09:26

我认为例如您应该阅读此表单回调: node_admin_nodes 来自node.admin.inc

I think for example you should to read this form callback: node_admin_nodes from node.admin.inc

╰沐子 2024-11-02 04:09:26

您需要才能使用此 theme_tableselect 函数

You need to use this theme_tableselect function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文