2012-05-11 39 views
1

我正在研究一個Drupal 6模塊,我想從每個數據庫中保存的數據生成一個表格,每行都有複選框。該表格生成的很好,但複選框不是在表格中呈現,而是將其節點ID放在表格的下方。見下圖:Drupal 6:複選框表格不能正確渲染

drupal checkboxes

「21」 是 「試題01」 的節點ID,而 「19」 是 「試題02」 的節點ID。

我正在使用的代碼(是的,這是所有在一個主題函數,而不是理想的我打算在移動的東西,一旦周圍的複選框問題得以解決。):

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; 
} 

回答

0

匝事實上,我試圖簡化問題實際上就是問題所在。也就是說,在hook_theme中做所有事情都是不正確的。相反,我定義的從數據庫拉動信息,並創建對應的複選框陣列並調用它在hook_form作爲這樣的功能:

$form['questions_wrapper']['questions'] = _qt_get_questions_table($node->lesson, $node->unit); 

在該函數的結束(_qt_get_questions_table()),我指定的主題函數把一切都到表這樣的:

$form['#theme'] = 'qt_assignment_questions_table'; 

我還是很新的Drupal的所以這個解釋未必是最好的具有相同問題的人,但我希望這將有助於。