advanced.html 3.9 KB

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