treejs icon indicating copy to clipboard operation
treejs copied to clipboard

Submit Checkboxes to form

Open trymeouteh opened this issue 2 years ago • 1 comments

Is it possible for the tree to use checkboxes to allow for the tree values to be submitted in a form?

trymeouteh avatar Apr 04 '23 22:04 trymeouteh

you could easily do that using the onChange method of the tree itself:

html:

<form ...>
    <input id="selectednodesformfield" type="hidden" name="selectednodes" />
</form>

js:

	let selectedNodes = [];
	let tree = new Tree('<selector>', {

		...,
        onChange: function() {
            selectedNodes = [];
            this.selectedNodes.forEach( function(node){
                selectedTree.push(node.id);
            });
            document.getElementById('#selectednodesformfield').value = JSON.stringify(selectedNodes);
        },
        ...
    });

Now you have an JSON encoded string of all node Ids in a formfield and you can work with that at the target of your form submission.

THenkeDE avatar Oct 17 '23 07:10 THenkeDE