Last active
May 29, 2024 09:19
-
-
Save codeBelt/2e3db2ae6db24e86a9a6 to your computer and use it in GitHub Desktop.
Select & Multi Select Handlebar Block Helper
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
/** | |
* Sets the 'selected' property on the select option(s) when the value is passed in. | |
* | |
* @method selectHelper | |
* @example | |
* // selectValue = "69" | |
* // selectValue = "69,70,71" | |
* <select> | |
* {{#selectHelper selectValue}} | |
* <option value="Completed">Completed</option> | |
* <option value="OverDue">OverDue</option> | |
* <option value="SentToPayer">SentToPayer</option> | |
* <option value="None">None</option> | |
* {{/selectHelper}} | |
* </select> | |
*/ | |
Handlebars.registerHelper('selectHelper', function(selected, options) { | |
var html = options.fn(this); | |
if (selected) { | |
var values = selected.split(','); | |
var length = values.length; | |
for (var i = 0; i < length; i++) { | |
html = html.replace( new RegExp(' value=\"' + values[i] + '\"'), '$& selected="selected"'); | |
} | |
} | |
return html; | |
}); |
You're a lifesaver! Thank you so much.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone gets
you may need to convert selected parameter to string first. So add the below line of code after var html = options.fn(this);
var selected = selected + '';