treejs
treejs copied to clipboard
Submit Checkboxes to form
Is it possible for the tree to use checkboxes to allow for the tree values to be submitted in a form?
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.