Selecting one term displays all terms with same value


#1

Hello everyone,

like my title say, when I select one term in the multiple select box, all terms who have the same option value/id are displayed/sent in the input.

Is this a normal comportment ? Is there a solution ? Here my code :

function formatRepo (repo) {
		console.log('formatRepo');
		return repo.text;
	}

	function formatRepoSelection (repo) {
		console.log('formatRepoSelection');
		return repo.text;
	}

	$('.chosen-select').select2({
		ajax: {
			url: ajaxobject.ajaxurl,
			dataType: 'json',
			data: function (params) {
				var tax_name = $("[name=main_cat_choice]").val();
				return {
					search: params.term, // search query
					action: 'keyword_terms_mobile',
					tax_name: tax_name,
					type: 'public'
				};
			},
			processResults: function( data ) {
				console.log("processResults: "+data);
				var options = [];
				if ( data ) {

					// data is the array of arrays, and each of them contains ID and the Label of the option
					$.each( data, function( index, text ) { // do not forget that "index" is just auto incremented value
						options.push( { id: text.id, text: text.label  } );
					});

				}
				return {
					results: options
				};
			},
			// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
		},
		templateResult: formatRepo,
		templateSelection: formatRepoSelection,
		// theme: "classic"
	});

Thank you !


#2

Can you give me an example of the data you have in your Select2? I’m trying to understand why you have duplicate data in the list, and how users would tell them apart if they selected one of the duplicated values.

Based only on your description above, I would expect it to behave as you described, because the Select2 widget is just a user interface widget that ultimately manipulates the underlying HTML select element. When an item is selected in the Select2 widget, I would expect the widget code to loop through the underlying select element’s options, setting each matching option to “selected”. So if there are multiple options with the same value, they would all get selected.