This is the checkboxes i have so far. I would like to have at least one checkbox checked all the time - how can this be accomplished?
SOLUTION:
HTML:
<form>
<ul>
{{#each checkbox}}
<li>
<input type="checkbox" checked="{{checked}}" class="toggle-checked"> {{name}}: {{checked}}
</li>
{{/each}}
</ul>
</form>
JS:
Cbtest = new Mongo.Collection('cbtest');
Template.checkbox.helpers({
checkbox: function () {
return Cbtest.find();
}
});
Template.checkbox.events({
"click .toggle-checked": function (event) {
var self = this;
//deactivate the checkboxes visually
event.preventDefault();
Meteor.call("setChecked", self._id, !self.checked);
}
});
Meteor.methods({
setChecked: function (checkboxId, setChecked) {
//deactivate the checkbox value change in the database
if (!setChecked && CheckboxResources.find({
checked: true
}).count() === 1)
return;
Cbtest.update(checkboxId, {
$set: {
checked: setChecked
}
});
}
});
I'd like to avoid jquery and stick with javascript and underscorejs solely.
Thanks in advance! Vin
You can just make sure there are at least two checked items before updating the current item to check: false
:
Meteor.methods({
setChecked: function (checkboxId, setChecked) {
if (!setChecked && Cbtest.find({checked: true}).count() === 1)
return; // or throw error
Cbtest.update(checkboxId, {
$set: {
checked: setChecked
}
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.