v1.2.2

API 參考:實例成員

最後更新


本頁列出 JSONEditor 實例建構後可用的屬性與方法。

屬性


body、editor、children 在資料載入完成後才以非同步方式指定,建構子剛回傳時尚未就緒,見核心概念。
成員型別說明
jsongetter → string序列化目前資料為 4 空格縮排的 JSON 字串;根層無鍵子節點會被忽略,見序列化
typegetter → string根節點型別;目前實作固定回傳 "object",匯入陣列也不會改變
childrenJSONEditorNode[]頂層子節點陣列
bodyHTMLElement編輯器根 DOM 元素(帶 pd-json-editor class)
editorHTMLElement節點容器 DOM 元素(<section>)
buttonArray初始值為 [],目前實作未使用

方法


方法回傳說明
import(data)Promise<void>匯入新資料(Object / File / URL string)並以 render(true) 重新渲染,見資料載入
export()void下載 NanoJSON-{timestamp}.json;根層只有一個無鍵子節點時仍會寫入(與 json getter 不同)
reset()void等同 import({});重設後沒有任何節點
enable()void解除唯讀;移除整棵已渲染 DOM 的 disabled 屬性
disable()void進入唯讀;為整棵已渲染 DOM 加上 disabled 屬性,見唯讀模式
render(isUpdate = false)void重新渲染所有頂層節點;僅當 isUpdate === true 且初次渲染已完成時觸發 Lifecycle.update
insert()void於根層新增一個空節點(型別 "string")並重新渲染,不觸發 Lifecycle.update

範例


初次渲染後讀取 editor.type,再以 import() 替換資料,updated 印出 editor.json:
  1. <section id="editor"></section>
  2. <script>
  3. const editor = new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. json: { name: "NanoJSON" },
  7. when: {
  8. rendered: () => {
  9. console.log("type:", editor.type);
  10. editor.import({ name: "NanoJSON", version: "1.2.1", tags: ["json"] });
  11. },
  12. updated: () => console.log(editor.json),
  13. },
  14. });
  15. </script>
即時預覽 · @pardnchiu/nanojson@1.2.2

延伸閱讀


EN