I have a situation where I have a number of checkboxes created in an *ngFor loop. Say there’s 5… I want to limit selections to, say, 3. Can I disallow the 4th selection by some logic in the ionChange method I’ve tied to the checkbox? I’ve tried setting event.value to false and returning false from the event, but those ideas didn’t pan out.
My end goal is to keep the user from selecting only X items of Y choices by canceling the event in the event handler, and posting a message that they have already selected the max number of possible choices and forcing them to deselect something to select another choice.
Is there a simpler way? Maybe by setting up a group of some sort and doing this natively somehow?
You can’t disable lines in the checkbox, but you can disable a submit button, and put an error message next to it, like “select at most three”.
If you’re repeating this in an ngFor, I think the cleanest way is to create a component that extends ion-checkbox. schuhma-checkbox. The component emits the choices when the user clicks submit, and handles the error message inside itself, so your main page html looks very simple.
Thanks, Aaron, I appreciate your thoughts and time to respond. You’ve given me several ideas to think about.
That’s not quite the UI experience I’m going for… I want more direct, immediate feedback that the user’s choices have violated the rules… it’s a voting application and it must be VERY clear to users that they are trying to OVER vote and they must take some action (deselect a selection) to vote for another candidate; I can’t let them overvote and leave it up to the user to figure out why the “next” button isn’t available, even with onscreen information explaining why. I’ll keep working at this with your suggestions in mind, though .
I’ve done more work on this and neither ionChange, nor click events are much help. In fact I’m using them BOTH and I cannot cause the checkbox to keep from changing states.
Is the checkbox busted? Why does ionChange provide a different type of Event object than the standard JS DOM Event you get in a “click” event?
I want more of the behavior I get with click (which provides a JS DOM Event and access to stopPropagation() and preventDefault(), if they worked), but I need the ability to work with the event.clicked, which you only get with the ionChange Event object. (the “clicked” element isn’t available in the event object you get in the JS DOM Event, which is weird be cause I pass the SAME object to each). Ideally I’d only have ionChange but get access to a working form of preventDefault() or stopPropagation().
Please check this site out for what I’m going for: http://anywhereballot.com/race/president-and-vice-president-of-the-united-states/ . Try selecting a second choice when the page loads and you’ll see what I mean. The checkboxes aren’t made unavailable when you hit the max selections - when you select more than the max a modal popup appears and tells you to deselect a choice, then select something else.
In v3, ionChange is an Angular EventEmitter, which emits a BaseInput (an internal class that pretty much every input-type component extends). You can think of it giving you a snapshot of the current state of the component. So it give you more information than just the current click.
In v4, ionChange will be much closer to what you might be expecting. It is a Stencil-based EventEmitter (not Angular), and it basically emits a string with an event code.
OMG, Robert! This was so simple! I had no idea you could cast $event to a Checkbox! I figured since I passed $event I had to deal with it as an Ionic Event object. The real key was both being able to retrieve the state via cbox.checked and setting it (I kept trying cbox.checked(false)). I didn’t even need the “fix” index.
This works because the event is a collection of core features of a checkbox (i.e., a BaseInput). There’s no official documentation about what I’m going to say now, so things might change, but my read of the v4 source code for ionChange itells me that the code @rapropos gave you will break in v4. I don’t know a better way to solve the problem though, just be aware that change is coming.
Thanks, Aaron - and thanks for the V3 vs V4 info you provided above, too . I’ll have to keep my eyes open and rework this part if/when things change. I appreciate your input!