【工具类代码】递归获取所有的子节点做删除获取用

sql语句删除父子集

              string sql = "Delete ProjectFileManagement where IDNumber in ("+ids+")";
            //删除所选择的数据
            DBHelper.ExecCommand(sql, null);
            //删除id-pid所对应的垃圾数据
            sql = "Delete ProjectFileManagement where pid>0 and pid not in (select IDNumber from [dbo].[ProjectFileManagement])";
            DBHelper.ExecCommand(sql, null);

 

 

数据递归

//pid 就是你选中需要删除的ID

//includeParent=true代表返回的集合是否包含pid这条对象

public List

GetAllChilder(String pid,boolean includeParent) {
  List menuList=new ArrayList();
  if(includeParent==true)
  {

//如果==true,根据pid去找到这个条对象,后台方法类是where id=pid
   menuList.add(Menu.me.findById(pid));
  }

//递归
  GetMenuChilder(menuList,pid);
  return menuList;
 }
 
 private void GetMenuChilder(List

menuList,String pid)
 {
  List list = Menu.me.find("select * from Menu where pid = ?",pid);
  for(Menu menu : list)
  {
   menuList.add(menu);
   GetMenuChilder(menuList,menu.getStr("ID"));
  }
 }

 

上面代码可以做获取递归的数据,也可以做删除,删除父节点的时候 获取所有的子节点,然后循环删除返回子节点集合

 

前台递归,返回一个id-pid模式的list json调用下面方法 会给你返回 childer[]这样的数据对象,用来做一些树形的东西不错

//将ID、ParentID这种数据格式转换为树格式
 function arrayToTree(data, id, pid)     
        {
            if (!data || !data.length) return [];
            var targetData = [];                    //存储数据的容器(返回)
            var records = {};
            var itemLength = data.length;           //数据集合的个数
            for (var i = 0; i < itemLength; i++) {
                var o = data[i];
                records[o[id]] = o;
            }
            for (var i = 0; i < itemLength; i++) {
                var currentData = data[i];
                var parentData = records[currentData[pid]];
                if (!parentData) {
                    targetData.push(currentData);
                    continue;
                }
                parentData.children = parentData.children || [];
                parentData.children.push(currentData);
            }
            return targetData;
        }
       

你可能感兴趣的:(【工具类代码】递归获取所有的子节点做删除获取用)