Moodle自定义区块开发问题:HTML表格显示位置异常
问题解决:Moodle自定义区块表格显示位置错误
问题根源在于你直接使用echo输出表格,这会把内容直接发送到页面输出流,导致表格显示在页面顶部而非区块内部。正确的做法是将表格的HTML内容赋值给区块的content->text属性。
修改后的代码如下:
class block_scorecard extends block_base { function init() { $this->title = get_string('pluginname', 'block_scorecard'); } function get_content() { global $DB; if ($this->content !== NULL) { return $this->content; } $courses = $DB->get_records('scorm_scoes_track', ['element' => 'cmi.core.total_time']); $table = new html_table(); $table->head = array('No of attempts','Time modified','status'); foreach ($courses as $course) { $attempt = $course->attempt; $timemodified = userdate($course->timemodified, get_string('strftimerecentfull')); $status = $course->value; $table->data[] = array($attempt, $timemodified, $status); } // 关键修改:将表格HTML赋值给content文本,而非直接echo $this->content = new stdClass; $this->content->text = html_writer::table($table); return $this->content; } }
核心修改说明:
- 移除了直接输出表格的
echo html_writer::table($table);语句 - 将
html_writer::table($table)生成的HTML字符串直接赋值给$this->content->text - 补充了
return $this->content,确保区块能正确返回渲染内容
内容的提问来源于stack exchange,提问作者Parul S
相关产品推荐
相关产品推荐

