Datatables

Administration tool ::: tool_datatables
Maintained by Fred Yankowski
Provides API to add DataTables features to Moodle HTML tables.
Latest release:
67 sites
19 downloads
17 fans
Current versions available: 2

DataTables support in Moodle

This Moodle plugin allows for enhancing HTML tables with DataTables features such as in-browser sorting and filtering of table values.

Examples

To add basic DataTables filtering and sorting to the tables generated by the Ad Hoc Database Queries plugin, add a javascript/datatables.js file to your theme with this content:

require(['core/first'], function() {
    require(['tool_datatables/init'], function(dt) {
        selector = '#page-admin-report-customsql-index .generaltable, #page-admin-report-customsql-view .generaltable';
        dt.init(selector, {});
    });
});

and add this to the admin theme's config.php:

$THEME->javascripts_footer = array("datatables");

The above general scheme allows one to apply DataTables to any table in the moodle core or plugins as long as one can construct the necessary CSS/jQuery selector to select those tables.

See test.php for an example plugin page that employs DataTables. The simplest case adds this to the page definition:

$PAGE->requires->js_call_amd('tool_datatables/init', 'init',
                             array('.datatable', array()));

Any table with the datatable class (matching the .datatable selector above) will get default DataTables features. The second parameter allows for passing options to DataTables, just an empty array() in this case.

Screenshots

Screenshot #0

Contributors

Fred Yankowski (Lead maintainer)
Please login to view contributors details and/or to contact them

Comments RSS

Show comments
  • Plugins bot
    Tue, 10 Nov 2015, 10:11 PM
  • Fred Yankowski
    Tue, 10 Nov 2015, 11:45 PM
    I have addressed all the issues reported by the prechecker in version 0.1, except for javascript and CSS warnings concerning the original third-party DataTables code, in version 0.2 just uploaded.
  • David Mudrák
    Thu, 26 Nov 2015, 8:43 PM

    Thanks Fred for sharing this plugin with the community. Please find some suggestions left in your issues tracker. You are cleared to land now, welcome to the Plugins directory!

  • Juraj Obonya
    Thu, 19 July 2018, 7:15 PM
    Hello. Fred. Is it possible to add the DataTables support for any other html_table in moodle ?? I mean no just on the datatable created by Ad-hoc database queries .
    Thanks in advance.
  • Fred Yankowski
    Thu, 19 July 2018, 10:49 PM
    Juraj: Yes, that should be possible. I haven't looked at this in quite a while, but I believe that if you modify the `selector` value in my initial example code above so that it matches the HTML table(s) of interest, datatables functionality will then be linked to those tables.
  • Juraj Obonya
    Fri, 20 July 2018, 6:32 PM
    Fred: Thank you for the answer. It works as you said.
  • Olli Savolainen
    Mon, 26 Nov 2018, 6:14 PM
    Hi. Where in the moodle directory should this be placed? Thanks.
  • Olli Savolainen
    Mon, 26 Nov 2018, 6:17 PM
    Oh, admin/tool apparently. So many plugin types... https://docs.moodle.org/dev/Plugin_types :D
  • Purple and green cubism
    Wed, 28 Nov 2018, 9:25 PM
    Any plans for 3.5+? The basic tables desperately need a rework
  • Bernard Widjanarko
    Mon, 6 July 2020, 6:24 PM
    Hello Sir .. I have two separate datatables in one moodle page .. following the samples provided :

    $params = array("select" => true, "paginate" => false);
    $params['buttons'] = array("selectAll", "selectNone");
    $params['dom'] = 'lfiprt';
    $params['debug'] = true;
    $selector = '.datatable';
    $PAGE->requires->js_call_amd('tool_datatables/init', 'init', array($selector, $params));
    $PAGE->requires->css('/admin/tool/datatables/style/dataTables.bootstrap.css');
    $PAGE->requires->css('/admin/tool/datatables/style/select.bootstrap.css');
    ...
    $PAGE->requires->js_call_amd('tool_datatables/init', 'init', array($selector, $params));
    echo $renderer->display_results(array('results' => $usersa)); // display first datatables
    echo $renderer->display_summaries(array('summaries' => $usersb)); // display second datatables
    .. etc ..
    It is working fine, the issue is both datatables are controlled by the init.js which is called in $PAGE->requires->js_call_amd ..

    How can i create 2 datatables which i can control separately ?

    Any valuable input will be appreciated ..

    Thanks,
    Bernard
Please login to post comments