This is a migrated thread and some comments may be shown as answers.

Dublicate Node Delete

4 Answers 118 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Telesto
Top achievements
Rank 1
Telesto asked on 14 Jan 2013, 01:35 PM
Hi all,

So I'm adding some duplicate nodes on my tree like this:

var fatherNode_DataItem = treeview.dataItem(treeview.select());
 
var nodeText = prompt("Please enter desired Node Name to be copied:","default");
 
var dataItem_toBeCopied = treeview.dataItem(treeview.findByText(nodeText));
 
fatherNode_DataItem.append(dataItem_toBeCopied);

So I get a copy of the node I inputed on the prompt.
Which means when I select one of the copies,all the node copies and the original are selected.
And if I change the dataItem attributes of one of the copies,all the dataItems are changed even though separate copies for each one of the copies is kept on the data Source.
I am ok with that,it's what I wanted.

The problem is that I want to delete one of the copies later,but If I try
var dataItem = treeview.dataItem(treeview.select());
treeview.remove(dataItem);
all the copies and the original are deleted alltogether!
I also tried:
$("#treeview").find(".k-state-selected").each(function() {
        if(confirm("Delete: " + $(this).text() + " with Father: " + treeview.text(treeview.parent($(this).closest(".k-item"))))) {
          treeview.remove($(this).closest(".k-item"));
    }
});
But on the first "ok" of a confirm, every node copy along with the original gets deleted again...not only the copy that I clicked ok for...

Is there a way I can delete only one of the copies based on it's parent?? (the copies will never have the same parent that's why I want to select which one to delete based on their parents)

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Jan 2013, 12:56 PM
Hello Telesto,

All items are deleted, because you are appending the same observable object. You should copy the properties that you wish to persist to a new object and then append it to the TreeView in order to avoid removing all. 

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Telesto
Top achievements
Rank 1
answered on 17 Jan 2013, 09:35 AM
Hey Daniel,
Thanks for your reply.However if I copy the properties and append a new object I will lose the concurrency of the items later on!Isn't that true?For example if later I change a property of the original object,it's copies won't be subjects to the change as they are now...

But even if I implement a workaround for this by repeating the change in every object with the same ID,I don't know from the start what properties will the object have,and some new ones may be added later and I will have the concurrency issue again which I may solve again with the repeating process but this will require searching the tree everytime and I fear this may be time-consuming...

Is there no way to work around this without changing the way I append?

For example if I search the tree dataSource with a variation of the code found in the documentation for getting the checked nodes:
var selNodeFathers = [];
gatherSelectedNodeFathers(treeview.dataSource.view(),selNodeFathers);
function gatherSelectedNodeFathers(nodes,selectedNodeFathers) {
    for (var i = 0; i < nodes.length; i++) {
        if ( (nodes[i].parentNode() != undefined) && (nodes[i].selected) ) {
            selectedNodeFathers.push(nodes[i].parentNode().text);
            alert("Father: " + nodes[i].parent.text);
        }
        if (nodes[i].hasChildren) {
            gatherSelectedNodeFathers(nodes[i].children.view(),selectedNodeFathers);
        }
    }
}
Instead of running through each dataItem once and show me two different fathers,this code will give me the last father a number of times equal to the number of the object existing in the tree...
Shouldn't this go through each object in the dataSource and check in which object's items property the running object exists since I ask for the parent?
0
Daniel
Telerik team
answered on 21 Jan 2013, 08:04 AM
Hello Telesto,

Yes, the changes will not be reflected because the object will no longer be the same. You could remove only the HTML element when the same object is used or append a copy and then apply the changes to all instances when a property is changed. 

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Telesto
Top achievements
Rank 1
answered on 21 Jan 2013, 12:12 PM
Well I figured a workaround for this,
In case someone else has the same problem,here is the code:
var txt = prompt("Enter Node to be Copied","default");
var fatherDataItem = treeview.dataItem(treeview.select());
 
var dataItem = treeview.dataItem(treeview.findByText(txt));
var dataToAppend = dataItem.toJSON();
fatherDataItem.append(dataToApp);
And after I have the copy I have a form bound to an observable viewModel to change the values of the current node.
But with the following code I run the whole tree and change the values in every node with the same ID:
$("#saveData").click(function() {
    findNodeCopies(treeview.dataSource.view(),dataItem.get("ID"));
    });
 
function findNodeCopies(nodes,id) {
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].ID == id && nodes[i].is == "node") {
            var dataItm = treeview.dataItem(treeview.findByText(nodes[i].text));
            dataItm.set("text", viewModel.nodeName);
            dataItm.set("ID", viewModel.nodeID);
        }
        if (nodes[i].hasChildren) {
            findNodeCopies(nodes[i].children.view(),id);
        }
    }
}
Hope this helps.
Even though I dont really like it since It has to run through the whole tree everytime I change something,there doesn't seem to be another way currently.

Daniel thanks for your time and help :)
Tags
TreeView
Asked by
Telesto
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Telesto
Top achievements
Rank 1
Share this question
or