1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Advanced JSON Editor Example</title>
<script src="../../../javascript/json-editor/jsoneditor.min.js"></script>
</head>
<body>
<h1>Advanced JSON Editor Example</h1>
<p>This example demonstrates the following:</p>
<ul>
<li>Loading external schemas via ajax (using $ref)</li>
<li>Setting the editor's value from javascript (try the Restore to Default button)</li>
<li>Validating the editor's contents (try setting name to an empty string)</li>
<li>Macro templates (try changing the city or state fields and watch the citystate field update automatically)</li>
<li>Enabling and disabling editor fields</li>
</ul>
<button id='submit'>Submit (console.log)</button>
<button id='restore'>Restore to Default</button>
<button id='enable_disable'>Disable/Enable Form</button>
<span id='valid_indicator'></span>
<div id='editor_holder'></div>
<script>
// This is the starting value for the editor
// We will use this to seed the initial editor
// and to provide a "Restore to Default" button.
var starting_value = [
{
name: "John Smith",
age: 35,
gender: "male",
location: {
city: "San Francisco",
state: "California",
citystate: ""
},
pets: [
{
name: "Spot",
type: "dog",
fixed: true
},
{
name: "Whiskers",
type: "cat",
fixed: false
}
]
}
];
// Initialize the editor
var editor = new JSONEditor(document.getElementById('editor_holder'),{
// Enable fetching schemas via ajax
ajax: true,
// The schema for the editor
schema: {
type: "array",
title: "People",
format: "tabs",
items: {
title: "Person",
headerTemplate: "{{i}} - {{self.name}}",
oneOf: [
{
$ref: "basic_person.json",
title: "Basic Person"
},
{
$ref: "person.json",
title: "Complex Person"
}
]
}
},
// Seed the form with a starting value
startval: starting_value,
// Disable additional properties
no_additional_properties: true,
// Require all properties by default
required_by_default: true
});
// Hook up the submit button to log to the console
document.getElementById('submit').addEventListener('click',function() {
// Get the value from the editor
console.log(editor.getValue());
});
// Hook up the Restore to Default button
document.getElementById('restore').addEventListener('click',function() {
editor.setValue(starting_value);
});
// Hook up the enable/disable button
document.getElementById('enable_disable').addEventListener('click',function() {
// Enable form
if(!editor.isEnabled()) {
editor.enable();
}
// Disable form
else {
editor.disable();
}
});
// Hook up the validation indicator to update its
// status whenever the editor changes
editor.on('change',function() {
// Get an array of errors from the validator
var errors = editor.validate();
var indicator = document.getElementById('valid_indicator');
// Not valid
if(errors.length) {
indicator.style.color = 'red';
indicator.textContent = "not valid";
}
// Valid
else {
indicator.style.color = 'green';
indicator.textContent = "valid";
}
});
</script>
</body>
</html>
|