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 21: Line 21:


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

Revision as of 08:31, 26 October 2022

Description

When viewing in both Single and Single grid dropdown layout. 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 using plugin 'select2'.
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'});
	});
});