Replace the option groups HTML for select2 in one chunk to improve performance when...
authorRomain Thouvenin <romain@wemove.eu>
Tue, 9 Mar 2021 15:07:02 +0000 (16:07 +0100)
committerRomain Thouvenin <romain@wemove.eu>
Tue, 9 Mar 2021 15:54:38 +0000 (16:54 +0100)
js/Common.js

index 8a5306763778e4b27059c4a0e07e57a9bd870545..e5754fcfe09bd07512ffbb12771af1bb10e2cf73 100644 (file)
@@ -418,11 +418,24 @@ if (!CRM.vars) CRM.vars = {};
           formatResult: formatCrmSelect2,
           formatSelection: formatCrmSelect2
         };
+
       // quickform doesn't support optgroups so here's a hack :(
+      var optGroups = {};
       $('option[value^=crm_optgroup]', this).each(function () {
-        $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />');
+        var groupHtml = '';
+          $(this).nextUntil('option[value^=crm_optgroup]').each(function () {
+          groupHtml += this.outerHTML;
+        });
+        optGroups[$(this).text()] = groupHtml;
         $(this).remove();
       });
+      var replacedHtml = '';
+      for (var groupLabel in optGroups) {
+        replacedHtml += '<optgroup label="' + groupLabel + '">' + optGroups[groupLabel] + '</optgroup>';
+      }
+      if (replacedHtml) {
+        $el.html(replacedHtml);
+      }
 
       // quickform does not support disabled option, so yet another hack to
       // add disabled property for option values