Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning an autocomplete form dynamically #1352

Open
YasirKusay opened this issue Jan 31, 2024 · 8 comments
Open

Cloning an autocomplete form dynamically #1352

YasirKusay opened this issue Jan 31, 2024 · 8 comments

Comments

@YasirKusay
Copy link

Hello,

I am sorry for opening up an issue for this but I did not know where else to ask this.

Lets say I have this form:

class PostForm:
    tag = forms.ModelChoiceField(queryset=Tags.objects.all(), 
                                                  required=True, 
                                                  widget=autocomplete.ModelSelect2(url='tag_autocomplete', 'attrs': {'id: 'tag_1'}}))

In otherwords, it contains an autocomplete field that calls tag_autocomplete to fetch a possible list of results from the search. (In the actual form, there are more fields but I am trying to keep the code as simple as possible)

I also have a view that takes the above form and renders it in a html file like this:

<form>
    {{ post_form.tag }}
    <input type="submit">
<form>

How can I create a duplicate of the autocomplete field, for example, by clicking an “Add Additional Tag” button. Normally, you can just get a duplicate of an input element by cloning it and changing the new one’s ID/name before appending it to the html. However, this is not possible here as DAL does some additional rendering which adds additonal tags below the form element (I tried to understand how it works from the documentation, specifically the tutorial but I couldn’t).

@jpic
Copy link
Member

jpic commented Jan 31, 2024 via email

@YasirKusay
Copy link
Author

I prefer to keep separate autocomplete forms, rather than using a single autocomplete form. Is there another way?

@jpic
Copy link
Member

jpic commented Feb 1, 2024 via email

@YasirKusay
Copy link
Author

Sorry, I meant that I would like separate autocomplete fields.

@jpic
Copy link
Member

jpic commented Feb 1, 2024 via email

@YasirKusay
Copy link
Author

YasirKusay commented Feb 2, 2024

I tried that before creating this issue, but there were a few problems with this. I made a simple recreation of what I was originally trying to achieve:

I have an all_autocomplete_forms div that stores wrapperDiv divs that actually contains the autocomplete form. When you click the add_more button, it will duplicate an the individual element (via cloneNode), change attributes such as the name and id and insert it into a new wrapperDiv which in turn gets inserted back into all_autocomplete_forms. Please look at the output attached here:

error1 In the above error, I am highlighting the div containing the first autocomplete element (which is missing the span element). error2

In the second image, I am highlighting the div the div containing the second autocomplete element. This, along with any subsequent elements appears fine.

I also did a method where I cloned the entire wrapperDiv element itself and changed attributes inside it before inserting it back into all_autocomplete_forms. The results of this are identical.

@YasirKusay
Copy link
Author

Could you let me know if I can do anything differently?

@YasirKusay
Copy link
Author

Hi I just want to check in again. Could you let me know if I did anything wrong and if I can do anything differently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants