New Metronic Docs!Added Integration Docs with Starter Kits Apps for Laravel, Laravel Livewire, Angular, Vue, Symfony, Blazor Server, Django & Flask & more
Browse Docs

Metronic 9.2.0 header and data table issue


Video Link :


Datatable will jump to top when we scroll to bottom. I think is due to the sticky header part.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (13)


Hi

//drive.google.com/file/d/1Chl7EvcmYQ2FmrmWicSa3fIu9R_OMfOy/view?usp=sharing

Could you pleae try with this updated ktui.js?

Place it somewhere in your Metronic v9 folder

Then you have to rebuild Metronic assets by updating this library config:
/metronic-tailwind-html/webpack.vendors.js


ktui: [
{
src: ["node_modules/@keenthemes/ktui/dist/ktui.min.js"],
dist: "/vendors/ktui/ktui.min.js",
},
],


Update the path to match with new ktui.js, instead of from node_modules.

Then rebuild the Metronic assets using npm run build.

Let me know if you also need the file Metronic assets (/metronic-tailwind-html/dist/assets)

Hopefully, it will be fixed. We will include the fix in the upcoming updates.

I added to watch for DOM changes affecting layout, triggering refresh() as needed

Thanks



hii, any update on this issue?



Hi

Do you have a public URL so we can check it further?

Thanks



hiii

u try to view at here

please add https:// in front

ams-demo.gamifierlabs.com/en/test



Hi

The problem probably is a race condition. The datatable initializes and fires first "kt.datatable.drew" event before script gets to run and attach listener. script misses the first event, and the sticky header isn't updated on the initial page load.


document.addEventListener("DOMContentLoaded", () => {
console.log("DATATABLE FIX (v2): Initializing sticky header fix...");

const headerElement = document.querySelector("#header");
const datatableElement = document.querySelector("[data-kt-datatable="true"]");

if (!datatableElement || !headerElement) {
console.error("DATATABLE FIX (v2): Could not find datatable or header element.");
return;
}
console.log("DATATABLE FIX (v2): Found datatable and header elements.");

const sticky = KTSticky.getOrCreateInstance(headerElement);

if (!sticky) {
console.error("DATATABLE FIX (v2): Could not get or create KTSticky instance.");
return;
}
console.log("DATATABLE FIX (v2): Sticky instance ready.");

// --- The Fix ---
// Check if the datatable has already been initialized.
// The "datatable-initialized" class is added after the first draw.
if (datatableElement.classList.contains("datatable-initialized")) {
console.log("DATATABLE FIX (v2): Datatable was already initialized. Manually calling sticky.update() once to fix initial state.");
sticky.update();
}

// Always attach the event listener for subsequent redraws (pagination, sorting, etc.)
datatableElement.addEventListener("kt.datatable.drew", function() {
console.log("DATATABLE FIX (v2): "kt.datatable.drew" event fired. Calling sticky.update().");
sticky.update();
});

console.log("DATATABLE FIX (v2): Event listener for "kt.datatable.drew" is now active for future redraws.");
});


Please replace previous versions of the script with this one. This will resolve the race condition. Hopefully it will be fixed.

Thank you for your patience.



hii, after replacing latest code, it still the same.

this line is not triggered

console.log("DATATABLE FIX (v2): "kt.datatable.drew" event fired. Calling sticky.update().");


here is my latest console

DATATABLE FIX (v2): Initializing sticky header fix...
DATATABLE FIX (v2): Found datatable and header elements.
DATATABLE FIX (v2): Sticky instance ready.
DATATABLE FIX (v2): Datatable was already initialized. Manually calling sticky.update() once to fix initial state.
DATATABLE FIX (v2): Event listener for 'kt.datatable.drew' is now active for future redraws.



Hi

I've added several console.log messages. If it still doesn't work, the output in your browser's developer console will tell us exactly where the breakdown is.


document.addEventListener("DOMContentLoaded", () => {
console.log("DATATABLE FIX: Initializing sticky header fix...");

// Using the corrected header selector for Demo 2
const headerElement = document.querySelector("#header");
const datatableElement = document.querySelector("[data-kt-datatable="true"]");

if (!datatableElement) {
console.error("DATATABLE FIX: Datatable element not found. Please check the selector `[data-kt-datatable="true"]`.");
return;
}
console.log("DATATABLE FIX: Datatable element found:", datatableElement);

if (!headerElement) {
console.error("DATATABLE FIX: Header element not found. Please check the selector `#header`.");
return;
}
console.log("DATATABLE FIX: Header element found:", headerElement);

// Use getOrCreateInstance to safely get the instance or create it if it doesn"t exist yet.
// This resolves potential timing issues.
const sticky = KTSticky.getOrCreateInstance(headerElement);

if (sticky) {
console.log("DATATABLE FIX: Successfully found or created KTSticky instance:", sticky);

datatableElement.addEventListener("kt.datatable.drew", function() {
console.log("DATATABLE FIX: "kt.datatable.drew" event fired. Calling sticky.update().");
sticky.update();
});

console.log("DATATABLE FIX: Event listener for "kt.datatable.drew" is now active.");

} else {
console.error("DATATABLE FIX: CRITICAL - Failed to get or create a KTSticky instance. The header element may be missing the `data-kt-sticky=\"true\"` attribute which is required for initialization.");
}
});


