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

Disable an option using extra attribute from server #34

Open
sabin2hats opened this issue Mar 6, 2023 · 2 comments
Open

Disable an option using extra attribute from server #34

sabin2hats opened this issue Mar 6, 2023 · 2 comments

Comments

@sabin2hats
Copy link

Is there any way to make an option disabled based on another field value from server , for example now we have id, text field returned from server , can I return an additional parametr as is_freeze so based on thats value option gets disabled.

@ksykulev
Copy link
Owner

ksykulev commented Mar 6, 2023

There is no built-in way to do this, however, if you take a look at the way I build up the options:
https://github.com/ksykulev/chosen-ajax-addition/blob/master/chosen.ajaxaddition.jquery.js#L112-L128

			if (!inputEmptied) {
				if ($.isArray(items)) {
					//array of kv pairs [{id:'', text:''}...]
					$.each(items, function (i, opt) {
						if (typeof valuesHash[opt.id] === 'undefined') {
							$('<option value="' + opt.id + '">' + opt.text + '</option>').appendTo(select);
						}
					});
				} else {
					//hash of kv pairs {'id':'text'...}
					$.each(items, function (value, text) {
						if (typeof valuesHash[value] === 'undefined') {
							$('<option value="' + value + '">' + text + '</option>').appendTo(select);
						}
					});
				}
			}

you should be able to modify the code to use an additional parameter such as is_freeze (assuming it is a boolean..)

Something like this..

			if (!inputEmptied) {
				if ($.isArray(items)) {
					//array of kv pairs [{id:'', text:''}...]
					$.each(items, function (i, opt) {
                                                let disabledFlag = opt.is_freeze ?  'disabled' : '';
						if (typeof valuesHash[opt.id] === 'undefined') {
							$('<option ' + disabledFlag + ' value="' + opt.id + '">' + opt.text + '</option>').appendTo(select);
						}
					});
				} else {
					//hash of kv pairs {'id':'text'...}
					$.each(items, function (value, text) {
                                                let disabledFlag = opt.is_freeze ?  'disabled' : '';
						if (typeof valuesHash[value] === 'undefined') {
							$('<option ' + disabledFlag + ' value="' + value + '">' + text + '</option>').appendTo(select);
						}
					});
				}
			}

I don't really modify the response from the server, so as long as the is_freeze value is returned alongside the id and text fields, it will be available for reference when building the options up.

Also, a word for caution, the original library that this project extends is deprecated (https://github.com/harvesthq/chosen) and I have not worked on this project for a pretty long time, so definitely use it carefully.

@sabin2hats
Copy link
Author

It helps, Thank you.

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

2 participants