vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.15k stars 1.38k forks source link

Checkbox Check/Uncheck performance #2505

Closed sdraper69 closed 3 years ago

sdraper69 commented 3 years ago

Recently I asked on Stack Overflow but realised this is probably more suitable.

I have an array of nodes that I want to be checked/un-checked.

Currently, I am using the following function

this.tree.jstree('check_node', nodeArray);

However, this proves to be very slow, even with what I would class a relatively small number (200-500)

In the Chrome dev tools, I can see it's doing them all separately and a trigger function seems to be the main culprit.

image

Is there a better way of checking a lot of nodes all at once?

vakata commented 3 years ago

No, there is no way to achieve that, unless you want to manually set the 'checked' state on all nodes. It depends on how many check_node event listeners you have and what they do.

check_node itself does not do much - take a look at the source. How easy it will be depends on your use case, but generally - change the internal state manually, and the redraw. Only trigger events if needed, and call the undetermined state function (if necessary)

sdraper69 commented 3 years ago

thanks for the response. when you say "change the internal state manually, and the redraw." I'm not sure what you mean by that, are you able to explain further or point me to any documentation? cheers

vakata commented 3 years ago

The internal state is here: $('#tree-instance').jstree(true)._model.data - an object containing all tree nodes indexed by their IDs.

Each node has a state property, which in turn contains selected or checked booleans (checked is only there is you have explicitly set checkbox.tie_selection to false when creating the instance).

You can change the above boolean values and then call the redraw method: https://www.jstree.com/api/#/?q=redraw&f=redraw([full])

If you have three_state set to true and IF you have an issue with the tree you might also want to finally call _undetermined(): https://www.jstree.com/api/#/?q=und&f=_undetermined()

sdraper69 commented 3 years ago

That's great thanks for the pointers, it does seem to have cured my performance issue. Doing it this way doesn't seem to affect the parents of nested children (e.g. if you uncheck all the children the parent remains checked, or if only checking 1 child the parent doesn't change to the square checkbox thing). I would have thought the redraw would cure that but apparently not. is there a simple way to do the parents?

sdraper69 commented 3 years ago

Any thoughts on the above?

vakata commented 3 years ago

There is no dedicated function you can call, here is how jstree does it: https://github.com/vakata/jstree/blob/master/src/jstree.checkbox.js#L193

sdraper69 commented 3 years ago

Hi There,

I am still having trouble with this. I assume nothing has changed in the later versions that may improve this? I'm finding it difficult to follow the function you linked to in the last comment. Assuming it still doesn't exist would anyone be able to assist me in writing a function that updates the parents? I think this would be useful to have in jstree anyway?