holiber / sl-vue-tree

Customizable draggable tree component for Vue.js
MIT License
344 stars 79 forks source link

Lost old data after toggle visibility #43

Closed kossa closed 5 years ago

kossa commented 5 years ago

Hello,

I have a categories system, with : id, name, parent_id, visible_on_menu

I put the name into title, id and visible_on_menu under data, the problem is once I update the visibility I lost the id I'm using this : https://github.com/holiber/sl-vue-tree/blob/master/demo/dark-theme.html#L134

kossa commented 5 years ago

image

holiber commented 5 years ago

Thanks for reporting this

holiber commented 5 years ago

I can't find any issues here. How do you update your data field? If you're using updateNode method, keep in mind that it doesn't patch the node recursively. It simply updates the node with Object.assign(nodeModel, patch); - line 650

kossa commented 5 years ago

I using the code on the demo, currently I added manualy the id slVueTree.updateNode(node.path, {data: { visible: visible, id: node.data.id}}); :

  ...
    toggleVisibility: function (event, node) {
        const slVueTree = this.$refs.slVueTree;
        event.stopPropagation();
        const visible = node.data && node.data.visible !== true;
        slVueTree.updateNode(node.path, {data: { visible: visible, id: node.data.id}});

        console.log('Toggle visible', node.data.id, visible);
      },
holiber commented 5 years ago

Just tested this code in test_toggle_visibility branch. It works for me. Are you up to date with the last version?

kossa commented 5 years ago

Alright you have to add the id manually :)

https://github.com/holiber/sl-vue-tree/commit/c9882584ff369131b7e99274de2ea419613c5f8b#diff-ecfbcb1c4778b2fc8a2fb4165903478eR138

kossa commented 5 years ago

Each time you change the visible property, you have to add all others data, for now I fixed the issue like that

holiber commented 5 years ago

Yes, there is no special method to patch the data field, you can only replace the whole object. In spite of this, you always have an old data object in the function's arguments. Just use spread syntax or Object.assign for convenience

    toggleVisibility: function (event, node) {
        ....
        slVueTree.updateNode(node.path, {data: { ...node.data, visible: visible}});
        ....
      },
kossa commented 5 years ago

Great thank you for help, I'm very happy that the plugin was integrated successfully with lazychaser/laravel-nestedset