Skip to content

Instantly share code, notes, and snippets.

@sineld
Forked from scottoffen/add-datatables.js
Created November 19, 2016 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sineld/1a8f2c2c17209476fd7f0ca5a6802c49 to your computer and use it in GitHub Desktop.
Save sineld/1a8f2c2c17209476fd7f0ca5a6802c49 to your computer and use it in GitHub Desktop.
Apply the DataTables jQuery plugin to any table on a website
var options = { class : "wikitable", idx : 0, id : "firstWikiTable" };
var dt_options = { paging: false, scrollY: 400 };
var jscript = document.createElement("script"); jscript.src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"; document.body.appendChild(jscript);
jscript.onload = function ()
{
var script = document.createElement("script"); script.src="https://cdn.datatables.net/1.10.6/js/jquery.dataTables.min.js"; document.body.appendChild(script);
script.onload = function ()
{
var css = document.createElement("link"); css.rel="stylesheet"; css.href="https://cdn.datatables.net/1.10.6/css/jquery.dataTables.min.css"; document.body.appendChild(css);
css.onload = function ()
{
var table = $("." + options.class)[options.idx];
if (table.id)
{
options.id = table.id;
}
else
{
table.setAttribute("id", options.id);
}
var thead = table.createTHead();
thead.appendChild($(table).find("tbody tr:first-child")[0]);
var dtable = $("#" + options.id).dataTable(dt_options);
console.log("BAM!");
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment