Best way to create a table?

Best way to create a table?

Dld Dld -
回帖数:4

Hi, I'm using Moodle 4.4. and I would like to create a table that fits well inside Moodle. I would like to know what is the best way to create a table, is using tablelib? But, I would like the user experience to be better, for example, as it happens in reportbuilder or in the user table of a course, where we don't need to reload the page by reordering columns, where we have modal dialogue to confirm the deletion of a record, etc. What tools does Moodle provide us to create a style page? For example, we would use form api (moodleform) for the form; modal / core for manners; require, inside the mustache (if we need it)?

What would you recommend?
 
 
 
 
回复Dld Dld

Re: Best way to create a table?

Bret Miller -
Particularly helpful Moodlers的头像

The only official way to create new tables is by using the XMLDB tool. You should start here:
https://moodledev.io/general/development/tools/xmldb

回复Dld Dld

Re: Best way to create a table?

JHON EDINSON RANGEL ARDILA -
Plugin developers的头像

Hello

You can use table_sql:

require "config.php";
require "$CFG->libdir/tablelib.php";
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url('/test.php');

$download = optional_param('download', '', PARAM_ALPHA);

$table = new table_sql('uniqueid');
$table->is_downloading($download, 'test', 'testing123');

if (!$table->is_downloading()) {
    // Only print headers if not asked to download data
    // Print the page header
    $PAGE->set_title('Testing');
    $PAGE->set_heading('Testing table class');
    $PAGE->navbar->add('Testing table class', new moodle_url('/test.php'));
    echo $OUTPUT->header();
}

// Work out the sql for the table.
$table->set_sql('*', "{user}", '1=1');

$table->define_baseurl("$CFG->wwwroot/test.php");

$table->out(40, true);

if (!$table->is_downloading()) {
    echo $OUTPUT->footer();
}

That example, Is ubicated in the documentation.
Or You can use html_table:

$table = new html_table();
$table->head = array('Lastname', 'Firstname', 'ID Number');
$table->data[] = array(... first row of data goes here ...);
$table->data[] = array( ... second row of data goes here ...);
echo html_writer::table($table);

It is part of HTML Writer API: https://moodledev.io/docs/4.1/apis/core/htmlwriter
回复Dld Dld

Re: Best way to create a table?

Mark Sharp -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像

Perhaps the quickest start is table_sql, you can write your sql directly in it. It will handle paging and sorting, but you'll need to manage your own filters.

Or reportbuilder... this requires a lot of boiler plate code; things here; things there, but it can handle filters, and there are examples in Moodle of the Modal dialogues you're after. Don't think it does resorting without reloading a full page, but someone might correct me on that.

Couple of things I like about reportbuilder is the built-in support for filters and it will look the same as Moodle's own filter dialogue, and that if you have your own entities you want to be made available to a wider audience you can do that, and build bespoke reports for whoever needs it.