Maximum Selection Size Autocompleter

Yudhizth 03 Mar, 2017
Dear all,

I have a problem to implement maximumSelectionSize on autocompleter. I put this js code above the autocompleter

jQuery(document).ready(function($){
				$("#nomor").select2(
					{
						maximumSelectionSize:3
												
					}
				);
			});


And when I test the form, it show an error on browser console "Uncaught query function not defined for Select2 nomor". Any advice to solved this?

Thank you.
GreyHead 09 Mar, 2017
1 Likes
Hi Yudhizth,

Sorry, I can't get this to work either. At the moment I can't see the cause :-(

Bob
Yudhizth 10 Mar, 2017
No problem. I solved this with create an autocompleter manually.

jQuery(document).ready(function($){
				$("#nomor").select2(
					{
						minimumInputLength: 1,
						containerCss:{"min-width":"200px"},
						width: "element",
						multiple: true,
						maximumSelectionSize:3,
						//tags: true,
						tokenSeparators: [","," "],
						ajax:{
							url: "http://localhost/jom/index.php?option=com_chronoforms5&chronoform=limit-autocompleter&event=number&tvout=ajax",
							dataType: "json",
							data: function (term, page){
								return {
									nomor: term,
								};
							},
							results: function (data, page){
								return {results: data};
							}
						},
						formatNoMatches: "Error!!",
						formatSearching: "Loading....",
						formatAjaxError: "No Data Found....",
						
						initSelection: function(element, callback){
							var data = [];
							$(element.val().split(",")).each(function (){
								data.push({id: this, text: this});
							});
							callback(data);
						}
						
					}
				);
			});



and put the select2 js on js file

http://localhost/jom/libraries/cegcore/assets/select2/select2.min.js

and put all css of select2

http://localhost/jom/libraries/cegcore/assets/select2/select2.css
http://localhost/jom/libraries/cegcore/assets/select2/select2-bootstrap.css
http://localhost/jom/libraries/cegcore/assets/select2/select2-gcore.css
GreyHead 10 Mar, 2017
Answer
Hi Yudhizth ,

Sorry, a bit late but here's the simpler solution. Add this to a Load JavaScript action *After* the AutoComplete action and before the HTML (Render form) action.
jQuery(document).ready(function(jQ){
  jQ('#nomor').data('select2').opts.maximumSelectionSize = 2;
});

Bob

PS For other readers, replace nomor with the ID of the autocompleter element and it should work for other options if you replace maximumSelectionSize = 2 with the option name and value.
Yudhizth 10 Mar, 2017
Thank you Bob,

with the code above, where can I set the maximumSelectionSize?
GreyHead 10 Mar, 2017
Hi Yudhizth ,

Oops - sorry, it got deleted in editing - now replaced.

Bob
Yudhizth 10 Mar, 2017
Thank you Bob,

It's working fine now, thanks. 🙂

God bless you

Yudhizth
This topic is locked and no more replies can be posted.