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

Advanced Select not displayed properly in drawer


Hi,

I am trying to use Advanced Select (Metronic Tailwind) within a drawer. I have a form that contains the select element. The content of the form is injected using ajax. When I place the select code inside the main page it works great, but when the form gets loaded inside the drawer it doesn't. I tried the sample select from the documentation as well:

<select class="kt-select"
data-kt-select="true"
data-kt-select-enable-search="true"
data-kt-select-search-placeholder="Search..."
data-kt-select-placeholder="Zgjidh furnitorin..."
data-kt-select-config='{
"optionsClass": "kt-scrollable overflow-auto max-h-[250px]"
}'>
<option value="react">React</option>
<option value="nextjs">Next.js</option>
<option value="angular">Angular</option>
<option value="vue">Vue</option>
<option value="svelte">Svelte</option>
<option value="ember">Ember</option>
<option value="nuxt">Nuxt.js</option>
<option value="remix">Remix</option>
</select>


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 (5)


Thanks! Really appreciate your sharing the solution with everyone.



Hi Faizal,

I got the error:

TypeError: Cannot read properties of undefined (reading 'querySelector')

However, I put the following code in the main javascritp file and all is good:

["mousedown", "click"].forEach(evt => {
document.addEventListener(evt, function (e) {
if (e.target.matches('input[data-kt-select-search="true"]')) {
e.stopImmediatePropagation();
return;
}
}, { capture: true });
});

Hopefully it will be helpful for someone else as well.

Thank you for your support!



Hi

This could be event handling conflicts or focus management problems.

Please try with this code


drawerBody.querySelectorAll("select.kt-select[data-kt-select]").forEach(sel => {
sel.setAttribute("data-kt-select-dropdown-container", "body");
const select = new KTSelect(sel, {
dropdownContainer: "body",
enableSearch: true
});

// Prevent the search input from closing the dropdown
const searchInput = select.element.querySelector("input[type="text"]");
if (searchInput) {
searchInput.addEventListener("click", (e) => {
e.stopPropagation();
});
searchInput.addEventListener("focus", (e) => {
e.stopPropagation();
});
}
});


If these approaches don't resolve the issue, please check your browser's console for any JavaScript errors

Thanks



Hi Faizal,

Thanks for the answer. After I load the drawer content using ajax, I set the attribute as you suggested and now the dropdown works fine.

drawerBody.querySelectorAll('select.kt-select[data-kt-select]').forEach(sel => {
sel.setAttribute('data-kt-select-dropdown-container', 'body');
new KTSelect(sel, {
dropdownContainer: 'body',
enableSearch: true
});
});

However, when I want to search, the moment I click the search section the dropdown disappears.

Thanks,



Hi

This is a known issue with KTSelect components in hidden containers. The problem occurs because the dropdown gets clipped or hidden by the parent container's overflow styles.

Use the dropdownContainer: 'body' option - the dropdown will be appended to the document body instead of the parent container, preventing visibility issues.


data-kt-select-dropdown-container="body"


Thanks


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  :(