1. 注册theme
function weike_course_evaluate_theme() {
return array(
'weike_course_evaluate_all' => array(
'render element' => 'form',
),
);
}
2. 生成form,数据以value类型form元素传递。
function weike_course_evaluate_all($form, &$form_state) {
$result = db_query('…');
$form['data'] = array();
$form['final-score'] = array();
$form['title'] = array(
'#markup' => '<h2>' . $node->title . '</h2>',
);
$count = 0;
foreach ($result as $srow) {
$form['data'][$count] = array();
$form['data'][$count]['uid'] = array(
'#type' => 'value',
'#value' => $srow->uid,
);
$form['data'][$count]['name'] = array(
'#type' => 'value',
'#value' => $srow->name,
);
if (isset($score_result[$srow->uid]->final_score))
$final_score = $score_result[$srow->uid]->final_score;
else
$final_score = '';
$form['final-score']['final-score-' . $count] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => $final_score,
);
$count++;
}
$form['save'] = array(
'#type' => 'submit',
'#value' => '保存',
);
return $form;
}
2. 实现theme
function theme_weike_course_evaluate_all($variables) {
$form = $variables['form'];
$i = 0;
$header = array('姓名', '最终考核成绩');
$rows = array();
for ($i = 0; isset($form['data'][$i]); $i++) {
$rows[] = array(
$form['data'][$i]['name']['#value'],
render($form['final-score']['final-score-' . $i]),
);
}
$output = render($form['title']);
$output .= theme('table', array('header' => $header, 'rows' => $rows));
$output .= drupal_render_children($form);
return $output;
}
参考: http://stackoverflow.com/questions/1771159/how-to-include-drupal-form-elements-in-a-data-table
评论