Sort tree control drag

Last sharing "can be a drag on demand asynchronous loading tree", but did not realize update the database after dragging, he realized taking the time today to share ideas and key here under the code.

Drag method entry:

        function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
            if (targetNode != null) {
                $.post("/Ashx/FileType.ashx?action=DragSortNum", { treeNodesId: treeNodes[0].id, targetNodeId: targetNode.id }, function (txt) {
                    if (txt != "OK") {
                        Alert ( "Update sort number failed, please try again later!" );
                    }
                }, "text");
            }
        }

Here only need to pass two parameters to the background, are the current drag and drop target node ID ID, divided into up dragging down the drag and drag, there are sort of database update numbers are different algorithm.

Detailed code is as follows:

        public Boolean UpdateFileTypeSortNum(String treeNodeId, String targetNodeId)
        {
            using (SqlConnection conn = DapperFactory.CrateOpenConnection())
            {
                // determination node is moving up or moving down 
                var the treeNode = GetFileType ( new new the Guid (treeNodeId));
                 var targetNode = GetFileType ( new new the Guid (targetNodeId));

                if (treeNode.SortNum <= targetNode.SortNum)
                {
                    // move down 
                    String = executeSql the String.Format ( @ " UPDATE FileType the SET SortNum SortNum + 2 =
                                    WHERE ID in(SELECT A.ID from [FileType] A,FileType B 
                                                where B.ID='{0}'
                                                and A.ParentId=B.ParentId and A.SortNum >= B.SortNum) ", targetNodeId);
                    conn.Execute(executeSql, null);

                    executeSql = String.Format(@"update t1 set t1.SortNum=t2.SortNum+1
                                from FileType t1,FileType t2 
                                where t1.ID='{0}' and t2.ID='{1}'", treeNodeId, targetNodeId);
                    conn.Execute(executeSql, null);
                }
                else
                {
                    // Move Up 
                    String = executeSql the String.Format ( @ " UPDATE FileType the SET SortNum SortNum + 1 =
                                    WHERE ID in(SELECT A.ID from [FileType] A,FileType B 
                                                where B.ID='{0}'
                                                and A.ParentId=B.ParentId and B.SortNum <= A.SortNum) ", targetNodeId);
                    conn.Execute(executeSql, null);

                    executeSql = String.Format(@"update t1 set t1.SortNum=t2.SortNum-1
                                from FileType t1,FileType t2 
                                where t1.ID='{0}' and t2.ID='{1}'", treeNodeId, targetNodeId);
                    conn.Execute(executeSql, null);
                }
                return true;
            }
        }

At this point, hit the big mess, if you feel good, please click on the recommendation, thank you.

If insufficient, like him valuable advice.

Demo URL: http://www.qicheba.net/FileManage/TypeManage  , welcome to play.

Reproduced in: https: //www.cnblogs.com/ushou/archive/2013/02/16/2913541.html

Guess you like

Origin blog.csdn.net/weixin_34212189/article/details/93162376