Tree control drag-and-drop sorting

Source: Internet
Author: User

Last time I shared "a drag-and-drop asynchronous load tree on demand", but I didn't implement the drag-and-drop operation to update the database. Today I took the time to implement it. Here I will share my thoughts and key points.Code.

Drag method entry:

 
FunctionOndrop (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 ("An error occurred while updating the order number. Please try again later! ");}},"Text");}}

Here, you only need to pass two parameters to the Background: The current drag node ID and the drag target ID. The drag and drop operations are divided into drag-up and drag-down operations, the updated sorting numbers in the database are different here.Algorithm.

The Code is as follows:

         Public  Boolean updatefiletypesortnum (string treenodeid, string targetnodeid ){  Using (Sqlconnection conn = Dapperfactory. crateopenconnection ()){  //  Determine whether to move the node up or down                  VaR Treenode = getfiletype ( New  GUID (treenodeid ));  VaR Targetnode = getfiletype ( New  GUID (targetnodeid ));  If (Treenode. sortnum <= Targetnode. sortnum ){  //  Move down String executesql = string. Format ( @"  Update filetype set sortnum = sortnum + 2 where ID in (select. ID from [filetype] A, filetype B where B. id = '{0}' and. parentid = B. parentid and. 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 = string. Format ( @"  Update filetype set sortnum = sortnum + 1 where ID in (select. ID from [filetype] A, filetype B where B. id = '{0}' and. parentid = B. parentid and B. sortnum <=. 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, we have achieved great success. If you think it is good, please click on the recommendation. Thank you.

If you have any questions, please provide more valuable comments.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.