Autoselect option if only one is available

I am trying to add an optimization to our workflow, and would like to automatically select the option in the dropdown as soon as there is only one available. The options are being fetched via Ajax.

I’ve hit a couple snags in my attempts, so perhaps these simpler questions might get me moving in the right direction.

  1. Is there any available event fired when the Ajax results are returned?
  2. Is there any way to change any of the Ajax options after the widget is initialized?

Thanks for your help!

Dan

Realized I was unclear about changing “Ajax options”. I mean things like the processResults function, not the selection “options” returned via Ajax.

AFAIK, it’s not possible to change the processReuslts callback once you’ve initialized the Select2. And it does not fire an event when results are returned (since the processResults callback already knows that).

Why not just put the logic you need in your processResults callback? You can check how many results were received, and if there’s only one you can programmatically select it.

Thanks for the reply! I am using the django-select2 module to provide my widgets, and it is opinionated on what processResults is supposed to do. I am trying to not deviate too far from stock in that way for maintenance reasons, so ideally I could let django-select2 do the initialization, then override processResults as I need. Is there a way to change processResults after the widget is initialized?

So sorry, read your reply too quickly and missed the obvious answer to my latest question. I’ll probably leave things as-is, but return to this once I’m a little more confident the project this widget is a part of is settled.

For any select2 devs, a way to set/update configuration outside of the initialization would be a nice enhancement.

Thanks again.