javascript写的带有checkbox的菜单权限树,怎样在C#中从数据库中查询出数据填充节点
权限树节点的定义
data:[{
id:"node1", //node id
text:"node 1", //node text for display.
value:"1", //node value
showcheck:false, //whether to show checkbox
checkstate:0, //Checkbox checking state. 0 for unchecked, 1 for partial checked, 2 for checked.
hasChildren:true, //If hasChildren and complete set to true, and ChildNodes is empty, tree will request server to get sub node.
isexpand:false, //Expand or collapse.
complete:false, //See hasChildren.
ChildNodes:[] // child nodes
}]
function createNode(id,text) {
var node = {
"id": id,
"text": text,
"value": id,
"showcheck": true,
complete: true,
"isexpand": true,
"checkstate": 0,
"hasChildren": true
};
return node;
}
function createLeafNode(id,text) {
var leaf = {
"id": id,
"text": text,
"value": id,
"showcheck": true,
complete: true,
"isexpand": true,
"checkstate": 0,
"hasChildren": false
};
return leaf;
}
ParmenuId CHAR(20),NOT NULL父菜单ID
我想的是递归调用,生成树,遇到的问题是没有办法建立子节点和父节点之间的联系
有解决的办法吗?或者别的思路?
[解决办法]
<script language="JavaScript">
var objdbConn = new ActiveXObject("ADODB.Connection");
var strdsn = "Driver={SQL Server};SERVER=localhost;UID=sa;PWD=;DATABASE=yuanWang";
objdbConn.Open(strdsn);
var objrs = objdbConn.Execute("Select * from [User]");
var fdCount = objrs.Fields.Count - 1;
if (!objrs.EOF){
document.write("<table border=1><tr>");
for (var i=0; i <= fdCount; i++)
document.write("<td><b>" + objrs.Fields(i).Name + "</b></td>");
document.write("</tr>");
while (!objrs.EOF){
document.write("<tr>");
for (i=0; i <= fdCount; i++)
document.write("<td valign='top'>" + objrs.Fields(i).Value + "</td>");
document.write("</tr>");
objrs.moveNext(); // 移到下一个记录点
}
document.write("</table>");
}
else
document.write("数据库内没有记录!<br>");
objrs.Close(); // 关闭记录集和
objdbConn.Close(); // 关闭数据库链接
[解决办法]
我感觉你应该是用这个控件吧
http://www.cnblogs.com/xuanye/archive/2009/10/26/1590250.html
用ajax来取数据
针对这个控件,我在服务器端写了个它的节点类,你可以用下看
public class JsonTreeNode
{
private int m_intCheckState = 0;
public string ID { get; set; }
public string Text { get; set; }
public string Value { get; set; }
public bool ShowCheck { get; set; }
public int CheckState
{
get
{
return m_intCheckState;
}
set
{
m_intCheckState = value;
if(value != 0)
{
UpdateCheckState();
}
}
}
public bool IsExpand { get; set; }
private JsonTreeNode m_Parent = null;
public void SetParent(JsonTreeNode paramParent)
{
m_Parent = paramParent;
}
private void SetCheckState()
{
if(m_Parent.CheckState == 0)
{
m_Parent.CheckState = 2;
}
}
public void UpdateCheckState()
{
if(m_Parent != null)
{
SetCheckState();
}
}
public List<JsonTreeNode> Children = new List<JsonTreeNode>();
//public JsonTreeNode()
//{
// ID = string.Empty;
// Text = string.Empty;
// Value = string.Empty;
// ShowCheck = true;
// IsExpand = false;
//}
public JsonTreeNode(JsonTreeNode paramParent)
{
ID = string.Empty;
Text = string.Empty;
Value = string.Empty;
ShowCheck = true;
IsExpand = false;
m_Parent = paramParent;
}
public override string ToString()
{
if(ID.Length == 0)
{
ID = Value;
}
if(CheckState == 2)
{
IsExpand = true;
}
StringBuilder sb = new StringBuilder();
sb.Append("{"id":"");
sb.Append(ID);
sb.Append("","text":"");
sb.Append(Text);
sb.Append("","value":"");
sb.Append(Value);
sb.Append("","showcheck":");
sb.Append(ShowCheck ? "true" : "false");
sb.Append(","complete":true,"checkstate":");
sb.Append(CheckState);
if(Children.Count > 0)
{
sb.Append(","isexpand":");
sb.Append(IsExpand ? "true" : "false");
sb.Append(","hasChildren":true,"ChildNodes":[");
for(int i = 0; i <= Children.Count - 1; i++)
{
if(i > 0)
{
sb.Append(",");
}
sb.Append(Children[i]);
}
sb.Append("]}");
}
else
{
sb.Append(","hasChildren":false,"ChildNodes":null}");
}
return sb.ToString();
}
public static string GetJsonData(List<JsonTreeNode> paramLst)
{
string strResult = string.Empty;
for(int i = 0; i <= paramLst.Count - 1; i++)
{
if(i > 0)
{
strResult += ",";
}
strResult += paramLst[i];
}
return strResult;
}
}
private List<JsonTreeNode> m_lstJsonTreeNode = new List<JsonTreeNode>();
#region private List<JsonTreeNode> GetChildList(JsonTreeNode paramParentNode)
/// <summary>
/// 取得List<JsonTreeNode>
/// </summary>
/// <param name="paramParentNode"></param>
/// <returns></returns>
private List<JsonTreeNode> GetChildList(JsonTreeNode paramParentNode)
{
if(paramParentNode == null)
{
return m_lstJsonTreeNode;
}
else
{
return paramParentNode.Children;
}
}
#endregion
#region private void ProcessDataTable(JsonTreeNode paramParentNode, DataTable paramDtAll, string paramName, int paramLen)/// <summary>
/// /// </summary>
/// <param name="paramParentNode"></param>
/// <param name="paramDtAll"></param>
/// <param name="paramName"></param>
/// <param name="paramLen"></param>
private void ProcessDataTable(JsonTreeNode paramParentNode, DataTable paramDtAll, string paramName, int paramLen)
{
if(paramLen == 0)
{
JsonTreeNode myJsonTreeNode = new JsonTreeNode(paramParentNode) { Text = "档案", Value = "0", CheckState = GetCheckState("0") };
GetChildList(paramParentNode).Add(myJsonTreeNode);
ProcessDataTable(myJsonTreeNode, paramDtAll, string.Empty, paramLen + 2);
return;
}
DataRow[] drs = paramDtAll.Select(m_strCode + " LIKE '" + paramName + "%' AND LEN(" + m_strCode + ") = " + paramLen);
foreach(DataRow drItem in drs)
{
JsonTreeNode myJsonTreeNode = new JsonTreeNode(paramParentNode) { Text = paramDtAll.Select(m_strCode + " = '" + drItem[m_strCode].ToString() + "'")[0][m_strName].ToString(), Value = drItem[m_strID].ToString(), ShowCheck = true, CheckState = GetCheckState(drItem[m_strID].ToString()) };
GetChildList(paramParentNode).Add(myJsonTreeNode);
ProcessDataTable(myJsonTreeNode, paramDtAll, drItem[m_strCode].ToString(), paramLen + 2);
}
}
#endregion