Last active
November 19, 2016 22:15
-
-
Save scottoffen/5bee51d77092576281fa to your computer and use it in GitHub Desktop.
Apply the DataTables jQuery plugin to any table on a website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
}; | |
}; | |
}; |
Scott you are my hero!!!!
I still think you need to make this available as a Pintrest-ish style Chrome Extension!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be aware that this is a potentially destructive hack as it injects jQuery 2.1.3 and and the DataTables 1.10.6 javascript and stylesheet to the page, even if they are already there.
The
dt_options
variable sets the options for creating the datatable.The
options
variable hold a few useful variables in one place:id
to add to the table if the table doesn't already have an id