Hanldebars/JSON - populating a select box complete with optgroups - handlebars.js

Dataset:
{
"data": {
"some": [],
"other": [],
"goes": [],
"workoutUnitLabels": [{
"groupLabel": "DISTANCE",
"selected": "",
"unitID": 2,
"unitLabel": "Meters"
}, {
"groupLabel": "DISTANCE",
"selected": "",
"unitID": 3,
"unitLabel": "Miles"
}, {
"groupLabel": "DISTANCE",
"selected": "",
"unitID": 4,
"unitLabel": "Yards"
}, {
"groupLabel": "TIME",
"selected": "selected",
"unitID": 5,
"unitLabel": "Time (hh:mm:ss)"
}, {
"groupLabel": "VELOCITY",
"selected": "",
"unitID": 6,
"unitLabel": "Velocity (m/s)"
}]
}
}
Given the dataset above, I'm trying to pass this to a handlebar template but can't figure out the looping I need to do to get it to work right. Below is what I'm looking to build out:
<select>
<optgroup label="DISTANCE">
<option value="2">Meters</option>
<option value="3">Miles</option>
<option value="4">Yards</option>
</optgroup>
<optgroup label="TIME">
<option value="5">Time (hh:mm:ss)</option>
</optgroup>
<optgroup label="VELOCITY">
<option value="6">Velocity (m/s</option>
</optgroup>
But this is what mine is currently doing (notice the DISTANCE gets repeated).
<select>
<optgroup label="DISTANCE">
<option value="2">Meters</option>
</optgroup>
<optgroup label="DISTANCE">
<option value="3">Miles</option>
</optgroup>
<optgroup label="TIME">
<option value="5">Time (hh:mm:ss)</option>
</optgroup>
<optgroup label="VELOCITY">
<option value="6">Velocity (m/s)</option>
</optgroup>
</select>
And here is what my handlebar template looks like:
{{#each .}}
<label>some label here <input type="text" name="somename" value="{{somevalue}}" /></label>
<select>
{{#workoutUnitLabels}}
<optgroup label="{{groupLabel}}">
<option value="{{unitID}}" {{selected}}>{{unitLabel}} ({{unitID}})</option>
</optgroup>
{{/workoutUnitLabels}}
</select>
{{/each}}
Any advice on what I need to switch up here?

There is no easy way to do that using handlebars, it would be better to tranform your data first. That said, assuming that your data will always be ordered by the groupLabel, it would be possible to do this (even though it's not pretty):
Helper:
Handlebars.registerHelper('notEqPrev', function(index, workoutUnitLabels, options) {
let prev = index - 1 < 0 ? 0 : index - 1;
if (index === 0 || workoutUnitLabels[prev].groupLabel !== workoutUnitLabels[index].groupLabel) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
HTML:
<label>some label here <input type="text" name="somename" value="{{somevalue}}" /></label>
<select>
{{#with data.workoutUnitLabels as |work| }}
{{#each work }}
{{#notEqPrev #index work }}
{{#unless #first }}
</optgroup>
{{/unless}}
<optgroup label="{{groupLabel}}">
{{/notEqPrev}}
<option value="{{unitID}}" {{selected}}>{{unitLabel}} ({{unitID}})</option>
{{#if #last}}
</optgroup>
{{/if}}
{{/each}}
{{/with}}
</select>

Related

ASP.NET Web Pages with Razor

how to get the name of select tag and use to if condition here is my code:
#{
var size="";
if(Request.Form["cboSize"] == "Extra Small (XS)"){
size = "Extra Small (XS)";
}
...
}
<select name="cboSize">
<option value="xs">Extra Small (XS)</option>
<option value="s">Small (S)</option>
<option value="m">Medium (M)</option>
<option value="l">Large (L)</option>
<option value="xl">Extra Large (XL)</option>
</select>
You need to wrap your select node in a form to get its value from Request.Form["cboSize"]:
<form method="POST">
<select name="cboSize">
<option value="xs">Extra Small (XS)</option>
<option value="s">Small (S)</option>
<option value="m">Medium (M)</option>
<option value="l">Large (L)</option>
<option value="xl">Extra Large (XL)</option>
</select>
<input type="submit" />
</form>
Then in your code:
public async Task<IActionResult> OnPostAsync()
{
var size = Request.Form["cboSize"];
//do something with it asyncronously
return RedirectToPage($"/Details/{size}");
}

Symfony2 form_widget datetime : an hour without leading zeros

formType
->add("someday", DateTimeType::class, [
"date_widget" => "single_text",
"minutes" => ["00", "20", "40"],
"required" => true,
])
template
{{ form_widget(form.someday.date) }}
{{ form_widget(form.someday.hour) }}
{{ form_widget(form.someday.minute) }}
Generated HTML (on hour part)
<select id="form_someday_time_hour" name="form[someday][time][hour]">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
In this case, selectbox of hour with leading zeros(00, 01, 02 ... 23),
but I would like to change this selectbox without leading zeros(0, 1, 2 ... 23).
I would like to generate HTML of this format. (on hour part)
<select id="form_someday_time_hour" name="form[someday][time][hour]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
I tried specifying the format in the form type, but there was no change.
Will there be options for the widget on the template side?
Thank you if you know.
Try the Twig date filter and specify the hour format without leading zeros like so:
{{ form_widget(form.someday.hour|date('h')) }}
Formats are specified using the DateInterval.format. I think that should work for you. Please try it.
EDIT #2
I'm not sure why you are getting that error. Maybe it's because the date filter is in the wrong place? Can you try this:
{{ form_widget(form.someday.hour)|date('h') }}
I'm not sure if that will fix it, but please try it. If that doesn't work, maybe you can tell me how you want the "date", "hour" and "minute" to be rendered in HTML format (update your question post to show that); then maybe we can use the formatting of the DateTimeType field.
EDIT #3
Why not try this then:
$hours = new array();
for ($i = 0; $i <= 23; $i++) {
array_push($hours, strval($i));
}
...
->add("someday", DateTimeType::class, [
"date_widget" => "single_text",
"minutes" => ["00", "20", "40"],
"hours" => $hours,
"required" => true,
])
I think that will work for you.
EDIT #4
Try this as well.
->add("someday", DateTimeType::class, [
"format" => "dd H mm",
"minutes" => ["00", "20", "40"],
"required" => true,
])
Then will be shown as drop down lists. Not sure why you were using date_widget???
Unfortunately, that's impossible.
https://github.com/symfony/symfony/blob/f7d9701cdfaf9be6b40f661d0d9ec973dc4b904b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php#L74
Looking at the code, recognized added leading zeros here.
If rewrite like this, it may be the expected behavior.
- $hours[str_pad($hour, 2, '0', STR_PAD_LEFT)] = $hour;
+ $hours[$hour] = $hour;

Meteor Dropdown list get and set

What is the best way to get and select values from a dropdown list (and also in radio) in Meteor.
I have created a helper:
Template.categories.helpers({
categories: ["facebook", "news", "tv", "tweets"]
});
and in html
...
<select class="form-control" id="category">
{{> categories}}
</select>
...
<template name="categories">
<option disabled="disabled" selected="selected">Please Select</option>
{{#each categories}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</template>
In case of edit, I would like to evaluate it with value coming from database (e.g. news) to be selected.
Thanks in advance.
Template HTML:
<select id="category-select">
<option disabled="disabled" selected="selected">Please Select</option>
{{#each categories}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
Template js:
Template.categories.helpers({
categories: function(){
return ["facebook", "news", "tv", "tweets"]
}
});
Template.categories.events({
"change #category-select": function (event, template) {
var category = $(event.currentTarget).val();
console.log("category : " + category);
// additional code to do what you want with the category
}
});
Template.categories.helpers({
categories: function(){
return ["facebook", "news", "tv", "tweets"]
}
});
And you should consider changing template name and helper, they shouldn't be the same.

Meteor ignoring 'selected' attribute on <option> on <select multi>

I am trying to use a multi select box in meteor and have some of the options be marked with selected based on info from the db for use with slectize.js. however it seems like meteor when building its DOM tree ignores the selected property.
<label>User</label>
<select id="customer_user_id" name="user_id" class="form-control input-sm" multiple>
{{#each users}}
{{#if inList _id ../customer_user_id}}
<option value="{{_id}}" selected>{{full_name}}</option>
{{else}}
<option value="{{_id}}">{{full_name}}</option>
{{/if}}
{{/each}}
</select>
and the helper
Handlebars.registerHelper("inList", function (val, list) {
console.log(list.indexOf(val) > -1)
console.log(list)
console.log(val)
return list.indexOf(val) > -1;
});
i see that the condition is true but there is no options with the selected property
I have been breaking my head on this for more then 24 hours now
I have also tried this method with the same result
<label>User</label>
<select id="customer_user_id" name="user_id" class="form-control input-sm" multiple>
{{#each users}}
<option value="{{_id}}" {{selected _id ../customer_user_id "selected"}}>{{full_name}}</option>
{{/each}}
</select>
with this helper
Handlebars.registerHelper("selected", function (val1, val2, string) {
if (val1 === val2) {
return string;
}
});
Try using selected="selected" instead of just selected:
<option value="{{_id}}" selected="selected">{{full_name}}</option>

Meteor each loop data not displaying

In each loop data is not displaying.value="{{param_type}}" not displaying.Please help me.
{{ #each api_method_param_data }}
<select id="param_type">
{{#each dropdown}}
{{#if isSelected this param_type}}
<option value="{{param_type}}" selected="selected"> {{this}} </option>
{{else}}
<option value="{{param_type}}" > {{this}} </option>
{{/if}}
{{/each}}
</select>
{{/each}}
This is the helper function
Template.apimethodchange.isSelected = function(fooToCheck, recordFoo)
{
var checkrec = "";
for(var i=0;i<recordFoo.length;i++)
{
checkrec = checkrec + recordFoo[i];
}
console.log(checkrec + fooToCheck);
return (fooToCheck == checkrec);
};
Template.apimethodchange.dropdown = ["string","array","int","boolean","double","struct"];
Try with ../ to access parent context
<option value="{{../param_type}}" selected="selected"> {{this}} </option>

Resources