This uses the KTSticky.getOrCreateInstance() method from KTSticky to the sticky component instance is always available. It then hooks into the kt.datatable.drew event from KTDataTable to trigger the sticky.update() method.

- Replace the old script with this new one.
- Open your browser's developer console (F12 or Cmd+Opt+J).
- Load the page and test the datatable pagination.
- You should see the "DATATABLE FIX" messages in your console. If the problem is still not fixed, please share any error messages or the full log output with me.



hiii this is my console

DATATABLE FIX: Initializing sticky header fix...

DATATABLE FIX: Datatable element found: <div data-kt-datatable=​"true" data-kt-datatable-page-size=​"100" id=​"formula_table" data-kt-datatable-initialized=​"true" class=​"datatable-initialized">​…​</div>​

DATATABLE FIX: Header element found: <header class=​"flex items-center shrink-0 bg-background h-(--header-height)​" data-kt-sticky=​"true" data-kt-sticky-class=​"transition-[height]​ fixed z-10 top-0 left-0 right-0 backdrop-blur-md bg-white/​70 border-b border-border" data-kt-sticky-name=​"header" data-kt-sticky-offset=​"200px" id=​"header" data-kt-sticky-initialized=​"true">​…​</header>​flex

DATATABLE FIX: Successfully found or created KTSticky instance: t {_dataOptionPrefix: 'kt-', _uid: 'sticky1127136697941', _element: header#header.flex.items-center.shrink-0.bg-background.h-(--header-height), _name: 'sticky', _defaultConfig: {…}, …}

DATATABLE FIX: Event listener for 'kt.datatable.drew' is now active.

this part of console.log is not triggered

datatableElement.addEventListener("kt.datatable.drew", function() {
console.log("DATATABLE FIX: '.datatable.drew' event fired. Calling sticky.update().");
sticky.update();
});

please add https:// in front to view the image

ibb.co/66wTH7P



Hi

This occurs when paginating to a page with very few rows, which reduces the total height of the page and conflicts with the sticky header's positioning logic.

To resolve this, you need to add a small JavaScript snippet to your page. This script will ensure the sticky header repositions itself correctly every time the datatable is redrawn.

Add the following code to a script tag on the relevant page, or include it in your site's custom JavaScript file:


document.addEventListener("DOMContentLoaded", () => {
// Select the datatable and the sticky header elements
const datatableElement = document.querySelector("[data-kt-datatable="true"]");
const headerElement = document.querySelector("#kt_header");

// Ensure both elements exist before proceeding
if (!datatableElement || !headerElement) {
console.warn("Datatable or Header element not found. The sticky header fix will not be applied.");
return;
}

// Get the KTSticky instance for the header
const sticky = KTSticky.getInstance(headerElement);

if (sticky) {
// When the datatable finishes redrawing, update the sticky header"s position
datatableElement.addEventListener("kt.datatable.drew", function() {
sticky.update();
});
} else {
console.warn("Could not find KTSticky instance for the header element.");
}
});


The KTDataTable component fires a kt.datatable.drew event upon completion, and the KTSticky component has a public update() method to recalculate its position. This script connects the two for interaction.

Thanks



hi i have try with your code, but it seem not working also.

const headerElement = document.querySelector("#kt_header");

In demo2, there the header id should be "header" rather than "kt_header", i already change to header but it seem like not working also. i tried to console log headerElement and datatableElement, it seem like the code able to select this two element. but when come to sticky.update(), this part of code is not working.



Hi,

Sorry for the late reply. May I know which demo are you using ? Can you paste here the URL from the preview site of the page where we can see this issue ? We allow only verified and internal URL posting. Otherwise spammers post multiple contents with spam urls.

Regards,
Sean



hi, i am using demo2. preview site don have such issue, because page content is too long. this problem only occur when the 1st page datatable cannot finish display data and move to the second page with only 1 or 2 row of data. When move to second page, the header should stick on the top, but due to the datatable only extra 1 or 2 row of data, the header cannot stick on top. so when u scroll to most bottom, it will cause the page shaking.



sorry i duno how to post the link. so i just post like this. Please add https:// in front

telemarketing.sgp1.cdn.digitaloceanspaces.com/280_1750515877.mp4


Deleted comment
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(