django-formset icon indicating copy to clipboard operation
django-formset copied to clipboard

Selectize programmatic value selection.

Open amikphoto opened this issue 1 year ago • 10 comments

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?

amikphoto avatar Apr 13 '24 16:04 amikphoto

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 avatar Apr 14 '24 09:04 jrief

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

jrief avatar May 01 '24 20:05 jrief

wow :) Great! thank you!

amikphoto avatar May 02 '24 05:05 amikphoto

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

jrief avatar May 07 '24 07:05 jrief

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

amikphoto avatar May 08 '24 06:05 amikphoto

is there any documentations how to use setters/getters?

amikphoto avatar May 08 '24 06:05 amikphoto

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.

jrief avatar May 08 '24 06:05 jrief

I see. Ok. Thank you.

amikphoto avatar May 08 '24 07:05 amikphoto

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?

amikphoto avatar May 20 '24 17:05 amikphoto

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.

jrief avatar May 21 '24 06:05 jrief