select2.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>JSON Editor Select2 Integration Example</title>
  6. <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor/dist/jsoneditor.min.js"></script>
  7. <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  8. <script src="//cdn.jsdelivr.net/select2/3.4.8/select2.min.js"></script>
  9. <link rel="stylesheet" href="//cdn.jsdelivr.net/select2/3.4.8/select2.css">
  10. </head>
  11. <body>
  12. <h1>JSON Editor Select2 Integration Example</h1>
  13. <p style='margin-bottom:20px;'>This example demonstrates JSONEditor's integration with Select2</p>
  14. <div id='editor_holder'></div>
  15. <button id='submit'>Submit (console.log)</button>
  16. <script>
  17. // Global Select2 options
  18. JSONEditor.plugins.select2.width = "300px";
  19. // Initialize the editor with a JSON schema
  20. var editor = new JSONEditor(document.getElementById('editor_holder'),{
  21. schema: {
  22. type: "object",
  23. title: "Text",
  24. required: ["fontSize","color","font","weight","tags","possibleFonts"],
  25. properties: {
  26. fontSize: {
  27. type: "integer",
  28. enum: [10,11,12,14,16,18,20,22,24,36,48,60,72,100],
  29. default: 24,
  30. options: {
  31. // Override defaullt options
  32. select2_options: {
  33. width: 'off'
  34. }
  35. }
  36. },
  37. color: {
  38. type: "string",
  39. enum: ["black","red","green","blue","yellow","orange","purple","brown","white","cyan","maagenta"]
  40. },
  41. font: {
  42. type: "string",
  43. enumSource: "possible_fonts",
  44. watch: {
  45. "possible_fonts": "root.possibleFonts"
  46. },
  47. },
  48. weight: {
  49. type: "string",
  50. enum: ["normal","bold","bolder","lighter"],
  51. options: {
  52. enum_titles: ["Normal (400)","Bold (700)","Bolder (900)","Lighter (200)"]
  53. }
  54. },
  55. tags: {
  56. type: "array",
  57. uniqueItems: true,
  58. format: "select",
  59. items: {
  60. type: "string",
  61. enum: ["bold","italic","smallcaps"]
  62. }
  63. },
  64. possibleFonts: {
  65. type: "array",
  66. format: 'table',
  67. items: {
  68. type: "string"
  69. },
  70. default: ["Arial","Times","Helvetica","Comic Sans"],
  71. options: {
  72. collapsed: true
  73. }
  74. }
  75. }
  76. },
  77. startval: {
  78. color: "red"
  79. }
  80. });
  81. // Hook up the submit button to log to the console
  82. document.getElementById('submit').addEventListener('click',function() {
  83. // Get the value from the editor
  84. console.log(editor.getValue());
  85. });
  86. </script>
  87. </body>
  88. </html>