我已经做了一些搜索并找到了与我的场景相似的任何东西,并且会欣赏另一双眼睛给我一些建议。
我的任务是显示一个数据树,并创建存储过程来创建这个表结构:
它应该包含我需要的所有内容,包括级别,父级和文件夹ID [document_category_id]
计划是使用以下HTML结构显示数据
<div class="parent">Folder 1</div>
<div class="sublevel">
<div class="subfolder">Folder 1 Subfolder</div>
<div class="sublevel">
<div class="subfolder">Folder 1 Subfolder Level 2</div>
</div>
<div class="parent">Folder 2</div>
<div class="sublevel">
<div class="subfolder">Subfolder 1</div>
<div class="subfolder">Subfolder 2</div>
</div>
sublevel类具有创建缩进的边距,随后的子节点插入会增加标识。
这是C#代码。不可否认,我没有太多的递归经验,我有兴趣学习如何解决这个问题。我在这篇文章之后轻轻地对此进行了建模:non-normalized heirarchy / stackoverflow但你会发现存在一些差异
SqlConnection connection = null;
connection = new SqlConnection(ConfigurationData.databaseConnectionString);
SqlCommand command = new SqlCommand("myconnectionstring", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@myparameter", myparameter);
try {
connection.Open();
SqlDataReader reader = command.ExecuteReader();
int rowcount = 0;
int count = reader.FieldCount;
while (reader.Read()) {
_folderCategoryId = (int)reader["document_category_id"];
if (!DBNull.Value.Equals(reader["parent_category_id"])) {
_folderParentCategoryId = (int)reader["parent_category_id"];
}
if (DBNull.Value.Equals(reader["parent_category_id"])) {
_folderParentCategoryId = 0;
}
_level = (int)reader["level"];
_folderName = (string)reader["name"];
_folderDescription = (string)reader["description"];
rowcount = rowcount + 1;
//parent catgories get their own closed "parent" class div
if (_level == 0) {
_displayString.Append("<div class=\"parent\" >");
_displayString.Append(_folderName);
_displayString.Append("</div>");
}
for (int i = 1; i <= _level; i++) {
WriteNode(reader, _displayString);
}
_displayString.Append("</div>");
}
if (rowcount < 1) {
_displayString.Clear();
_displayString.Append("No Folders");
}
}
catch {
_displayString.Clear();
_displayString.Append("Error Retreiving Documents List");
}
finally {
if (connection != null) {
connection.Close();
}
}
}
// recursively write out each node
public static void WriteNode(SqlDataReader reader, StringBuilder _displayString) {
// find children rows...
int level = (int)reader["level"];
string name = (string)reader["name"];
if (level >= 1) {
_displayString.Append("</div>");
_displayString.Append("<div class=\"sublevel\">");
_displayString.Append(name);
}
}
所以,我知道这里的东西显然是错的。我一直在破坏它,它有点工作,但大多是不可靠的通常是未关闭的div,或根据孩子的数量太多关闭div标签。
很可能是对WriteNode方法的调用 - 我应该在这里查看以前的folderid值吗?长度足够好吗?我应该在这里关闭一个div吗?
任何指针都会非常感激。
答案 0 :(得分:0)
几天前想出这个。我使用的数据集不需要递归。
if (_level > prevLevel) { //must be a child
_displayString.Append("<div class=");
_displayString.Append("\"subfolder\" >");
formatClickString(_displayString, _folderCategoryId, _uploadVisibility, _folderName);
}
if (_level < prevLevel) { //start new level
for (int i = 0; i <= prevLevel; i++) {
_displayString.Append("</div>");
}
_displayString.Append("<div class=\"subfolder\" >");
formatClickString(_displayString, _folderCategoryId, _uploadVisibility, _folderName);
}
if (_level == prevLevel) { //is child and own line
_displayString.Append("</div>");
_displayString.Append("<div class=\"subfolder\" >");
formatClickString(_displayString, _folderCategoryId, _uploadVisibility, _folderName);
}
prevLevel = _level;
希望如果其他人需要建造一棵树,这将会派上用场。