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

Check radio button or checkboxes with $.var? #54

Open
dueringa opened this issue Dec 31, 2018 · 1 comment
Open

Check radio button or checkboxes with $.var? #54

dueringa opened this issue Dec 31, 2018 · 1 comment

Comments

@dueringa
Copy link

Hi,

it seems like it's not possible to check checkboxes or radio buttons with DisplayJS based on a variable, like in the following example:
https://jsfiddle.net/jgb0qc38/

Is there another way to do this?
This might be related to #37 - the checked attribute, I guess.

@arguiot
Copy link
Owner

arguiot commented Jan 1, 2019

There is definitely a way to this, but not with DisplayJS currently. Here is a little plugin that you can use to render radio checkboxes:

$.fn.radioRender = function(push) {
	// the function
	const var_push = () => {
		this.if();
		this.else();
		const elements = document.querySelectorAll("[var]");
		for (let i = 0; i < elements.length; i++) {
			const attr = elements[i].getAttribute("var");

			if (!attr.includes(".")) {
				if (elements[i].type == "radio") {
					elements[i].checked = this.obj[attr]
				} else {
					elements[i].innerHTML = this.obj[attr];
				}
			} else {
				const parts = attr.split(".");
				let val = this.obj[parts[0]];

				for (let p = 1; p < parts.length; p++) {
					val = val[parts[p]];
				}

				if (elements[i].type == "radio") {
					elements[i].checked = val
				} else {
					elements[i].innerHTML = val
				}
			}
		}
	};
	// push var cheking
	if (!push) {
		var_push();
	} else if (push == true) {
		var_push();
		this.live(this.obj, () => {
			var_push();
		});
	} else {
		window.setInterval(() => {
			var_push();
		}, push);
	}
}

I'm not closing this issue as I definitely need to upgrade the $.var() function. As I'm a bit busy and not really working on DisplayJS lately, submitting a PR would help a lot. Thank you very much for telling me about this issue.

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