Ztree using series four (Ztree to implement sibling drag-and-drop sorting and to save the sorted results to the database)

Source: Internet
Author: User
Tags set time

Ztree, the last of the series, is also one of the powerful embodiment of ztree-the sorting function.

Ztree enables arbitrary drag-and-drop sorting between all nodes. I am here to implement the only consent to the same level of arbitrary drag-and-drop ordering, in fact, the same principle, just a narrow range, a little more inferences. The following is the code for each layer. Or just post the function code. Because of this drag-and-drop sorting function I feel very good, so a separate blog to show, but also easy to understand.

The JS code for JSP page implementation features such as the following:

     Run Var dragid before dragging; function Beforedrag (Treeid, TreeNodes) {for (var i=0,l=treenodes.length; i<l; i++) {drag                 Id = Treenodes[i].pid;                if (Treenodes[i].drag = = = False) {return false;     }} return true; }//Drag release to run function Beforedrop (Treeid, TreeNodes, TargetNode, Movetype) {if (Targetnode.pid = = Drag                  ID) {var data = {Id:treenodes[0].id,targetid:targetnode.id,movetype:movetype};                  var confirmval = false;                       $.ajax ({async:false, type: "Post", Data:data, URL: "<%=request.getcontextpath ()%>/library/updatelibrarysort/", success:funct  Ion (JSON) {if (json== "Success") {Confirmval = true;                             Alert (' Operation succeeded! '); } else{alert (' Pro, Operation failed '); }}, Error:function () {alert (' Pro, the network is a bit of a force.                       ');                 }                  });             return confirmval; } else{alert (' Pro, can only be sorted in sibling order!

'); return false; } }

Controller layer code such as the following:

     /**     * Update sorting sort     * @return     *     /@RequestMapping (value= "updatelibrarysort/")     @ResponseBody     Public String Updatelibrarysort (httpservletrequest request, HttpServletResponse Response,libraryvo Libraryvo) {          String Createname= (String) request.getsession (). getattribute (constants.current_user_name);          Return Libraryservice.updatelibrarysort (Libraryvo,createname);     }

Service layer code such as the following:

     /** * Update Sort * @return */public String updatelibrarysort (Libraryvo libraryvo,string userName) {          int numflag = 0; Library Libraryt = Librarymapper.selectbyprimarykey (Libraryvo.gettargetid ());//target classification Information Library Library = Libraryma          Pper.selectbyprimarykey (Libraryvo.getid ());//Drag the Classification information library.settarget_order_id (LIBRARYT.GETORDER_ID ()); Library.setupdate_time (New Date ());//Set Time Library.setupdate_user (userName);//Set operator libraryt.setupdate_t IME (New Date ());//Set Time Libraryt.setupdate_user (userName);//set operator map<string,object> Librarymap = n EW hashmap<string, object> ();//Parameter Set if (Libraryvo.getmovetype (). Equals ("prev")) {//move forward Libr               Arymap.put ("pid", Library.getpid ());               Librarymap.put ("order_id", library.gettarget_order_id ());               Librarymap.put ("target_order_id", library.getorder_id ()); list<library> Listlibraryflag = LibrarymApper.findlibrarylistbyorderid (LIBRARYMAP);               int order_id = libraryt.getorder_id () + 1;               LIBRARY.SETORDER_ID (libraryt.getorder_id ());               LIBRARYT.SETORDER_ID (order_id); for (int i = 0; i < listlibraryflag.size (); i++) {//Update all affected sort fields Library ly = Listlibraryflag.get (i)                    ; if (! ( Ly.getid ()). Equals (Library.getid ()) &&!                         (Ly.getid ()). Equals (Libraryt.getid ())) {Ly.setupdate_time (New Date ());                         Ly.setupdate_user (UserName);                         LY.SETORDER_ID (ly.getorder_id () +1); Librarymapper.updateorderid (ly);//update other affected categorical information}} Librarymapper.updateorde RId (library);//Update Drag classification Information Numflag = Librarymapper.updateorderid (LIBRARYT);//Update target classification information}else if (library              Vo.getmovetype (). Equals ("Next")) {//Move backwards librarymap.put ("pid", Library.getpid ()); Librarymap.put ("order_id", library.getorder_id ());               Librarymap.put ("target_order_id", library.gettarget_order_id ());               list<library> Listlibraryflag = Librarymapper.findlibrarylistbyorderid (Librarymap);               int order_id = libraryt.getorder_id ();               LIBRARY.SETORDER_ID (order_id);               LIBRARYT.SETORDER_ID (order_id-1); for (int i = 0; i < listlibraryflag.size (); i++) {//Update all affected sort fields Library ly = Listlibraryflag.get (i)                    ; if (! ( Ly.getid ()). Equals (Library.getid ()) &&!                         (Ly.getid ()). Equals (Libraryt.getid ())) {Ly.setupdate_time (New Date ());                         Ly.setupdate_user (UserName);                         LY.SETORDER_ID (ly.getorder_id ()-1); Librarymapper.updateorderid (ly);//update other affected categorical information}} Librarymapper.updateorde RId (library);//Update Drag classification Information Numflag = LibRarymapper.updateorderid (LIBRARYT);//Update target classification information}else{//} return Serviceutil.retu     Rnstring (Numflag); }

Mapper layer code such as the following

     /**     * Query all affected information according to PID, order_id and target_order_id     * @param pid     * @param order_id     * @param target_order_ ID     * @return     *     /Public list<library> Findlibrarylistbyorderid (map<string,object> Library);         /** *     Update     * @param library     * @return * *     /public     int Updateorderid (library library);
The corresponding XML code is as follows:

  < SQL id= "Base_column_list" >    ID, PID, name, Create_time, Update_time, Is_delete, Update_user, Create_user, le vel_id,    order_id  </sql>  <!--update order_id-  < update id= "Updateorderid" Parametertype= "Library" >           update onair_vms_library            <set >                update_time = #{update_time},                Update_user = #{update_user},                order_id = #{order_id}            </set >           where id = #{id}  </Update >   <!--query All affected information based on PID, order_id and target_order_id--  < select id= "Findlibrarylistbyorderid" resultmap= "Baseresultmap" parametertype = "Java.util.Map" >     select     <include refid = "Base_column_list "/> from     onair_vms_library     where  order_id between  #{order_id} and  #{target_order_id}  and  pid = #{pid}  </select>


Written in general, the wrong place to welcome correct, there is an efficient or more convenient way to share, especially when the sorting function to save the time to assume that you can not use a loop of SQL can be done more powerful. You are welcome to advise.

Ztree using series four (Ztree to implement sibling drag-and-drop sorting and to save the sorted results to the database)

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.