Browse Source

清单模板支持多选删除

zhangweicheng 7 years ago
parent
commit
790831a6c9

+ 51 - 15
public/web/id_tree.js

@@ -579,30 +579,35 @@ var idTree = {
 
         Tree.prototype.delete = function (node) {
             var success = false, that = this;
-            var deleteIdIndex = function (nodes) {
+            if(node) success = that.m_delete([node]);
+            return success;
+        };
+        Tree.prototype.m_delete = function (nodes) {
+            let success = false, that = this;
+            let deleteIdIndex = function (nodes) {
                 nodes.forEach(function (node) {
                     delete that.nodes[that.prefix + node.getID()];
                     deleteIdIndex(node.children);
                 })
-            }
-            if (node) {
-                deleteIdIndex([node]);
-                //delete this.nodes[this.prefix + node.getID()];
-                if (node.preSibling) {
-                    node.preSibling.setNextSibling(node.nextSibling);
-                } else if (node.nextSibling) {
-                    node.nextSibling.preSibling = null;
-                }
-                if (node.parent) {
-                    node.parent.children.splice(node.siblingIndex(), 1);
+            };
+            for(let n of nodes){
+                deleteIdIndex([n]);
+                if (n.preSibling) {
+                    n.preSibling.setNextSibling(n.nextSibling);
+                } else if (n.nextSibling) {
+                    n.nextSibling.preSibling = null;
+                }
+                if (n.parent) {
+                    n.parent.children.splice(n.siblingIndex(), 1);
                 } else {
-                    this.roots.splice(node.siblingIndex(), 1);
+                    this.roots.splice(n.siblingIndex(), 1);
                 }
-                tools.sortTreeItems(this);
-                success = true;
             }
+            tools.sortTreeItems(this);
+            success = true;
             return success;
         };
+
         Tree.prototype.getDeleteData = function (node) {
             var data = [];
             var addUpdateDataForDelete = function (datas, nodes) {
@@ -621,6 +626,37 @@ var idTree = {
             }
             return data;
         };
+        Tree.prototype.getDeleteDatas = function (deleteMap,deleteNodes){//批量删除
+            let datas = [];
+            addDeleteDatas(datas,deleteNodes);
+            for(let d of deleteNodes){
+                addPreUpdateData(datas,deleteMap,d.preSibling,d.nextSibling);
+            }
+           function addPreUpdateData(updateDatas,map,preSibling,nextSibling) {
+               if(preSibling && (map[preSibling.getID()] == undefined || map[preSibling.getID()] == null)){
+                   if(nextSibling){
+                      if(map[nextSibling.getID()]){//如果下一个节点也是要删除的,则再往下顺延
+                          addPreUpdateData(updateDatas,map,preSibling,nextSibling.nextSibling);
+                      }else {
+                          updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),nextSibling.getID())});
+                      }
+                   }else {
+                       updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),-1)});
+                   }
+               }
+           }
+
+            function addDeleteDatas(dataArray,nodes) {
+                for(let n of nodes){
+                    let delData = {};
+                    delData[n.tree.setting.id] = n.getID();
+                    dataArray.push({type: 'delete', data: delData});
+                    addDeleteDatas(dataArray,n.children);
+                }
+            }
+            return datas;
+        };
+
 
         /*Tree.prototype.editedData = function (field, id, newText) {
          var node = this.findNode(id), result = {allow: false, nodes: []};

+ 16 - 0
public/web/tree_sheet/tree_sheet_controller.js

@@ -113,6 +113,22 @@ var TREE_SHEET_CONTROLLER = {
                 }
             }
         };
+        controller.prototype.m_delete = function (nodes) {
+            var that = this, sels = this.sheet.getSelections();
+            if(nodes.length > 0){
+                if(this.tree.m_delete(nodes)){
+                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
+                        let rowCount = 0;
+                        for(let node of nodes){
+                            rowCount = rowCount+node.posterityCount() + 1;
+                        }
+                        that.sheet.deleteRows(sels[0].row, rowCount);
+                        that.setTreeSelected(that.tree.items[sels[0].row]);
+                        that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
+                    });
+                }
+            }
+        };
         controller.prototype.upLevel = function () {
             var that = this;
             if (this.tree.selected) {

+ 33 - 4
web/users/js/template.js

@@ -413,15 +413,44 @@ $(document).ready(function () {
     $('#delete').click(function () {
         let me = this;
         $(me).addClass('disabled');
-        var selected = controller.tree.selected, updateData;
-        if (selected) {
-            updateData = controller.tree.getDeleteData(selected);
+        let selection = controller.sheet.getSelections()[0], updateData,deleteMap={},deleteNodes=[];
+        for(let i=0;i < selection.rowCount;i++){
+            let tem_node = controller.tree.items[selection.row+i];
+            if(i == 0){//第一个直接添加;
+                deleteMap[tem_node.getID()] = tem_node;
+                deleteNodes.push(tem_node); 
+            }else {
+                setNodeToMapAndArray(tem_node,deleteMap,deleteNodes);
+            }
+        }
+
+        if (deleteNodes.length > 0) {
+            updateData = controller.tree.getDeleteDatas(deleteMap,deleteNodes);
             CommonAjax.post(updateUrl, updateData, function (data) {
-                controller.delete();
+                controller.m_delete(deleteNodes);
                 controller.showTreeData();
                 $(me).removeClass('disabled');
             });
         }
+        function setNodeToMapAndArray(node,map,array) {
+            let nodeID = node.getID();
+            if(map[nodeID]==undefined||map[nodeID]==null){
+                newMap(node,node.parent,map,array)
+            }
+            function newMap(node,parent,map,array) {
+                let nodeID =node.getID();
+                if(parent==null){//说明已经是最顶层了
+                    map[nodeID]=node;
+                    array.push(node);
+                }else {
+                    let parentID = parent.getID();
+                    if(map[parentID]==undefined||map[parentID]==null){
+                        newMap(node,parent.parent,map,array);
+                    }
+                }
+            }
+        }
+        
     });
     $('#upLevel').click(function () {
         let me = this;

File diff suppressed because it is too large
+ 1 - 1
web/users/views/layout/layout.html