Created
December 23, 2013 23:09
-
-
Save elranu/8106375 to your computer and use it in GitHub Desktop.
json to html Form simple script
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="json to html form" /> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<form id="modelForm" novalidate > | |
</form> | |
</body> | |
</html> |
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
//Json to html form | |
//replace the var model with your json | |
var model ={name: "pepe", email : "[email protected]", age: "56"}; | |
String.prototype.format = function() { | |
var formatted = this; | |
for( var arg in arguments ) { | |
formatted = formatted.replace("{" + arg + "}", arguments[arg]); | |
} | |
return formatted; | |
}; | |
var htmlfieldset = '<fieldset><label for="field">{0} : </label><input ng-model="model.account.{1}" type="text"></fieldset>'; | |
var fields = []; | |
for(var fieldname in model ){ | |
fields.push({ name: fieldname , value: model[fieldname] }); | |
} | |
for(var index in fields){ | |
var htmlToIsnert = htmlfieldset.format(fields[index].name, fields[index].name); | |
$("form#modelForm").append(htmlToIsnert); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment