javascript - Node Mustache: How to get each value of an Array on a separate Radio Button? -
environment: node, express, mu2express, mongoose
what want array mongoose , have template render each value own radio button.
can't find example.
what have:
the route in app.js:
app.get('/mutest',function(req, res){ var info = [1,2,3,4]; res.render('mutest',{ 'locals': { data: info }}); });
the template (mutest.mustache)
<form name="test" action="mutest" method="post"> {{#data}} <input type="radio" name="choice" value={{data}}>{{data}}<br/> {{/data}} <input type="submit" value="submit"> </form>
the result:
1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4and, of course, choosing of buttons submits value of "1,2,3,4".
what want: page rendered this:
1 2 3 4with single value (e.g. string "3") submitted.
tia suggestions!
the trick have refer current item beingness pointed at:
in first case, you're using {{data}} value (and text of radio button) refers info array.
in sec case, you're using {{data[0]}} refer first element of array.
for mustache (super spec compliant) have wrap values (1,2,3,4) object:
data = [ {value: 1, label: 1}, {value: 2, label: 2}, .... ];
then:
<form name='test' action='mutest' method='post'> {{#data}} <input type='radio' name='choice' value={{#value}}>{{#label}}</input/><br/> {{/data}} <input type='submit' value='submit'/> </form>
in mustache.js, {{.}} operator using 'this' object in javascript (or self in python, etc):
<form name='test' action='mutest' method='post'> {{#data}} <input type='radio' name='choice' value={{.}}>{{.}}</input/><br/> {{/data}} <input type='submit' value='submit'/> </form>
and output is:
<form name='test' action='mutest' method='post'> <input type='radio' name='choice' value='1'>1</input><br/> <input type='radio' name='choice' value='2'>2</input><br/> <input type='radio' name='choice' value='3'>3</input><br/> <input type='radio' name='choice' value='4'>4</input><br/> <input type='submit' value='submit'/> </form>
javascript node.js express mongoose mustache
No comments:
Post a Comment