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

New ClientChanges property in Q2 -- doesn't work properly for drag and drop

4 Answers 64 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ed
Top achievements
Rank 1
Ed asked on 28 Jul 2008, 06:33 PM
Hi there,

I am working with the new ClientChanges property for the RadTreeView, included in last week's Q2 release of the Telerik controls.  I am trying to edit, remove and drag and drop nodes on the client side and then process the changes all at once on the server, to minimise the number of post-backs.  First, I am calling the trackChanges() method of the client-side tree object in a Javascript function that handles the OnClientLoad event.  Then I have the following function to handle the OnClientNodeDropping event on the client:

      function ClientNodeDropping(sender, args)
      {
        // get source and destination nodes
        var sourceNode = args.get_sourceNode(), destNode = args.get_destNode();
        if (destNode)
        {
          destNode.get_nodes().add(sourceNode);
        }
      }

Finally, I have a Submit button, with a Click event handler that calls the commitChanges() method of the client-side tree object and then generates a post-back.

On the server, I iterate through the list of ClientChanges to build up a SQL string like this:

        foreach (ClientOperation<RadTreeNode> operation in tree.ClientChanges)
        {
            RadTreeNode node = operation.Item;
            switch (operation.Type)
            {
                case ClientOperationType.Insert:
                    SQLString.AppendLine("INSERT INTO ... VALUES ...");
                    break;
                case ClientOperationType.Remove:
                    SQLString.AppendLine("DELETE FROM ... WHERE ID = ...");
                case ClientOperationType.Update:
                    UpdateClientOperation<RadTreeNode> update = operation as UpdateClientOperation<RadTreeNode>;
                      SQLString.AppendLine("UPDATE ... SET ParentId = ... WHERE ID = ...");
            }
        }
        // now commit the changes to the database
        UpdateDatabase(SQLString.ToString());

The UpdateDatabase function executes the commands contained in the string.

This is working well apart from one crucial issue; whenever I drag and drop a node, the ClientChanges collection of operations includes one Remove operation for deleting the node from its original (source) parent, one Insert operation for inserting it under its new (destination) parent and then one Insert operation for *each and every child* that was also moved in the drag operation!  These children are already present in the database, so the code crashes at this point.

Am I doing something wrong, or is this a known issue?  If so, is there a work-around?

All help gratefully received,

Ed Graham

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 29 Jul 2008, 06:48 AM
Hello Ed,

This is actually by design. All nodes need to be inserted in order for the proper persistence after postback. If we move only the parent node all its children will be lost after postback (because they are not added in the target treeview).

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 29 Jul 2008, 06:59 AM
Fair enough, Albert -- thanks for the reply.  Could you explain how I should update the database in this situation?  Ordinarily, I can map the Insert, Update and Remove operations to INSERT, UPDATE and DELETE commands in SQL.  After dragging a node, however, the Update operations create nodes that are already in the database, and hence my code breaks.  Is there any way of disabling the logging of these operations?

Maybe I am actually on the wrong track entirely -- what I want to do is to make all the changes to the tree on the client, then (after pressing Submit) run through the changes on the server and commit them to the database.  What would you suggest?

Best regards,

Ed
0
Accepted
Atanas Korchev
Telerik team
answered on 29 Jul 2008, 07:11 AM
Hi Ed,

Perhaps you could remove the children of the parent node (during the Remove operation from the source treeview). It is logically to do so because otherwise its children would remain orphans. Then you would insert them again during the Insert. I hope this is a viable workaround. The other option is to check if the nodes from the Insert operation are not children of an already inserted node.

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 29 Jul 2008, 07:43 AM
I see.  Thanks again, Albert.
Tags
TreeView
Asked by
Ed
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Ed
Top achievements
Rank 1
Share this question
or