2

I am using Select2 control with knockout and I have a special requirement where I need to set the minimumInputLength option of the Select2 control dynamically based on the length of the options in the select2 combobox. For example if I have 100 options, I need to set the minimumInputLength to 2 and if its less than 100, I need to set it to nothing (or 0).

I tried the following options. select2Combobox here is a custombinding I created.

1) Have a ko Computed Property (bound to the minimumInputLength setting) which will return 2 or 0 based on the length of the observableArray that the select2 is bound to. Here TypeAheadMinInputLength is my computed property.

select2Combobox: { select2Options: { width: '100%', minimumInputLength: TypeAheadMinInputLength}, data: TypeaheadData}

2) Bind the minimumInputLength setting with a function call which again returns 2 or 0 based on the length of input array that the function is provided with.

select2Combobox: { select2Options: { width: '100%', minimumInputLength: getTypeAheadMinInputLength(TypeaheadData())}, data: TypeaheadData}

Any suggestions

Thanks.

Krishna Veeramachaneni
  • 2,131
  • 3
  • 18
  • 24

1 Answers1

0

I don't know it this can help, but you can dynamicaly update the value of minimumInputLength for a given select2 by updating this internal dict. it is hackish, but it work:

$('#id_of_select').data("select2").opts.minimumInputLength = 0

so, it won't be select2 that will compute the value of minimumInputLength, but you can add a change event to you form and update the select2 widget each time it is needed

ornoone
  • 651
  • 5
  • 11