Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allowClear option with ajax load #1785

Closed
kylemwhite opened this issue Oct 9, 2013 · 4 comments
Closed

allowClear option with ajax load #1785

kylemwhite opened this issue Oct 9, 2013 · 4 comments

Comments

@kylemwhite
Copy link

I'm not sure if I'm misunderstanding or if my use case is just not possible right now. From what I can tell, it is not possible to have the allowClear icon on an S2 box that uses ajax because ajax requires an INPUT tag instead of a SELECT tag but the allowClear option requires a blank OPTION tag inside the SELECT tag.

Is there a way to use the ajax properties AND the allowClear option? If so, how? If not, then maybe this is a feature request.

Thanks.

@kevin-brown
Copy link
Member

You should be able to use allowClear on a hidden <input> element. It may require the placeholder option as well (see #1283).

@kylemwhite
Copy link
Author

KB, you are correct sir. While I did specify a placeholder, it was an empty string. I have changed it to a single whitespace and now it is working. Thanks.

@pandu-49-zz
Copy link

Can you tell me or paste an example of code to know how did you add placeholder for select2 ajax call @kylemwhite

Thanks

@webdevilopers
Copy link

webdevilopers commented Dec 7, 2016

Was this issue related to < 4.* those days @kylemwhite ?

Here is an example with 4.0.3 using <select> and AJAX and allowClear:

		$j("#customerId").select2({
			minimumInputLength: 2,
			placeholder: 'All',
			allowClear: true,
			ajax: {
				url: "/customer/autocomplete",
				dataType: 'json',
				delay: 400,
				processResults: function (data) {
					return {
						results: data
					};
				}
			}
		}).val('1').trigger('change');
		<select id="customerId" name="customerId" style="width: 300px;">
			<option value=""></option>
			<option value="1">Pre-selected Customer here</option>
		</select>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants