<link rel="stylesheet"
href="
https://cdn.datatables.net/2.1.8/css/dataTables.dataTables.min.css" />
<script src="
https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<style type="text/css">
.dt-demo-scope { box-sizing: border-box; max-width: 100%; padding: 8px 0; }
.dt-demo-scope table.dataTable tbody td { font-size: 14px; }
</style>
<div id="dt-shadow-demo-root" class="dt-demo-scope">
<h2 style="margin:0 0 12px; font-size:1.1rem">Sample table (DataTables + shadow root)</h2>
<table id="dt-demo-table" class="display compact" style="width:100%">
<thead>
<tr><th>Name</th><th>Role</th><th>Office</th></tr>
</thead>
<tbody>
<tr><td>Alice</td><td>Architect</td><td>London</td></tr>
<tr><td>Bob</td><td>Engineer</td><td>New York</td></tr>
<tr><td>Carol</td><td>Designer</td><td>Tokyo</td></tr>
<tr><td>Dan</td><td>Analyst</td><td>Berlin</td></tr>
</tbody>
</table>
</div>
<script type="text/javascript">
(function () {
"use strict";
/* Change these IDs if you duplicate the example on one page */
var MOUNT_ID = "dt-shadow-demo-root"; // outer wrapper the script searches for
var TABLE_ID = "dt-demo-table"; // the <table> DataTables will enhance
/* Retry budget — Angular sometimes paints the panel after the script runs */
var MAX_TRIES = 40;
var TRY_MS = 100;
// Inside UDB: use the dashboard host's shadow root.
// Plain browser test: fall back to document so the demo still runs.
function resolveShadowOrDocument() {
var host = document.querySelector("app-dashboard-builder");
if (host && host.shadowRoot) return host.shadowRoot;
return document;
}
function getMount() {
var root = resolveShadowOrDocument();
if (root && typeof root.getElementById === "function") {
return root.getElementById(MOUNT_ID);
}
return null;
}
// Find the table INSIDE the wrapper — never rely on document alone.
function getTableEl() {
var mount = getMount();
if (!mount) return null;
if (typeof mount.querySelector === "function") {
return mount.querySelector("#" + TABLE_ID);
}
return null;
}
// Turn the plain table into a DataTable (once).
function initOnce() {
if (window.__dtDemoInited) return true;
var table = getTableEl();
if (!table) return false;
var DT = window.DataTable;
if (typeof DT !== "function") {
console.warn("[DT shadow demo] DataTable constructor not found (script order?)");
return false;
}
try {
new DT(table, {
paging: true,
pageLength: 3,
ordering: true,
searching: true,
info: true
});
window.__dtDemoInited = true;
try { console.info("[DT shadow demo] initialized on #" + TABLE_ID); } catch (_e) {}
return true;
} catch (err) {
console.error("[DT shadow demo] init failed", err);
return true;
}
}
// Try now, then retry — friendly for slow dashboard rendering.
function schedule() {
var n = 0;
(function tick() {
n++;
if (initOnce() || n >= MAX_TRIES) return;
setTimeout(tick, TRY_MS);
})();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", schedule);
} else {
schedule();
}
})();
</script>