简体   繁体   中英

Meteor checkbox - Display Value of expression as String, not interpreted as Boolean

I have a checkbox implemented and it's working just fine.

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 () {
       var self = this;
        Meteor.call("setChecked", self._id, !self.checked);
    }
});

Meteor.methods({
    setChecked: function (checkboxId, setChecked) {
        Cbtest.update(checkboxId, {
            $set: {
                checked: setChecked
            }
        });
    }
});

在此处输入图片说明

I want to display the Value ("true" or "false") depending on the checkbox's state. As now it seems that the Expression "{{ checked }}" is evaluatiated to true or false, and if its true then it returns the value of the corresponding document entry. How can i just display the content as String ("true" / "false")?

Thanks in advance! Vin

You could add another helper that transforms the checked Bool into a String using the toString() function:

checkedString: function () {
  return this.checked.toString();
}

And then use this helper in the template:

<input type="checkbox" checked="{{checked}}" class="toggle-checked"> {{name}}: {{checkedString}}

See this meteor pad for a demo.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM