jrief / django-formset

The missing widgets and form manipulation library for Django
https://django-formset.fly.dev/
MIT License
315 stars 29 forks source link

Selectize programmatic value selection. #133

Open amikphoto opened 4 months ago

amikphoto commented 4 months ago

Is it possible to programmatically select and set a value in the Selectize field?

I have a form with Selectize field. I added a button near this field. When I press this button I call the form for adding a new item to a base table, which is set as foreign_key in the Selectize field. The form is called modally. After closing the form, the new element is added to the base and I would like to set this element as selected in the Selectize field.

Could you please advise me the way to solve the problem?

jrief commented 4 months ago

In version 1.4 there is full support for dialog forms. There the use-case you're describing is not handled but it seems interesting enough to be implemented.

jrief commented 4 months ago

I will implement this soon. Have been busy with releasing version 1.4.

amikphoto commented 4 months ago

wow :) Great! thank you!

jrief commented 3 months ago

In version 1.4 the Selectize web component offers a getter and setter function for value. Please try them out.

amikphoto commented 3 months ago

Great! I'll try it tonight. Thank you very much! you're the best! :)

amikphoto commented 3 months ago

is there any documentations how to use setters/getters?

jrief commented 3 months ago

Just find the original HTMLSelectElement in the DOM using querySelector or getElementById.

Then you can just set and get its value. Internally this then is retrieved/applied to the TomSelect component, which is doing the heavy lifting.

amikphoto commented 3 months ago

I see. Ok. Thank you.

amikphoto commented 3 months ago

I've just had a chance to test this. It works fine when all list of selected values are uploaded to a browser. If I added new item in the backend and set the value of select in frontend it doesn't work. Could you make update selected options?

jrief commented 3 months ago

I'm glad that you ask.

This feature has been implemented in version 1.5 – you may have a look at the develop-branch. There are still some issues in 1.5, hence I havn't released it yet.