Difference between revisions of "Replace single questions dropdown by plugin"

From Catglobe Wiki
Jump to: navigation, search
(Solution)
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Description ==
 
== Description ==
  
When viewed in dropdown layout of both Single and Single grid question. There are 3 problems in this case:<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''• '''Some characters on keyboard decode/encode problem. Such as: Ampersand ( & ), single quotation&nbsp; ( ' ), double quotation marks ( " ), is more than ( &gt; ), is less than ( &lt; ).<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''• '''Same problem with characters not on keyboard. For example: ÷, ×, ±, ≠, ≡, ≤, ≥, …, ‘, °C and maybe more than that.<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''•''' Problem with display HTML format in dropdown list when using it to design UI.
+
When viewing in both Single and Single grid dropdown layout. There are 3 problems because using the default HTML &lt;select&gt; tag:<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''• '''Decode/encode problem with some characters on keyboard. Such as: Ampersand ( & ), single quotation&nbsp; ( ' ), double quotation marks ( " ), is more than ( &gt; ), is less than ( &lt; ).<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''• '''Same problem with characters not on keyboard. For example: ÷, ×, ±, ≠, ≡, ≤, ≥, …, ‘, °C and maybe more than that.<br style="box-sizing: inherit; color: #1d1c1d; font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">'''•''' Problem with display HTML format in dropdown list when using it to design UI.
 +
 
 
== Expectation ==
 
== Expectation ==
  
These decode/encode problems will be fix and display normally like other question type and layout.
+
These problems will be fix and display normally like other question type and layout.
 +
 
 
== Example ==
 
== Example ==
[Before-replace.png]
+
Before:
 +
[[File: Before-replace.png]]
 +
 
 +
 
 +
After:
 +
[[File: After-replace.png]]
 +
 
 
== Solution ==
 
== Solution ==
  
Replace select dropdown default by plugin 'select2'.<br>URL: [https://select2.org/ https://select2.org/]
+
Replace the default HTML &lt;select&gt; tag by using 'select2' plugin.<br>URL: [https://select2.org/ https://select2.org/]
 +
 
 
== Code ==
 
== Code ==
  
Add this code to javascript.
+
Add this code to Javascript editor in single or single grid dropdown layout.
<code class="mwt-code" >//replace select dropdown by plugin 'select2'</code>
+
 
<code class="mwt-code" >//https://select2.org/</code>
+
<source lang="javascript">
<code class="mwt-code" >Question.bind('afterShowQuestion', function(ev, question, $el) {</code>
+
Question.bind('afterShowQuestion', function(ev, question, $el) {
<code class="mwt-code" >&nbsp;$.when(</code>
+
$.when(
<code class="mwt-code" >&nbsp; $.fn.select2 || $.ajax({</code>
+
$.fn.select2 || $.ajax({
<code class="mwt-code" >&nbsp; &nbsp;type: 'GET',</code>
+
type: 'GET',
<code class="mwt-code" >&nbsp; &nbsp;dataType: 'script',</code>
+
dataType: 'script',
<code class="mwt-code" >&nbsp; &nbsp;cache: true,</code>
+
cache: true,
<code class="mwt-code" >&nbsp; &nbsp;url: 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js'</code>
+
url: 'https://cdn.catglobe.com/select2-4.1.0/js/select2.min.js'
<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>
+
$.fn.select2 || $('<link/>', {rel: 'stylesheet', href: 'https://cdn.catglobe.com/select2-4.1.0/css/select2.min.css'}).appendTo('head')
<code class="mwt-code" >&nbsp;).done(function() {</code>
+
).done(function() {
<code class="mwt-code" >&nbsp; $('select', $el).select2({</code>
+
$('select', $el).select2({
<code class="mwt-code" >&nbsp; &nbsp;minimumResultsForSearch: Infinity,</code>
+
minimumResultsForSearch: Infinity,
<code class="mwt-code" >&nbsp; &nbsp;width: '100%',</code>
+
width: '100%',
<code class="mwt-code" >&nbsp; &nbsp;templateResult: function(state) {</code>
+
templateResult: function(state) {
<code class="mwt-code" >&nbsp; &nbsp; //custom option template in dropdown</code>
+
//custom option template in dropdown
<code class="mwt-code" >&nbsp; &nbsp; if (!$(state.element).instance()) return null;</code>
+
if (!$(state.element).instance()) return null;
<code class="mwt-code" >&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
+
return $('<div/>').html(state.text).css({'text-align': 'left'});
<code class="mwt-code" >&nbsp; &nbsp;},</code>
+
},
<code class="mwt-code" >&nbsp; &nbsp;templateSelection: function(state) {</code>
+
templateSelection: function(state) {
<code class="mwt-code" >&nbsp; &nbsp; //custom selected value</code>
+
//custom selected value
<code class="mwt-code" >&nbsp; &nbsp; if (!$(state.element).instance()) return question.selectText;</code>
+
if (!$(state.element).instance()) return question.selectText;
<code class="mwt-code" >&nbsp; &nbsp; return $('&lt;div/&gt;').html(state.text).css({'text-align': 'left'});</code>
+
return $('<div/>').html(state.text).css({'text-align': 'left'});
<code class="mwt-code" >&nbsp; &nbsp;}</code>
+
}
<code class="mwt-code" >&nbsp; });</code>
+
});
<code class="mwt-code" >&nbsp; $('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});</code>
+
$('.select2-selection', $el).css({height: 'auto', 'min-height': '28px'});
<code class="mwt-code" >&nbsp;});</code>
+
});
<code class="mwt-code" >});</code>
+
});
<br>
+
</source>

Latest revision as of 04:13, 27 October 2022

Description

When viewing in both Single and Single grid dropdown layout. There are 3 problems because using the default HTML <select> tag:
Decode/encode problem with some characters on keyboard. 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 problems will be fix and display normally like other question type and layout.

Example

Before: Before-replace.png


After: After-replace.png

Solution

Replace the default HTML <select> tag by using 'select2' plugin.
URL: https://select2.org/

Code

Add this code to Javascript editor in 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://cdn.catglobe.com/select2-4.1.0/js/select2.min.js'
		}),
		$.fn.select2 || $('<link/>', {rel: 'stylesheet', href: 'https://cdn.catglobe.com/select2-4.1.0/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'});
	});
});