jsoneditor icon indicating copy to clipboard operation
jsoneditor copied to clipboard

Selective expand based on node's value

Open inbasic opened this issue 13 years ago • 12 comments

It would be great if there is an option to decide whether a node show is expanded or not based on a user defined function.

In my case I just want to expand a single node.

inbasic avatar Jan 01 '13 19:01 inbasic

Do you have a more specific ideas on how this would work?

josdejong avatar Jan 01 '13 19:01 josdejong

Right now there is no method for selective expansion, I would like to have an option to help decide whether a node is expanded or not, something like this:

var editor = new JSONEditor(container, { isNodeExpanded: function (node) {... return node.value === "..."} });

inbasic avatar Jan 01 '13 19:01 inbasic

Do you want to be able for every individual node to expand it, collapse it, and check whether the node is expanded? (thus also be able to traverse over all nodes and their childs?)

josdejong avatar Jan 01 '13 20:01 josdejong

Yes, exactly. If I have understood the code correctly, right now during the tree construction, there is a this.expanded = false. I would prefer a user defined function decide on that.

inbasic avatar Jan 01 '13 20:01 inbasic

Ok got it. Interesting.

Maybe it is an idea not to expose the internal node object, but give a selection of useful parameters to the user defined function, something like this:

var editor = new JSONEditor(container, {
    expand: function (field, value, type, level) { ... }
});

Where the currently built in expand strategy is this:

var editor = new JSONEditor(container, {
    expand: function (field, value, type, level) { return (level == 0); }
});

josdejong avatar Jan 01 '13 20:01 josdejong

Great! Please keep me posted if there is any progress on this.

inbasic avatar Jan 01 '13 20:01 inbasic

+1

I just came here to look for alternatives for expandAll(), would love to expand selectively.

Madd0g avatar Oct 19 '14 18:10 Madd0g

This seems like a great idea! And i think in some way is related to #297.

Is there any work around right now? Or maybe hide some nodes from the editor?

Thanks for the great work with the editor!

zenzei avatar Aug 31 '16 18:08 zenzei

If you really need, you can traverse over the internal nodes and call expand/collapse on them individually. For example:

// expand/collapse the root node
editor.node.expand(false /*recurse*/)
editor.node.collapse(false /*recurse*/)

// expand the 3th child node of the root node
editor.node.childs[2].expand(false /*recurse*/)

WARNING: this is no official API, if you build upon these internal structures you're code may break in future versions.

josdejong avatar Sep 03 '16 07:09 josdejong

This is a quick function I wrote that collapses a certain property based on field name. In my particular use-case I have a nested property called "properties" that is always huge. I wanted to collapse it so it didnt take up so much scrolling space.

function close_properties_recursive(childs) {
    // Will look at every property in the jsoneditor window and collapse any called "properties".
    // This is just a look-and-feel thing because the properties value is always huge and I want to hide it.
    // editor.node.childs[2].childs[0].childs[4].collapse(true)

    for (let index = 0; index < childs.length; ++index) {
        const element = childs[index];
        if (element.hasOwnProperty("field") && element.field === "properties") {
            element.collapse(true)
        }
        if (element.hasOwnProperty("childs")) {
            close_properties_recursive(element.childs)
        }
    }
}

Called with close_properties_recursive(editor.node.childs)

mmssix avatar Feb 13 '24 19:02 mmssix

i got editor.nodes undefined for mode: code; is there any way to use the same technique for json code mode?

gerbil avatar Mar 15 '24 11:03 gerbil

@gerbil I don't think so.

josdejong avatar Mar 15 '24 15:03 josdejong