Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Replace single questions dropdown by plugin: Difference between revisions

From Catglobe Wiki
Line 13: Line 13:
== Code ==
== Code ==


Add this code to javascript.
Add this code to Javascript editor of both single and single grid dropdown layout.
  <code class="mwt-code" >//replace select dropdown by plugin 'select2'</code>
  <code class="mwt-code">//replace select dropdown by plugin 'select2'</code>
  <code class="mwt-code" >//https://select2.org/</code>
  <code class="mwt-code">//https://select2.org/</code>
  <code class="mwt-code" >Question.bind('afterShowQuestion', function(ev, question, $el) {</code>
  <code class="mwt-code">Question.bind('afterShowQuestion', function(ev, question, $el) {</code>
  <code class="mwt-code" >&nbsp;$.when(</code>
  <code class="mwt-code">&nbsp;$.when(</code>
  <code class="mwt-code" >&nbsp; $.fn.select2 || $.ajax({</code>
  <code class="mwt-code">&nbsp; $.fn.select2 || $.ajax({</code>
  <code class="mwt-code" >&nbsp; &nbsp;type: 'GET',</code>
  <code class="mwt-code">&nbsp; &nbsp;type: 'GET',</code>
  <code class="mwt-code" >&nbsp; &nbsp;dataType: 'script',</code>
  <code class="mwt-code">&nbsp; &nbsp;dataType: 'script',</code>
  <code class="mwt-code" >&nbsp; &nbsp;cache: true,</code>
  <code class="mwt-code">&nbsp; &nbsp;cache: true,</code>
  <code class="mwt-code" >&nbsp; &nbsp;url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'</code>
  <code class="mwt-code">&nbsp; &nbsp;url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'</code>
  <code class="mwt-code" >&nbsp; }),</code>
  <code class="mwt-code">&nbsp; }),</code>
  <code class="mwt-code" >&nbsp; $.fn.select2 || $('&lt;link/&gt;', {rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css'}).appendTo('head')</code>
  <code class="mwt-code">&nbsp; $.fn.select2 || $('&lt;link/&gt;', {rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css'}).appendTo('head')</code>
  <code class="mwt-code" >&nbsp;).done(function() {</code>
  <code class="mwt-code">&nbsp;).done(function() {</code>
  <code class="mwt-code" >&nbsp; $('select', $el).select2({</code>
  <code class="mwt-code">&nbsp; $('select', $el).select2({</code>
  <code class="mwt-code" >&nbsp; &nbsp;minimumResultsForSearch: Infinity,</code>
  <code class="mwt-code">&nbsp; &nbsp;minimumResultsForSearch: Infinity,</code>
  <code class="mwt-code" >&nbsp; &nbsp;width: '100%',</code>
  <code class="mwt-code">&nbsp; &nbsp;width: '100%',</code>
  <code class="mwt-code" >&nbsp; &nbsp;templateResult: function(state) {</code>
  <code class="mwt-code">&nbsp; &nbsp;templateResult: function(state) {</code>
  <code class="mwt-code" >&nbsp; &nbsp; //custom option template in dropdown</code>
  <code class="mwt-code">&nbsp; &nbsp; //custom option template in dropdown</code>
  <code class="mwt-code" >&nbsp; &nbsp; if (!$(state.element).instance()) return null;</code>
  <code class="mwt-code">&nbsp; &nbsp; if (!$(state.element).instance()) return null;</code>
  <code class="mwt-code" >&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
  <code class="mwt-code">&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
  <code class="mwt-code" >&nbsp; &nbsp;},</code>
  <code class="mwt-code">&nbsp; &nbsp;},</code>
  <code class="mwt-code" >&nbsp; &nbsp;templateSelection: function(state) {</code>
  <code class="mwt-code">&nbsp; &nbsp;templateSelection: function(state) {</code>
  <code class="mwt-code" >&nbsp; &nbsp; //custom selected value</code>
  <code class="mwt-code">&nbsp; &nbsp; //custom selected value</code>
  <code class="mwt-code" >&nbsp; &nbsp; if (!$(state.element).instance()) return question.selectText;</code>
  <code class="mwt-code">&nbsp; &nbsp; if (!$(state.element).instance()) return question.selectText;</code>
  <code class="mwt-code" >&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
  <code class="mwt-code">&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
  <code class="mwt-code" >&nbsp; &nbsp;}</code>
  <code class="mwt-code">&nbsp; &nbsp;}</code>
  <code class="mwt-code" >&nbsp; });</code>
  <code class="mwt-code">&nbsp; });</code>
  <code class="mwt-code" >&nbsp; $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});</code>
  <code class="mwt-code">&nbsp; $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});</code>
  <code class="mwt-code" >&nbsp;});</code>
  <code class="mwt-code">&nbsp;});</code>
  <code class="mwt-code" >});</code>
  <code class="mwt-code">});</code>
<br>
<br>

Revision as of 06:59, 26 October 2022

Description

When viewed in dropdown layout of both Single and Single grid question. There are 3 problems in this case:
Some characters on keyboard decode/encode problem. Such as: Ampersand ( & ), single quotation  ( ' ), double quotation marks ( " ), is more than ( > ), is less than ( < ).
Same problem with characters not on keyboard. For example: ÷, ×, ±, ≠, ≡, ≤, ≥, …, ‘, °C and maybe more than that.
Problem with display HTML format in dropdown list when using it to design UI.

Expectation

These decode/encode problems will be fix and display normally like other question type and layout.

Example

Before-replace.png

Solution

Replace select dropdown default by plugin 'select2'.
URL: https://select2.org/

Code

Add this code to Javascript editor of both single and single grid dropdown layout.

//replace select dropdown by plugin 'select2'
//https://select2.org/
Question.bind('afterShowQuestion', function(ev, question, $el) {
 $.when(
  $.fn.select2 || $.ajax({
   type: 'GET',
   dataType: 'script',
   cache: true,
   url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'
  }),
  $.fn.select2 || $('<link/>', {rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css'}).appendTo('head')
 ).done(function() {
  $('select', $el).select2({
   minimumResultsForSearch: Infinity,
   width: '100%',
   templateResult: function(state) {
    //custom option template in dropdown
    if (!$(state.element).instance()) return null;
    return $('<div/>').html(state.text).css({'text-align': 'left'});
   },
   templateSelection: function(state) {
    //custom selected value
    if (!$(state.element).instance()) return question.selectText;
    return $('<div/>').html(state.text).css({'text-align': 'left'});
   }
  });
  $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});
 });
});