/// <summary>
/// 生成树状 ul-li
/// </summary>
/// <param name="parentid">parentid</param>
/// <returns>返回树状的html节点</returns>
public string CreateChildTree(string parentid)
{
string where = String.Format(" and parentid={0} and status=1 order by displayorder asc ", parentid);
List<Models.dnt_forums> list = DAL.dnt_forums.GetSingleton().Query(where);
StringBuilder childStr = new StringBuilder();
if (list != null && list.Count > 0)
{
childStr.Append("<ul>");
foreach (var item in list)
{
if (item.parentid.ToString() == parentid)
{
if (IsParent(item.fid.ToString()))
{
childStr.AppendFormat("<li><span id=\"{0}\" class=\"folder\">{1}</span>", item.fid.ToString(), item.name.ToString());
}
else
{
childStr.AppendFormat("<li><span id=\"{0}\" class=\"file\">{1}</span>", item.fid.ToString(), item.name.ToString());
}
if (IsParent(item.fid.ToString()))
{
childStr.Append(CreateChildTree(item.fid.ToString()));//执行递归
}
childStr.Append("</li>");
}
}
childStr.Append("</ul>");
}
return childStr.ToString();
}
#endregion
/// <summary>
/// 根据ID判断是不是父ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private bool IsParent(String id)
{
bool b = false;
string where = " and parentid=" + id;
var list = DAL.dnt_forums.GetSingleton().Query(where);
if (list != null && list.Count > 0)
{
b = true;
}
return b;
}
/// <summary>
/// 生成下拉列表的数据项
/// </summary>
/// <param name="parentid"></param>
/// <returns></returns>
public String TreeSelectOption(string parentid)
{
StringBuilder childStr = new StringBuilder();
string where = String.Format(" and parentid={0} and status=1 order by displayorder asc ", parentid);
List<Models.dnt_forums> list = DAL.dnt_forums.GetSingleton().Query(where);
if (list != null && list.Count > 0)
{
foreach (var item in list)
{
if (item.parentid.ToString() == parentid)
{
if (IsParent(item.fid.ToString()))
{
childStr.Append("<option value=\"" + item.fid + "\">"+ GetNull(item.fid.ToString())+ "|- " + item.name + "</value>");
}
else
{
childStr.Append("<option value=\"" + item.fid + "\">"+GetNull(item.fid.ToString()) + "└ " + item.name + "</value>");
}
if (IsParent(item.fid.ToString()))
{
childStr.Append(TreeSelectOption(item.fid.ToString()));//执行递归
}
}
}
}
return childStr.ToString();
}
/// <summary>
/// 根据id获取父ID 及其上一级父ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private string GetParentIdByChild(string id)
{
string result = "";
Models.dnt_forums model = DAL.dnt_forums.GetSingleton().QueryModel(id);
if (model != null)
{
if (model.parentid>0)
{
result = result + model.parentid + ",";
string str = GetParentIdByChild(model.parentid.ToString());
if (!string.IsNullOrWhiteSpace(str))
{
result = result + str + ",";
}
}
}
return result.Trim(',');
}
/// <summary>
/// 获取 空格数
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private string GetNull(string id)
{
string result = "";
int count = 0;
string parentIds = GetParentIdByChild(id);
if (!string.IsNullOrWhiteSpace(parentIds))
{
count = parentIds.Split(',').Length;
for (int i = 0; i < count; i++)
{
result = result + " ";
}
}
return result;
}