Class DataTablesMixin
/juneau-datatables.js.
Compose into a host resource via @Rest(mixins=DataTablesMixin.class); the
/juneau-datatables.js URL then becomes available alongside the host's own endpoints, so browser pages can
load the glue without the application hosting it itself.
Where the DataTables library comes from:
This module declares the jQuery and DataTables WebJars (org.webjars:jquery,
org.webjars.npm:datatables.net, datatables.net-dt, datatables.net-buttons,
datatables.net-buttons-dt) as dependencies; Juneau's own jars don't copy their files.
WebJarsMixin serves them from the classpath. This mixin serves
only the first-party juneau-datatables.js glue, which auto-initializes any server-rendered
<table data-juneau-datatable> (e.g. one built by DataTablesTable) by calling
$(table).DataTable(opts) once the library is present.
On a console page (juneau-console-freemarker), every <@card type="datatables">
requires the "datatables-glue" toolkit pack, which pulls jQuery and DataTables in before the glue. Compose
this mixin and WebJarsMixin into the host; the page never loads the library by hand. Outside a console
page, wire the assets yourself as below.
Wiring the page assets (caller-side @HtmlDocConfig):
Juneau's @HtmlDocConfig is resolved per endpoint / per
resource-class hierarchy — it is not contributed across a host's other endpoints by a mixin. So a mixin
cannot silently inject page assets into your data endpoints; instead, add the wire-points to the resource (or
op) that renders the HTML table, referencing your chosen DataTables distribution and this served glue. The URLs are
SVL-resolvable, so the DataTables location can be overridden per environment without code changes.
Example:
Server-side processing (JuneauDataTables.ajax):
For a large table, let DataTables fetch pages from the server instead of rendering every row. The served glue
exposes ajax option that POSTs the
server-side request as JSON to a @RestOp(method="POST") endpoint taking @Content
DataTablesRequest — see DataTablesQuery for
the endpoint side:
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script> <script src="/releases/juneau-datatables.js"></script> <table id="releases"></table> <script> new DataTable('#releases', { serverSide: true, ajax: JuneauDataTables.ajax('/releases/query'), columns: [{data: 'name', title: 'Name'}, {data: 'version', title: 'Version'}] }); </script>
A server-rendered DataTablesTable can skip the inline <script> entirely: give it the
DataTablesTable.AJAX_ATTR attribute (e.g. via a direct Table DOM edit after
DataTablesTable.of) and the glue wires
opts.ajax/opts.serverSide automatically on load.
Mixin-only deployment:
This resource is designed for composition via @Rest(mixins=...). The mount path is pinned at the op level by
@RestGet(path="/juneau-datatables.js") on getGlueScript(); a class-level
@Rest(paths=...) declaration would be silently ignored under the mixin pattern. WebJarsMixin follows the same
pattern for /webjars/*.
See Also:
- Since:
- 10.0.0
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription[GET /juneau-datatables.js] — serve the first-party DataTables glue script.
-
Field Details
-
GLUE_PATH
The URL path at which the first-party glue script is served (relative to the host mount).- See Also:
-
-
Constructor Details
-
DataTablesMixin
public DataTablesMixin()
-
-
Method Details
-
getGlueScript
[GET /juneau-datatables.js] — serve the first-party DataTables glue script.- Returns:
- The glue script as a JavaScript
HttpResource. - Throws:
IOException- If the shipped glue resource could not be read (effectively unreachable — the resource is shipped in the same jar as this class).
-