v1.2.1

API 參考

最後更新


本頁列出 JSONEditor(src/model)constructor 的完整設定參數。

匯出方式


載入方式取得 JSONEditor
UMD(dist/NanoJSON.js)全域 window.JSONEditor
ES Module(dist/NanoJSON.esm.js)import { JSONEditor } from ".../NanoJSON.esm.js"

JSONEditor 是唯一匯出的類別;JSONEditorNode 與 Lifecycle 不對外匯出,只能經由 editor.children 取得節點實例。

Constructor


  1. new JSONEditor(config)

config 不是物件時印出 Failed to load config. 並結束。
參數型別預設值說明
idstring—掛載目標的 DOM 元素 id,其內容會被取代;省略則建立新的 <section>(不會自動掛載到頁面)
titlestring""於 <header> 中顯示的編輯器標題
descriptionstring""於 <header> 中顯示的編輯器描述;title 與 description 皆為空時不輸出 <header>
fillbooleantrue設定 data-fill="1"(或 "0"),控制是否填滿容器
readonlybooleanfalse以唯讀模式啟動,見唯讀模式
collapsedbooleantrue由初始資料、import()、insert() 建立的節點之初始折疊狀態;巢狀內手動新增的子節點不套用,見核心概念
confirmKeyRemovebooleantrue移除節點前是否顯示 confirm();套用範圍同 collapsed
cssstringhttps://cdn.jsdelivr.net/npm/@pardnchiu/nanojson@1.1.7/dist/NanoJSON.css自訂 CSS 路徑;未提供或空字串時使用預設值
fileFile | string—初始資料來源(File 物件或 URL 字串)
jsonobject | Array{}以 JavaScript 物件或陣列表示的初始資料
pathstring—URL 字串來源;三者以 file ?? json ?? path 取第一個非 null 值,見資料載入
button.importbooleantrue顯示 Open 按鈕與隱藏的檔案輸入框
button.exportbooleantrue顯示 Download 按鈕
button.resetbooleantrue顯示 Reset 按鈕
when.beforeRender() => boolean | void—見生命週期鉤子
when.rendered() => void—同上
when.beforeUpdate() => boolean | void—同上
when.updated() => void—同上
when.beforeDestroy() => boolean | void—同上(目前未被自動呼叫)
when.destroyed() => void—同上(目前未被自動呼叫)

button 不是物件時三個按鈕全部顯示;物件中缺少的鍵視為 true。

範例


標題、按鈕顯示與節點設定皆由 constructor config 決定:
  1. <section id="editor"></section>
  2. <script>
  3. new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. title: "NanoJSON",
  7. description: "Configured from the constructor",
  8. collapsed: false,
  9. confirmKeyRemove: false,
  10. button: { import: false, reset: false },
  11. json: { name: "NanoJSON", tags: ["json", "editor"] },
  12. });
  13. </script>
即時預覽 · @pardnchiu/nanojson@1.2.2

延伸閱讀


EN