<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Using_jquery_datatables_in_moodle</id>
	<title>Using jquery datatables in moodle - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Using_jquery_datatables_in_moodle"/>
	<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Using_jquery_datatables_in_moodle&amp;action=history"/>
	<updated>2026-05-07T18:42:47Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Using_jquery_datatables_in_moodle&amp;diff=10383&amp;oldid=prev</id>
		<title>Олег Давидович: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Using_jquery_datatables_in_moodle&amp;diff=10383&amp;oldid=prev"/>
		<updated>2024-10-18T07:00:08Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 07:00, 18 октября 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Олег Давидович</name></author>
	</entry>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Using_jquery_datatables_in_moodle&amp;diff=10382&amp;oldid=prev</id>
		<title>1&gt;Kl3b4n: correct image</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Using_jquery_datatables_in_moodle&amp;diff=10382&amp;oldid=prev"/>
		<updated>2021-05-26T17:45:44Z</updated>

		<summary type="html">&lt;p&gt;correct image&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Introduction ==&lt;br /&gt;
Although Moodle has an excellent Table API, I have found myself preferring using the jQuery DataTables plugin in most of the Moodle plugins that I develop. In the past, I could load DataTables using the jquery plugins folder structure. That is, creating a folder in my plugin called jquery and then adding DataTable files in the folder and pointing to those files using an array within the plugins.php (see [https://docs.moodle.org/dev/jQuery_pre2.9 jQuery pre2.9]). However, since the arrival of AMD modules, I have yet to find a way to initialize DataTables with all features. I can get it to work with the base features, but not with extensions. If someone knows how to do it with AMD, please write some documentation on it :). Not wanting to loose the features for DataTables, I found another way to load it from CDN.&lt;br /&gt;
&lt;br /&gt;
== Using DataTables with AMD == &lt;br /&gt;
The way of using DataTable with AMD module that worked for me. Feel free to correct me.&lt;br /&gt;
&lt;br /&gt;
How to do it:&lt;br /&gt;
# Go to the [https://datatables.net/download/ DataTables download page ]&lt;br /&gt;
## &amp;quot;Step 1. Choose a styling framework&amp;quot; --&amp;gt; select Bootstrap 4&lt;br /&gt;
## &amp;quot;Step 2. Select packages&amp;quot; --&amp;gt; Select DataTables&lt;br /&gt;
## &amp;quot;Extensions&amp;quot; --&amp;gt; Select all extensions you want to use.&lt;br /&gt;
## &amp;quot;Step 3 Pick a download method&amp;quot; --&amp;gt; Select &amp;quot;Download&amp;quot; tab&lt;br /&gt;
## Download files with minify and concatenate&lt;br /&gt;
# Put &amp;#039;&amp;#039;&amp;#039;datatables.js&amp;#039;&amp;#039;&amp;#039; into &amp;quot;plugin/folder/amd/src&amp;quot; folder&lt;br /&gt;
# Put &amp;#039;&amp;#039;&amp;#039;datatables.min.js&amp;#039;&amp;#039;&amp;#039; into &amp;quot;plugin/folder/amd/build&amp;quot; folder&lt;br /&gt;
# Put &amp;#039;&amp;#039;&amp;#039;datatables.css&amp;#039;&amp;#039;&amp;#039; into &amp;quot;plugin/folder/styles&amp;quot; folder&lt;br /&gt;
# Add HTML table on your page:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
echo &amp;#039;&amp;lt;table id=&amp;quot;table1&amp;quot; class=&amp;quot;display&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;Column 1&amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;Column 2&amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;tbody&amp;gt;&lt;br /&gt;
    &amp;lt;/tbody&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&amp;#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&amp;lt;li value=&amp;quot;6&amp;quot;&amp;gt;Add &amp;#039;&amp;#039;&amp;#039;my_datatables.js&amp;#039;&amp;#039;&amp;#039; into &amp;quot;plugin/folder/amd/src&amp;quot;. My code looks like this:&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
import $ from &amp;#039;jquery&amp;#039;;&lt;br /&gt;
import &amp;#039;local_booking/datatables&amp;#039;;&lt;br /&gt;
&lt;br /&gt;
export const init = () =&amp;gt; {&lt;br /&gt;
    var data = [&lt;br /&gt;
        [&lt;br /&gt;
            &amp;quot;Tiger Nixon&amp;quot;,&lt;br /&gt;
            &amp;quot;System Architect&amp;quot;,&lt;br /&gt;
        ],&lt;br /&gt;
        [&lt;br /&gt;
            &amp;quot;Garrett Winters&amp;quot;,&lt;br /&gt;
            &amp;quot;Director&amp;quot;,&lt;br /&gt;
        ]&lt;br /&gt;
    ];&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        $(&amp;#039;#table1&amp;#039;).DataTable({&lt;br /&gt;
            data: data&lt;br /&gt;
        });&lt;br /&gt;
    });&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&amp;lt;li value=&amp;quot;7&amp;quot;&amp;gt;Require css and js before rendering of header&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;css(&amp;#039;/plugin/folder/styles/datatables.css&amp;#039;);&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_call_amd(&amp;#039;local_booking/my_datatables&amp;#039;, &amp;#039;init&amp;#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
If JS and styles are applied correctly without errors, it should look like this: https://i.imgur.com/s8sZdf1.png&lt;br /&gt;
&lt;br /&gt;
== Loading DataTables from CDN == &lt;br /&gt;
This is how I am loading and using DataTables in my plugins. This may not be the best way to do it, but it does work for me.&lt;br /&gt;
&lt;br /&gt;
Open the php file that contains the page that will be displayed. Make sure the global $PAGE is set. Then do the following:&lt;br /&gt;
&lt;br /&gt;
# Go to the [https://datatables.net/download/ DataTables download page ]&lt;br /&gt;
# Step 1 Choose a styling framework: select Bootstrap 4&lt;br /&gt;
# Step 2 Select Packages: Select DataTables&lt;br /&gt;
# Extensions: Select all extensions you want to use.&lt;br /&gt;
# Step 3 Pick a download method: Select CDN tab&lt;br /&gt;
# Copy the CSS link without the quotes (inside the href=&amp;quot;&amp;quot;)&lt;br /&gt;
# Paste as $PAGE-&amp;gt;requires-&amp;gt;css(new \moodle_url(&amp;#039;the_css_link_provided_by_datatables_that_you_copied&amp;#039;)); &amp;#039;&amp;#039;&amp;#039;Note the single quotes.&amp;#039;&amp;#039;&amp;#039; This is important because the CDN links already use double quotes. This simplifies the copy pasting by not having to escape all double quotes.&lt;br /&gt;
# Copy the JS link without the quotes (inside the src=&amp;quot;&amp;quot;)&lt;br /&gt;
# Paste as $PAGE-&amp;gt;requires-&amp;gt;js(new \moodle_url(&amp;#039;the_js_link_provided_by_datatables_that_you_copied&amp;#039;), true); &amp;#039;&amp;#039;&amp;#039;Note the single quotes.&amp;#039;&amp;#039;&amp;#039; as above.&lt;br /&gt;
# Create a JS file in a folder within your plugin. I usually call the folder js.&lt;br /&gt;
# Add $PAGE-&amp;gt;requires-&amp;gt;js(new \moodle_url(&amp;#039;path_to_your_new_js_file_created_above&amp;#039;));&lt;br /&gt;
&lt;br /&gt;
Your code should look something like this: I am using a plugin in the local folder called sample&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js(new \moodle_url(&amp;#039;https://cdn.datatables.net/v/bs4/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/b-print-1.6.1/cr-1.5.2/fc-3.3.0/fh-3.1.6/kt-2.5.1/r-2.2.3/rg-1.1.1/rr-1.2.6/sc-2.0.1/sp-1.0.1/sl-1.3.1/datatables.min.js&amp;#039;), true);&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;css(new \moodle_url(&amp;#039;https://cdn.datatables.net/v/bs4/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/b-print-1.6.1/cr-1.5.2/fc-3.3.0/fh-3.1.6/kt-2.5.1/r-2.2.3/rg-1.1.1/rr-1.2.6/sc-2.0.1/sp-1.0.1/sl-1.3.1/datatables.min.css&amp;#039;));&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js(new \moodle_url($CFG-&amp;gt;wwwroot . &amp;#039;/local/sample/js/my_datatables.js&amp;#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
If you are developing a block, then do the following:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;requires-&amp;gt;js(new \moodle_url(&amp;#039;https://cdn.datatables.net/v/bs4/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/b-print-1.6.1/cr-1.5.2/fc-3.3.0/fh-3.1.6/kt-2.5.1/r-2.2.3/rg-1.1.1/rr-1.2.6/sc-2.0.1/sp-1.0.1/sl-1.3.1/datatables.min.js&amp;#039;), true);&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;requires-&amp;gt;css(new \moodle_url(&amp;#039;https://cdn.datatables.net/v/bs4/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/b-print-1.6.1/cr-1.5.2/fc-3.3.0/fh-3.1.6/kt-2.5.1/r-2.2.3/rg-1.1.1/rr-1.2.6/sc-2.0.1/sp-1.0.1/sl-1.3.1/datatables.min.css&amp;#039;));&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;requires-&amp;gt;js(new \moodle_url($CFG-&amp;gt;wwwroot . &amp;#039;/blocks/sample/js/my_datatables.js&amp;#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your js file, add all of the tables that you will need DataTables for and configure each instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$(&amp;#039;#myFirstTable&amp;#039;).DataTable({&lt;br /&gt;
    dom: &amp;#039;Blfrtip&amp;#039;,&lt;br /&gt;
    buttons: [&lt;br /&gt;
        &amp;#039;copyHtml5&amp;#039;,&lt;br /&gt;
        &amp;#039;csvHtml5&amp;#039;,&lt;br /&gt;
    ]&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
$(&amp;#039;#mySecondTable&amp;#039;).DataTable({&lt;br /&gt;
    dom: &amp;#039;lrtip&amp;#039;,&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
$(&amp;#039;#myThirdTable&amp;#039;).DataTable();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>1&gt;Kl3b4n</name></author>
	</entry>
</feed>