v1.2.2

API 參考:JSONEditorNode

最後更新


本頁列出 JSONEditorNode 的公開欄位與方法。節點由 JSONEditor 內部建立,類別本身不對外匯出,透過 editor.children 與各節點的 children 存取。

屬性


屬性型別預設值說明
idstring64 字元隨機字串用於 key-{id}、value-{id} 等 DOM id
keystring""節點鍵名;陣列子節點為空字串。編輯時會去除換行與前後空白
typestring"string""string" / "number" / "boolean" / "object" / "array",見型別系統
valuestring""以字串保存的原始節點值(僅非容器型別有效)
parentJSONEditor | JSONEditorNode—父節點參照
childrenJSONEditorNode[][]子節點陣列(object / array 型別使用)
collapsedbooleanfalse目前折疊狀態;由 JSONEditor 建立的節點以 config.collapsed(預設 true)初始化

方法


方法說明
render()渲染並回傳此節點的 DOM 元素(呼叫私有 #create())
addChild()新增一個空子節點、插入 DOM 並觸發 Lifecycle.update;新節點不繼承 collapsed / confirmKeyRemove,見核心概念
updateChild()重建此節點 DOM 並觸發 Lifecycle.update
setCollapsed()切換折疊狀態並重建此節點 DOM,不觸發 Lifecycle.update
json(getter)回傳此節點對應的 JavaScript 值(遞迴),見序列化

範例


遞迴走訪 editor.children,印出每個節點的 key / type / value,最後印出第一個節點的 json:
  1. <section id="editor"></section>
  2. <script>
  3. const editor = new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. json: { user: { name: "Pardn", langs: ["Go", "JS"] } },
  7. when: {
  8. rendered: () => {
  9. const walk = (nodes, depth) => nodes.forEach((n) => {
  10. console.log(" ".repeat(depth) + (n.key || "-"), n.type, n.value);
  11. walk(n.children, depth + 1);
  12. });
  13. walk(editor.children, 0);
  14. console.log(editor.children[0].json);
  15. },
  16. },
  17. });
  18. </script>
即時預覽 · @pardnchiu/nanojson@1.2.2

延伸閱讀


EN