javascript树问题
我在网上找了几个树,都是用add(new node(“”))诸如此类方法追加节点,我现在用SQL查出来这样的记录:一列是级别(level),一列是本身id,一列是名字(name),一列是父(parid);
我现在想把这个结果集动态的加到一个根里,我该怎么加?
例:var tv = new treeview("treeview");
var b = new node("name1","tag");
var c = new node("name2");
var d = new node("name3");
tv.add(b);
b.add(c);
b.add("name8");
c.add(d);
帮帮忙吧,脑袋都想大了,实在不会了!都说这东西递归能行,我还递不明白!!
------解决方案--------------------首先,将这些数据order by level desc
然后,用个foreach 语句,逐条构造成node,因为已经按照level排了序,所以此时的node根据其parid就可以找到对应的父
node getNode(var parid,var level) //写个根据层次和id找节点的方法
{ ... }
找到其父节点,调用父节点的add即可
------解决方案--------------------http://www.cnblogs.com/doll-net/archive/2007/08/05/843991.html
希望对你有所帮助
------解决方案--------------------给段代码吧
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
GetDataTable1();
InitTreeRootNode(TreeView1);
if (TreeView1.Nodes.Count == 1)
{
TreeView1.ExpandDepth = 1;
}
}
}
public void GetDataTable1()
{
Database dbase = new Database();
string username = Request.Cookies["Username"].Value.ToString();
SqlParameter[] prams ={ dbase.MakeInParam("@Username", SqlDbType.VarChar, 20, username) };
try
{
dataTbl1 = dbase.RunProcAdd("sp_GetShowClass", prams);
}
catch (Exception ex)
{
KMS.Components.Error.Log(ex.Message);
Response.Write(ex.Message);
}
dataTbl1.TableName = "TreeView";
}
private void GetDataTable2(int nodeID)
{
Database dbase = new Database();
SqlParameter[] prams = { dbase.MakeInParam("@Class_id", SqlDbType.Int, 20, nodeID) };
try
{
dataTbl2 = dbase.RunProcAdd("sp_GetAllChildClass", prams);
}
catch (Exception ex)
{
Response.Write(ex.Message);
throw new Exception(ex.Message);
}
dataTbl2.TableName = "TreeView";
}
/// <summary>
/// 初始化TreeView 的 RootNode
/// </summary>
private void InitTreeRootNode(System.Web.UI.WebControls.TreeView TNC)
{
for (int i = 0; i < dataTbl1.Rows.Count; i++)
{
System.Web.UI.WebControls.TreeNode tn = new System.Web.UI.WebControls.TreeNode();
tn.Value = dataTbl1.Rows[i]["ClassID"].ToString();
tn.Text = "<span style='font-size:10pt' onmouseover=javascript:this.style.color='white';this.style.backgroundColor='#8592B5'; onmouseout=javascript:this.style.color='#494949';this.style.backgroundColor='white'; title='" + dataTbl1.Rows[i]["className"].ToString() + "'>" + dataTbl1.Rows[i]["className"].ToString() + "</span>";
tn.ImageUrl = GetIcon(dataTbl1.Rows[i]["classType"].ToString());
tn.NavigateUrl = "../SubModule/UnitiveDocument/switch.aspx?Action=1&ClassID=" + dataTbl1.Rows[i]["classID"].ToString();
tn.Target = "MainFrame";
TNC.ExpandDepth =