Difference between revisions of "Replace single questions dropdown by plugin"
(→Code) |
(→Code) |
||
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. |
+ | |||
+ | <code class="mwt-code" >Question.bind('afterShowQuestion', function(ev, question, $el) {</code><br><code class="mwt-code" > $.when(</code><br><code class="mwt-code" > $.fn.select2 || $.ajax({</code><br><code class="mwt-code" > type: 'GET',</code><br><code class="mwt-code" > dataType: 'script',</code><br><code class="mwt-code" > cache: true,</code><br><code class="mwt-code" > url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'</code><br><code class="mwt-code" > }),</code><br><code class="mwt-code" > $.fn.select2 || $('<link/>', {rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css'}).appendTo('head')</code><br><code class="mwt-code" > ).done(function() {</code><br><code class="mwt-code" > $('select', $el).select2({</code><br><code class="mwt-code" > minimumResultsForSearch: Infinity,</code><br><code class="mwt-code" > width: '100%',</code><br><code class="mwt-code" > templateResult: function(state) {</code><br><code class="mwt-code" > //custom option template in dropdown</code><br><code class="mwt-code" > if (!$(state.element).instance()) return null;</code><br><code class="mwt-code" > return $('<div/>').html(state.text).css({'text-align': 'left'});</code><br><code class="mwt-code" > },</code><br><code class="mwt-code" > templateSelection: function(state) {</code><br><code class="mwt-code" > //custom selected value</code><br><code class="mwt-code" > if (!$(state.element).instance()) return question.selectText;</code><br><code class="mwt-code" > return $('<div/>').html(state.text).css({'text-align': 'left'});</code><br><code class="mwt-code" > }</code><br><code class="mwt-code" > });</code><br><code class="mwt-code" > $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});</code><br><code class="mwt-code" > });</code><br><code class="mwt-code" >});</code> |
Revision as of 08:56, 26 October 2022
Contents
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
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) {
$.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'});
});
});