Jan 19, 2015, 7:27 PM
I think this is not possible with current database structure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
namespace TreeGridMVC2._5.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
private NodeCollection data()
{
List<module> mod = module.GenarateModule();
List<submodule> submod = submodule.GenarateSubModule();
List<menu> mnu = menu.GenarateMenu();
NodeCollection n = new NodeCollection();
Node n_mod = null;
Node n_submod = null;
Node n_menu = null;
foreach (module obj_mod in mod)
{
var v_submod = submod.FindAll(item => item.moduleid == obj_mod.moduleid);
n_mod = new Node();
n_mod.Icon = Icon.Folder;
n_mod.Expanded = false;
n_mod.AttributesObject =
new moduleAttributes
{
moduleid = obj_mod.moduleid,
name = obj_mod.modulename,
p1 = false,
p2 = false,
p3 = false,
p4 = false
};
foreach (submodule obj_submodule in v_submod)
{
var v_menu = mnu.FindAll(item => item.submoduleid == obj_submodule.submoduleid);
n_submod = new Node();
n_submod.Icon = Icon.Folder;
n_submod.Expanded = false;
n_submod.AttributesObject =
new submoduleAttributes
{
submoduleid = obj_submodule.submoduleid,
name = obj_submodule.submodulename,
p1 = false,
p2 = false,
p3 = false,
p4 = false
};
foreach (menu obj_menu in v_menu)
{
n_menu = new Node();
n_menu.Icon = Icon.Folder;
// n_menu.Expanded = true;
n_menu.Leaf = true;
n_menu.AttributesObject =
new menuAttributes
{
menuid = obj_menu.menuid,
name = obj_menu.menuname,
p1 = obj_menu.p1,
p2 = obj_menu.p2,
p3 = obj_menu.p3,
p4 = obj_menu.p4
};
n_submod.Children.Add(n_menu);
}
BubbleToParent(n_submod);
n_mod.Children.Add(n_submod);
}
BubbleToParent(n_mod);
n.Add(n_mod);
}
return n;
}
private void BubbleToParent(Node parent)
{
bool allP1Checked = true;
bool allP2Checked = true;
bool allP3Checked = true;
bool allP4Checked = true;
foreach (Node child in parent.Children)
{
nodeAttributes attrb = (nodeAttributes)child.AttributesObject;
if (attrb.p1 != allP1Checked)
{
allP1Checked = false;
}
if (attrb.p2 != allP2Checked)
{
allP2Checked = false;
}
if (attrb.p3 != allP3Checked)
{
allP3Checked = false;
}
if (attrb.p4 != allP4Checked)
{
allP4Checked = false;
}
}
nodeAttributes subAttrb = (nodeAttributes)parent.AttributesObject;
subAttrb.p1 = allP1Checked;
subAttrb.p2 = allP2Checked;
subAttrb.p3 = allP3Checked;
subAttrb.p4 = allP4Checked;
}
public ActionResult genaratemenuajax(string node)
{
if (node == "Root")
{
return this.Content(data().ToJson());
}
return new HttpStatusCodeResult((int)System.Net.HttpStatusCode.BadRequest);
}
}
public class nodeAttributes
{
public string name { get; set; }
public bool p1 { get; set; }
public bool p2 { get; set; }
public bool p3 { get; set; }
public bool p4 { get; set; }
}
public class moduleAttributes : nodeAttributes
{
public int moduleid { get; set; }
}
public class submoduleAttributes : nodeAttributes
{
public int submoduleid { get; set; }
}
public class menuAttributes : nodeAttributes
{
public int menuid { get; set; }
}
public class module
{
public int moduleid { get; set; }
public string modulename { get; set; }
public static List<module> GenarateModule()
{
List<module> l = new List<module>();
for (int i = 1; i <= 2; i++)
{
var v = new module();
v.moduleid = i;
v.modulename = "Module " + i.ToString();
l.Add(v);
}
return l;
}
}
public class submodule
{
public int moduleid { get; set; }
public int submoduleid { get; set; }
public string submodulename { get; set; }
public static List<submodule> GenarateSubModule(int modid = 10)
{
List<submodule> l = new List<submodule>();
for (int j = 1; j <= modid; j++)
{
for (int i = 1; i <= 2; i++)
{
var v = new submodule();
v.moduleid = j;
v.submoduleid = i;
v.submodulename = "SubModule " + i.ToString();
l.Add(v);
}
}
return l;
}
}
public class menu
{
public int menuid { get; set; }
public int submoduleid { get; set; }
public string menuname { get; set; }
public bool p1 { get; set; }
public bool p2 { get; set; }
public bool p3 { get; set; }
public bool p4 { get; set; }
public static List<menu> GenarateMenu(int submodid = 10)
{
List<menu> l = new List<menu>();
for (int j = 1; j <= submodid; j++)
{
for (int i = 1; i <= 2; i++)
{
var v = new menu();
v.submoduleid = j;
v.menuid = i;
v.menuname = "Menu " + i.ToString();
if (i % 2 == 0) v.p1 = true; // just to check out everything works
v.p2 = false;
v.p3 = true;
v.p4 = true;
l.Add(v);
}
}
return l;
}
}
}