Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created March 7, 2023 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/4580a0b116bbb6650df8c9be7a9bffaf to your computer and use it in GitHub Desktop.
Save bjoerntx/4580a0b116bbb6650df8c9be7a9bffaf to your computer and use it in GitHub Desktop.
function createHtmlForm(filename) {
var bDocument;
var serviceURL = "@Url.Action("GetFormFields", "Home")";
$.ajax({
type: "GET",
url: serviceURL + "?filename=" + filename,
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
$("#smartForm").empty();
var fieldset = $("<form><fieldset><legend>Form</legend></fieldset></form>").appendTo("#smartForm");
// loop through all fields and add input elements to dynamicly created form
data.forEach(formField => {
switch (formField.typeName) {
case "SmartTextFormField":
fieldset.append($("<div class='form-group'><label for='" + formField.name + "'>" + formField.displayName.toUpperCase() + "</label><input placeholder='Type in " + formField.name + "' class='form-control' name='" + formField.name + "' id='" + formField.name + "' type='text' value='" + formField.text + "' /></div>"));
break;
case "SmartCheckboxField":
var checked = "";
if (formField.Checked === true)
checked = "checked";
fieldset.append($("<div class='form-check'><input " + checked + " class='form-check-input' name='" + formField.name + "' id='" + formField.name + "' type='checkbox' /><label class='form-check-label' for='" + formField.name + "'>" + formField.displayName.toUpperCase() + "</label></div>"));
break;
case "SmartDateField":
fieldset.append($("<div class='form-group'><label for='" + formField.name + "'>" + formField.displayName.toUpperCase() + "</label><input class='form-control' name='" + formField.name + "' id='" + formField.name + "' type='date' value='" + formField.date + "' /></div>"));
break;
case "SmartDropdownField":
console.log("drop down");
var items;
formField.items.forEach(item => {
if (item === formField.text)
items += "<option selected>" + item + "</option>"
else
items += "<option>" + item + "</option>"
});
fieldset.append($("<div class='form-group'><label for='" + formField.name + "'>" + formField.displayName.toUpperCase() + "</label><select class='form-control' name='" + formField.name + "' id='" + formField.name + "'>" + items + "</div></div>"));
break;
}
});
}
function errorFunc() {
alert("Error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment