daweilv / treejs

A lightweight tree widget, compatible with VanillaJS / React / Vue. Tiny size after gzip. Zero dependence.
MIT License
141 stars 49 forks source link

`checked: true` boolean inversion issue #38

Open ForbiddenEra opened 5 months ago

ForbiddenEra commented 5 months ago

Hi,

If your tree array is as such:

let data = [
  {
    "id": "0",
    "text": "0",
    "checked": true,
    "children": [
      {
        "id": "0-0",
        "text": "0-0",
    "checked": true,
        "children": []
      }
    ]
  }
]

Then no nodes are selected. This is because the checked must be implemented as a toggle of sorts, inverting the previous value, so because the parent is checked, setting checked: true on a child causes the parent to be inverted, unchecking it and seemingly also unchecking the child.

This doesn't make intuitive sense and it took me a good few minutes to figure out what was going on in this case.

Because of this, you either need to set checked: true on the most-parent node that you want all children checked in, or, if you don't want all that parent's children checked, then you need to not have the property on the parent and only on the child nodes you want checked.

checked: true should mean I want that entry checked, regardless of any previous values; there should be no inversion toggling.

AR0101 commented 2 months ago

I had the same problem. I was able to resolve it by directly calling the setValues function from loaded when creating the Tree structure. Additionally, I set the status to 2 during creation. This method fixed the toggle issue of checked.