tabs.html 3.5 KB

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