首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Drupal 6:复选框表不能正确呈现

Drupal 6:复选框表不能正确呈现
EN

Stack Overflow用户
提问于 2012-05-12 07:36:30
回答 1查看 544关注 0票数 1

我正在开发一个Drupal6模块,我想根据保存在数据库中的数据生成一个表,其中每行都有复选框。表格生成得很好,但是复选框并没有在表格中呈现,而是将它们的节点id放在表格下面。如下截图所示:

"21“是”测试题01“的节点id,"19”是“测试题02”的节点id。

我正在使用的代码(是的,它都在一个主题函数中,这不是很理想。我计划在复选框问题解决后移动东西):

代码语言:javascript
复制
function theme_qt_assignment_questions_table($form) {
   // Get the questions from the database
   $db_results = db_query('SELECT {qt_questions}.nid, title, lesson, unit FROM node INNER JOIN {qt_questions} on {node}.nid = {qt_questions}.nid WHERE lesson = %d AND unit = %d', 
                     $form['#lesson'], $form['#unit']);

  // Define the headers for the table
  $headers = array(
    theme('table_select_header_cell'),
    array('data' => t('Title'), 'field' => 'title'/*, 'sort' => 'asc'*/),
    array('data' => t('Lesson'), 'field' => 'lesson'),
    array('data' => t('Unit'), 'field' => 'unit'),
  );

  while($row = db_fetch_object($db_results)) {
    $checkboxes[$row->nid] = '';

    $form['nid'][$row->nid] = array(
      '#value' => $row->nid
    );
    $form['title'][$row->nid] = array(
      '#value' => $row->title
    );
    $form['lesson'][$row->nid] = array(
      '#value' => $row->lesson
    );
    $form['unit'][$row->nid] = array(
      '#value' => $row->unit
    );
  }

  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#options' => $checkboxes,
  );

  // Add the questions to the table
  if(!empty($form['checkboxes']['#options'])) {
    foreach(element_children($form['nid']) as $nid) {
      $questions[] = array(
        drupal_render($form['checkboxes'][$nid]),
        drupal_render($form['title'][$nid]),
        drupal_render($form['lesson'][$nid]),
        drupal_render($form['unit'][$nid]),
     );
    }
  } else {
    // If no query results, show as such in the table
    $questions[] = array(array('data' => '<div class="error">No questions available for selected lesson and unit.</div>', 'colspan' => 4));
  }

  // Render the table and return the result
  $output = theme('table', $headers, $questions);

  $output .= drupal_render($form);
  return $output;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-13 11:04:13

事实证明,我试图简化这个问题,实际上就是这个问题。也就是说,用hook_theme做所有的事情都是不正确的。相反,我定义了一个函数,它从数据库中提取信息,创建checkboxes数组并在hook_form中调用它,如下所示:

代码语言:javascript
复制
$form['questions_wrapper']['questions'] = _qt_get_questions_table($node->lesson, $node->unit);

在此函数(_qt_get_questions_table())的末尾,我指定了主题函数,该函数将所有内容放入表中:

代码语言:javascript
复制
  $form['#theme'] = 'qt_assignment_questions_table'; 

我仍然是Drupal的新手,所以对于有同样问题的人来说,这个解释可能不是最好的,但希望它能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10559815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档