【问题标题】:Drupal form in tabular layout表格布局中的 Drupal 表单
【发布时间】:2010-10-21 07:40:25
【问题描述】:

我正在尝试为表单呈现表格布局(html 表格),以便第一个单元格包含一个复选框。

$form = array();
$res = query('SELECT * FROM {mytable}');
$rows = array();
while($row = db_fetch_array($res)){
    $record = array();
    $checkbox = array(
        '#type' => 'checkbox',
        '#attributes' => array(
            'value' => $row['id'],
            'name' => 'myrecord[]',
        ),
    ); 

    $record[] = drupal_render($checkbox);
    $record[] = $row['other_field_1'];
    $record[] = $row['other_field_2']
    $rows[] = $record;
}

$form['records'] = array(
    '#value' => theme('table', array(), $rows);
);

return $form;

但是所有渲染的复选框都有输出

<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">

名称和值属性未应用于复选框的问题是什么?

【问题讨论】:

    标签: drupal drupal-6 drupal-fapi drupal-render


    【解决方案1】:

    在使用表单 API 时,您不能使用属性设置名称和值。该名称与表单项的键有关,所以如果你有

    $form['pony'] = $checkbox;
    $form['yes'] = $checkbox;
    

    第一个复选框的名称为pony,而第二个复选框的名称为yes。我相信复选框的值始终是 1,但我不是 100% 确定。

    要正确制作此表单,您应该做的是仅在表单定义中创建表单。做这样的事情:

    $form = array();
    $res = query('SELECT * FROM {mytable}');
    while($row = db_fetch_array($res)){
      $form[$row['id']] = array('#tree' => TRUE);
      $form[$row['id']]['checkbox'] = array('#type' => 'checkbox');
      $form[$row['id']]['other_field_1'] = array('#value' => $row['other_field_1']);
      $form[$row['id']]['other_field_2'] = array('#value' => $row['other_field_1']);
    }
    return $form;
    

    这样做,您可以在表单的主题函数中创建表格。在主题函数中渲染表单元素,将为它们提供正确的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-24
      • 2019-10-04
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      相关资源
      最近更新 更多