Select2 minimumResultsForSearch

It seems the generated code for Select2 in v2022 is different with v2021. For v2022, we need to adjust the setting option from Client Scripts of the current page. We cannot setup that option from global Client Scripts anymore. Please correct me if I’m wrong.I put this code in Client Scripts → Global → Pages with header/footer → Client Script:ew.selectOptions.minimumResultsForSearch=2;after that re-generate ALL the script files again, and I saw that code was added into js/userevent.js file in v2022, but it does not work for the Select2 control. The Search textbox does not appear when we click on the Select2 control, and I have double checked, the number of options in that Select2 is more than two items.

That is because the Select2 controls may already be initiated when the global Client/Startup Script is fired. It is better to set these global settings in Page_Head server event, e.g.

<script>
ew.selectOptions.minimumResultsForSearch = 2;
</script>

Note that it is only global setting. There is also field level setting, if “Requires search” not enabled, minimumResultsForSearch will be set to Infinity to disable searching in Select2.

I know that setting, and I obvisouly don’t want to enable it. Look…, in v2021, setting up the minimumResultsForSearch from global Client Scripts is working properly, it will apply to all Select2 control. But it does not work in v2022. That’s the main problem here.

  1. From source code, Select2 controls were initiated on “head” event in v2021. In v2022, they are initiated earlier as soon as the form is initiated (after the “” event), so you should set global settings in Page_Head server event in v2022.
  2. As said if “Requires search” not enabled, field level minimumResultsForSearch will be set to Infinity to disable searching in Select2. There will be no search box at all. To use search (and minimumResultsForSearch) you should enable it.

arbei wrote:

so you should set global settings in Page_Head server event in v2022.

As I mentioned before, I did already, and it does not work. Please try it by yourself, and you will see what I meant. I will explain it below.arbei wrote:

To use search (and minimumResultsForSearch) you should enable it.

If I enable Requires search option, then the Search textbox will appear when we click on Select2 control in the generated web app, but… there are no option values displayed below that Search textbox, until we type one or two characters in it that match with the available options, then the option values will be displayed. Unfortunately, that’s not we want, actually.Here we want. We want to obviously keep disable Requires search option in v2022, but also we want to keep display the available option values and display the Search textbox when we click on Select2 control, as well as in v2021.Let me explain you in more detail, by giving the step-by-step both for v2021 and v2022.In v2021, please do the following steps:

  1. Disable Requires search option from Fields setup,
  2. Put this code in Client Scripts → Global → Pages with header/footer → Client Script:
ew.selectOptions.minimumResultsForSearch=2;
  1. Re-generate ALL the script files
  2. Open the generated web app via browser, and click on the Select2 control, you will see a Search textbox displayed, following with the available options in it.
  3. That means, setting up ew.selectOptions.minimumResultsForSearch from global Client Script is working properly as we want.

In v2022, please do the following steps:

  1. Disable Requires search option from Fields setup,
  2. Put this code in Server Events → Global → All Pages → Page_Head:
<script>
ew.selectOptions.minimumResultsForSearch = 2;
</script>
  1. Re-generate ALL the script files
  2. Open the generated web app via browser, and click on the Select2 control, there are no Search textbox displayed
  3. That means, setting up ew.selectOptions.minimumResultsForSearch in Page_Head in v2022, will not work as well as in v2021.

If Ajax, there is no search box. The text input is for initiating the Ajax to search records from the server side, see Ajax (remote data), it is not same as the search box for filtering existing options (an JavaScript array). Select2 does not have a search box for further filtering results loaded by Ajax.If not Ajax, e.g. User Values or lookup cached enabled, the options are a JavaScript array (see Loading array data), the search box (if “Requires search” enabled) appears if the number of options is larger than the minimumResultsForSearch, see Limiting display of the search box to large result sets.If “Requires search” is not enabled, the search box is hidden, see Hiding the search box. In v2022, the minimumResultsForSearch option is set to Infinity to hide the search box. Since it is a field level setting (not global or page level), it is only set when the field’s Select2 is initiated.

As explained (see previous post), you cannot set the global setting “minimumResultsForSearch” in Page_Head server event in v2022 because it is set to Infinity by the field level setting “Requires search” (which did not exist in v2021) if it is not enabled.So if you want to show the search box for filtering options, you may:

  • Enable “Requires search” (so search box is enabled)
  • Use User Values or lookup cached enabled or server event to set options (so the options are in an array, not retrieved by Ajax)
  • Set the minimumResultsForSearch to your value (so the search box will appear if the number of options is larger than the value)

In v2021 you were able to type and search in a Select field and this would filter the list for you (it was just there as standard no additional settings).Now in v2023 that option has disappeared and only is there if you select “Requires search” (however, this does not show any results until you start typing).

arbei wrote:

  • Enable “Requires search” (so search box is enabled)
  • Use User Values or > Use lookup cache > or server event to set options (so the options are already available in an array on page load, not retrieved by Ajax)
  • Set the > minimumResultsForSearch > to your value (so the search box will appear if the number of options is larger than the value)

If you want to show results without starting typing, you may try above and set the follows in Page_Head server event:

<script>
ew.selectMinimumInputLength = 0; // Minimum number of characters required to start a search
ew.selectOptions.minimumResultsForSearch = 0; // Minimum number of results required to display the search box
</script>

To enable lookup cache for Add/Edit pages also, see Lookup cache enabled pages. (v2023)

Finally, it works by enabling Use lookup cache option from Tools → Advanced Settings, and adding that code in Page_Head server event.Thanks!