在Moodle中将html_table更改为flexible_table后,我得到的第一行完全为空,因此排序不起作用。
我更改了表格,因为html_table没有排序功能。
代码是:

ob_start();
            $table = new flexible_table('Cars');
            $table->define_baseurl(new moodle_url("/blocks/cars/view.php"));
                $table->define_columns(array(
                    'carname',
                    'carnumberplate',
                    'carhiredate',
                    'city',
                    'actions',
                ));
            $table->define_headers(array(
                    get_string('carname', 'block_cars'),
                    get_string('carnumberplate', 'block_cars'),
                    get_string('carhiredate', 'block_cars'),
                    get_string('city', 'block_cars'),
                    get_string('actions')
                ));
            $table->sortable(true, 'cars');
            $table->collapsible(false);

            $table->set_attribute('cellspacing', '0');
            $table->set_attribute('id', 'view-block-cars');
            $table->set_attribute('class', 'generaltable');
            $table->setup();
            // Add one blank line
            $table->add_data(NULL);

            $strdateformat = get_string('strftimedate');
            $strtimeformat = get_string('strftimetime');
            foreach ($cars as $car) {
                    $datestart = userdate($car->starttime, $strdateformat);
                    $dateend = userdate($car->endtime, $strdateformat);
                    $timestart = userdate($car->starttime, $strtimeformat);
                    $timeend = userdate($car->endtime, $strtimeformat);

                    $date = $datestart;
                    if ($datestart != $dateend) {
                        $date .= ' - '.$dateend;
                    }
                    $time = $timestart.' - '.$timeend;

                    $actions = array(
                        html_writer::link($car->get_view_url(), get_string('view')),
                    );
                    $actions = implode(' | ', $actions);

                    $row = array(
                        format_string($car->carname),
                        format_string($car->platenumber),
                        $date,
                        format_string($car->city),
                        $actions,
                    );
                    $table->add_data($row);
            }
            $table->finish_html();
            $out .= ob_get_clean();

        return $out;


我缺少什么?

最佳答案

首先,仔细检查您是否从carname中获取了字段$DB->get_records()(我假设您正在使用此功能或类似功能)。

其次,它有助于在foreach中执行print_object($car->carname);来确定问题,以检查这是否正确打印了数据。在您进行foreach之前,还要先print_object($cars);并检查结果

第三,如果数据正确打印,我强烈建议您调试文件并查看可能收到的错误,警告或注意消息。在require "../../config.php";之后添加以下行

// Force a debugging mode regardless the settings in the site administration
@error_reporting(E_ALL | E_STRICT); // NOT FOR PRODUCTION SERVERS!
@ini_set('display_errors', '1');    // NOT FOR PRODUCTION SERVERS!
$CFG->debug = (E_ALL | E_STRICT);   // === DEBUG_DEVELOPER - NOT FOR PRODUCTION SERVERS!
$CFG->debugdisplay = 1;             // NOT FOR PRODUCTION SERVERS!


让我知道是否有帮助

07-26 05:36