Home > Software engineering >  I can't add child elements as treetable dynamic
I can't add child elements as treetable dynamic

Time:11-28

I want to add elements to treetable dynamically.but I can't combine arrays when merging.

const nodes=([]);
const children=([])

then I add an element to the nodes array

 nodes.push({
        key: keyIndex.value,
        data:{
          "name":newExplanation.value,
          "size":"100kb",
          "type":"Folder"

        },
      })

This is how I push on my second string

children.push({
            children:[
              {
                key: tempRoot.value   '-0' ,
                data: {
                  "name": Explanation.value,
                  "size": "100kb",
                  "type": "Folder"

                }
              }]

          })

then I want to add a child node element under this element and I want to combine these two arrays. But they don't get together, they seem like two separate elements in an array. for example nodes[0:{first element}, {second element]

Here's how I do the merge

let r = nodes.concat({...children})

CodePudding user response:

What if you push in children in the same way as in nodes like this

children.push({
    key: tempRoot.value   '-0' ,
    data: {
        "name": Explanation.value,
        "size": "100kb",
        "type": "Folder"
    }
});

and then concatenate like this

let r = nodes.concat(children);

CodePudding user response:

Try like following snippet:

const nodes=([]);
const children=([])

nodes.push({key: 1, data:{"name":11, "size":"100kb", "type":"Folder"},})

children.push({children:[{key: 12   '-0', data: {"name": 123, "size": "100kb", "type": "Folder"}}]})
          
let r = nodes.concat(...children[0].children)
console.log(r)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related