Monday, January 5, 2015

Tree View Structure From DataBase


        [HttpGet]
        public List getValue(int id)
        {

            var dataList = nodeList().Where(x => x.parentID == 0).ToList();
            List nList = new List();
            foreach (var item in dataList)
            {
                node i = new node();
                i.name = item.name;
                FillChild(i, item.ID);
                nList.Add(i);
            }

            return nList;
        }

        public static List nodeList()
        {
            var nodeList = new List();
            var node1 = new nodeDB();
            node1.ID = 1;
            node1.name = "node1";
            node1.parentID = 0;
            nodeList.Add(node1);
            var node2 = new nodeDB();
            node2.ID = 2;
            node2.name = "node2";
            node2.parentID = 1;
            nodeList.Add(node2);
            var node3 = new nodeDB();
            node3.ID = 3;
            node3.name = "node3";
            node3.parentID = 2;
            nodeList.Add(node3);
            var node4 = new nodeDB();
            node4.ID = 4;
            node4.name = "node4";
            node4.parentID = 0;
            nodeList.Add(node4);
            var node5 = new nodeDB();
            node5.ID = 5;
            node5.name = "node5";
            node5.parentID = 4;
            nodeList.Add(node5);
            return nodeList;
        }
        public int FillChild(node parent, int ID)
        {
            var data = nodeList().Where(x => x.parentID == ID).ToList();
            if (data.Count > 0)
            {
                foreach (var item in data)
                {
                    node child = new node();
                    child.name = item.name;
                    int tempID = item.ID;
                    parent.nodes.Add(child);
                    FillChild(child, tempID);
                }
                return 0;
            }
            else
            {
                return 0;
            }
        }

No comments:

Post a Comment