Jump to content
Toggle menu
  • 915 articles
  • 3.8K files
  • 30.2K users
  • 279.2K edits
Catglobe Wiki
Toggle preferences menu
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 18: Line 18:
== Code ==
== Code ==


Add this code to Javascript editor in both single or single grid dropdown layout.
Add this code to Javascript editor in both single or single grid dropdown layout.<pre>Question.bind('afterShowQuestion', function(ev, question, $el) {<br>&nbsp;$.when(<br>&nbsp; $.fn.select2 || $.ajax({<br>&nbsp; &nbsp;type: 'GET',<br>&nbsp; &nbsp;dataType: 'script',<br>&nbsp; &nbsp;cache: true,<br>&nbsp; &nbsp;url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'<br>&nbsp; }),<br>&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')<br>&nbsp;).done(function() {<br>&nbsp; $('select', $el).select2({<br>&nbsp; &nbsp;minimumResultsForSearch: Infinity,<br>&nbsp; &nbsp;width: '100%',<br>&nbsp; &nbsp;templateResult: function(state) {<br>&nbsp; &nbsp; //custom option template in dropdown<br>&nbsp; &nbsp; if (!$(state.element).instance()) return null;<br>&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});<br>&nbsp; &nbsp;},<br>&nbsp; &nbsp;templateSelection: function(state) {<br>&nbsp; &nbsp; //custom selected value<br>&nbsp; &nbsp; if (!$(state.element).instance()) return question.selectText;<br>&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});<br>&nbsp; &nbsp;}<br>&nbsp; });<br>&nbsp; $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});<br>&nbsp;});<br>});</pre>
 
Question.bind('afterShowQuestion', function(ev, question, $el) {<br>&nbsp;$.when(<br>&nbsp; $.fn.select2 || $.ajax({<br>&nbsp; &nbsp;type: 'GET',<br>&nbsp; &nbsp;dataType: 'script',<br>&nbsp; &nbsp;cache: true,<br>&nbsp; &nbsp;url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'<br>&nbsp; }),<br>&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')<br>&nbsp;).done(function() {<br>&nbsp; $('select', $el).select2({<br>&nbsp; &nbsp;minimumResultsForSearch: Infinity,<br>&nbsp; &nbsp;width: '100%',<br>&nbsp; &nbsp;templateResult: function(state) {<br>&nbsp; &nbsp; //custom option template in dropdown<br>&nbsp; &nbsp; if (!$(state.element).instance()) return null;<br>&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});<br>&nbsp; &nbsp;},<br>&nbsp; &nbsp;templateSelection: function(state) {<br>&nbsp; &nbsp; //custom selected value<br>&nbsp; &nbsp; if (!$(state.element).instance()) return question.selectText;<br>&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});<br>&nbsp; &nbsp;}<br>&nbsp; });<br>&nbsp; $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});<br>&nbsp;});<br>});

Revision as of 07:55, 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:


After: File:After-replace.PNG

Solution

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

Code

Add this code to Javascript editor in both single or single grid dropdown layout.

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