Total Pageviews

Monday, August 24, 2020

 Updating a hierarchy path


  static void GeneratePath(List<HierarchyNode> all, HierarchyNode current)

        {

            string path = "";

            Action<List<HierarchyNode>, HierarchyNode> GetPath = null;


            GetPath = (List<HierarchyNode> ps, HierarchyNode p) => {

                var parents = all.Where(x => x.HierarchyNodeId == p.ParentNodeId);

                foreach (var parent in parents)

                {

                    path += $"/{ parent.Name}";

                    GetPath(ps, parent);

                }

            };


            GetPath(all, current);

            string[] split = path.Split(new char[] { '/' });

            Array.Reverse(split);


            current.Path = string.Join(" > ", split) + current.Name;

        }

Tuesday, July 14, 2020

Flatten tree view data

//Nested tree list, result list contains list of lists
 List<NavigationItem> allitems= result;

//Flatten the list
 List<NavigationItem> childitems = Flatten(allitems, a=>a.Items).ToList();


   public  List<NavigationItem> Flatten(List<NavigationItem> rootLevel, Func<NavigationItem, List<NavigationItem>> nextLevel)
        {
            List<NavigationItem> accumulation = new List<NavigationItem>();
            int i = 0;
            flattenLevel(accumulation, rootLevel, nextLevel, i);
            return accumulation;
        }

     
        private  void flattenLevel(List<NavigationItem> accumulation, List<NavigationItem> currentLevel, Func<NavigationItem, List<NavigationItem>> nextLevel, int level)
        {
            level++;

            foreach (NavigationItem item in currentLevel)
            {
             
                item.LevelNo = level;
                item.Level = "Level " + level.ToString();
                accumulation.Add(item);
                flattenLevel(accumulation, nextLevel(item), nextLevel, level);
            }
        }

Wednesday, July 1, 2020

Loading Treeview fast

Func<Employee, int> getXId = (x => x.EmployeeId);
Func<Employee, int?> getXParentId = (x => x.ManagerId);
Func<Employee, string> getXDisplayName = (x => x.Name);
LoadListItems(employees, getXId, getXParentId, getXDisplayName);

 private void LoadListItems<T>(IEnumerable<T> items, Func<T, int> getId, Func<T, int?> getParentId, Func<T, string> getDisplayName)
        {
            dicNodeList.Clear();

            var result = new List<NavigationItem>();

            foreach (var item in items)
            {
                var id = getId(item);
                var displayName = getDisplayName(item);
                var node = new NavigationItem { ID = id, Name = displayName, NodeInfo = item };
                node.Items = new List<NavigationItem>();
                dicNodeList.Add(getId(item), node);

            }

            foreach (var id in dicNodeList.Keys)
            {
                var currentNode = GetXNode(id);
                var nodeInfo = (T)currentNode.NodeInfo;
                var parentNodeID = getParentId(nodeInfo);

                if (parentNodeID.HasValue && dicNodeList.ContainsKey(parentNodeID.Value))
                {
                    var parent= GetXNode(parentNodeID.Value);
                    parent.Items.Add(currentNode);

                }
                else
                {
                    result.Add(currentNode);
                }
            }


            var z = result;

        }

public NavigationItem GetXNode(int id)
{
            return dicNodeList[id];
}

 Dictionary<int, NavigationItem> dicNodeList = new Dictionary<int, NavigationItem>();


 public class NavigationItem
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int? ParentID { get; set; }
        public List<NavigationItem> Items { get; set; }
        public object NodeInfo { get; set; }

    }