zoukankan      html  css  js  c++  java
  • 递归获取子节点

    节点ID,父节点ID,根据节点获取该节点下所有子节点。用于点击类别查询此类别下所有商品

            private string GetChildIdsById(int id)
            {
                List<ProductCategoryModel> listCategory = GetDataCategory();
                string ids = id.ToString();
                 GetChilds(id, listCategory,ref ids);
                 return ids;
            }
            private void GetChilds(int id, List<ProductCategoryModel> listc,ref string ids)
            {
               
                foreach (var item in listc.Where(p => Convert.ToInt32(p.FId) == id).ToList())
                {
                    ids += "," + item.Id;
                    GetChilds(Convert.ToInt32(item.Id), listc,ref ids);
                }
                
            }
    

     递归绑定目录(Winform)

            private void BindTreeView()
            {
                try
                {
                    treeView1.BeginUpdate();
                    treeView1.Nodes.Clear();
                    List<ProductCategoryModel> listCategory = GetDataCategory();
                    if (listCategory.Count > 0)
                    {
                        //绑定根目录
                        ProductCategoryModel model = listCategory.Where(a => a.FId == -1).FirstOrDefault();
                        if (model != null)
                        {
                            TreeNode rootNode = new TreeNode();
                            rootNode.Text = model.Name;
                            rootNode.Tag = model.Id.ToString();
                            rootNode.ImageIndex = 0;
                            treeView1.Nodes.Add(rootNode);
                            AddNodes(rootNode, listCategory);
                        }
                        if (treeView1.Nodes.Count > 0)
                        {
                            treeView1.ExpandAll();
                        }
                    }
                    treeView1.EndUpdate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示");
                }
    
            }
            private static void AddNodes(TreeNode pNode, List<ProductCategoryModel> listc)
            {
                pNode.Nodes.Clear();
                foreach (var item in listc.Where(p => p.FId == Convert.ToInt32(pNode.Tag)).ToList())
                {
                    TreeNode parentNode = new TreeNode();
                    parentNode.Text = item.Name;
                    parentNode.Tag = item.Id;
                    parentNode.ImageIndex = 1;
                    AddNodes(parentNode, listc);
                    pNode.Nodes.Add(parentNode);
                }
            }
    

      

     

  • 相关阅读:
    iOS 程序内国际化的一些心得(2)
    iOS 程序内国际化的一些心得(1)
    写个自己的Xcode4插件
    iOS命令行获取工程内所有的国际化资源并且整合
    用xib自定义UITableViewCell的注意事项——重用问题
    遍历类成员
    iOS 键盘取消晃动撤销动作
    iap验证。
    4 WPF学习---系统的学习XAML语法
    webService访问加密-Soapheader
  • 原文地址:https://www.cnblogs.com/huangzhen22/p/4760082.html
Copyright © 2011-2022 走看看