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.