Last active
August 5, 2024 20:15
-
-
Save LukeChannings/6173ab951d8b1dc4602e to your computer and use it in GitHub Desktop.
A select helper for handlebars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handlebars.registerHelper("select", function(value, options) { | |
return options.fn(this) | |
.split('\n') | |
.map(function(v) { | |
var t = 'value="' + value + '"' | |
return ! RegExp(t).test(v) ? v : v.replace(t, t + ' selected="selected"') | |
}) | |
.join('\n') | |
}) |
How to create it for multiple selection.
multiselect: function (selected, option) {
if(selected == undefined) {
return '';
}
return selected.indexOf(option) !== -1 ? 'selected' : '';
}
<div class="form-group col-6">
<label for="booking_type">Booking Type</label>
<select name="booking_type" id="booking_type" class="form-control" multiple>
<option value="one_way_transfer" {{{multiselect ptb.booking_type "one_way_transfer"}}}>One Way Transfer</option>
<option value="two_way_transfer" {{{multiselect ptb.booking_type "two_way_transfer"}}}>Two Way Transfer</option>
<option value="nightclub_tour" {{{multiselect ptb.booking_type "nightclub_tour"}}}>Nightclub Tour</option>
<option value="kids_bus_party" {{{multiselect ptb.booking_type "kids_bus_party"}}}>Kids Bus Party</option>
</select>
</div>
Worked like a charm! Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to create it for multiple selection.