Open iragm opened 1 year ago
Put a setTimeout of like 100/200, then call select2() with ajax settings
it's the same issue like https://github.com/yourlabs/django-autocomplete-light/issues/1221 atm the only option is to trigger a page load event to force an initialization of dal
That is the code that's supposed to initialize new autocompletes: https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal/static/autocomplete_light/autocomplete_light.js#L199-L221
Is it not triggering with HTMX?
Would be great to know if using the system in alight_new branch (see PR #1330) does indeed work with HTMX
just tested it with current version of alight_new branch (3.9.7 87dcae9) and it doesn't initialize. atm i still do this "manually" in the snippet that's getting loaded by htmx
htmx.on("htmx:afterSettle", (e) => {
setTimeout(() => {
if (!dal_init) {
window.dispatchEvent(new Event('load'));
$('.js-services-multiselect').select2();
dal_init = true;
}
}, 200);
})
for some reason it's sufficient to run this the very first time the snippet is loaded, every subsequent load of the same snippet will be initialized by then
hello, I managed to get everything work with original autocomplete_light.js using the script provided by @ideesnoires in his post, but slightly modified, removing dal_init. The script must be added to the modal content template
htmx.on("htmx:afterSettle", (e) => {
setTimeout(() => {
window.dispatchEvent(new Event('load'));
$('.js-services-multiselect').select2();
}, 200);
});
The only problem that I see now - every modal window generates dozen of "The DAL function "select2" has already been registered" errors, not sure if it possible to do something with that.
@foggy54 that's why I used the dal_init variable. It's getting initialized while loading a view and avoiding the multiple select2() calls.
@ideesnoires sorry, I can't understand where and how you define variable dal_init? I'm getting error ReferenceError: dal_init is not defined
@foggy54 well before any htmx code is getting called I initialize this variable with a simple:
var dal_init = false;
🙃
@ideesnoires I tried this (code below, just checking if variable is defined), also works perfectly, thx for advice!
``` htmx.on("htmx:afterSettle", (e) => {
setTimeout(() => {
if (typeof dal_init === 'undefined') {
window.dispatchEvent(new Event('load'));
$('.js-services-multiselect').select2();
dal_init = true;
}
}, 200);
});
First - great project, wonderful documentation!
I am using dal with HTMX to create an autocomplete field in a modal window. (HTMX loads the modal window from a different View/url and appends it to the current page.) When I visit the HTMX URL directly, everything works fine. But, when I use HTMX to append that same working page to my existing page's DOM, the dropdown menu isn't initializing correctly.
I see a bunch of stuff on Stack Overflow that references this kind of issue, but it's always (?) caused by forgetting lo load Javascript or form tags. I also see #1270 which is the only other reference to HTMX, but this seems to be a bit different than both of those.
I have a feeling that stuff is getting loaded in the wrong order - that is, HTMX dumps the scripts into the DOM, and they execute on the page before the form has a chance to initialize. I believe this because the form works the second time it's loaded, or the third time if I mess with the order the libraries are loaded in. I am new to HTMX, so it's possible the PEBKAC.
I was able to hack together a very ugly but functioning solution as follows. In the template that my HTMX view returns, I added jquery:
Then, I hacked autocomplete_light.js to split the load event listener into a separate function so I could call it manually:
Finally, I added this snippet to autocomplete_light.js to call my newly created function:
This works, every time, as far as I can tell. I have already invested too much time in this to spend more time figuring out the root cause and putting together a PR, but maybe this will help someone else who is using this with HTMX (most likely me after I upgrade DAL in a year or two and my hacks break).
Here is my complete autocomplete_light.js file: