构建动态层次结构

发布于 2024-11-05 16:59:29 字数 949 浏览 5 评论 0原文

我有一个查询返回以下格式的行:

ID      NAME                                                            PARENTID
1     Top level.                                                      0
7     Second level1.                                                  1
10   Third Level under Second Level1.                                7
22   Second Level2.                                                  1
23   Third Level1 under Second Level2.                               22
24      Third level2 under Second Level2.                               22

我想要做的是动态构建一个结构。上述数据需要按以下层次结构进行组织:

Top Level.
    Second Level1. 
                Third Level under Second Level1.
    Second Level2.
                Third Level under Second Level2.
                Third Level under Second Level2.

数据可以是任何内容,并且层次结构的深度没有限制。我正在寻找一种编程方式来将这些数据组织成一个结构,无论查询返回什么。我正在 ColdFusion 中编写此内容,因此如果您有 CF 示例那就太好了,但伪代码也可以。感谢您的帮助。

I have a query that returns rows in the following format:

ID      NAME                                                            PARENTID
1     Top level.                                                      0
7     Second level1.                                                  1
10   Third Level under Second Level1.                                7
22   Second Level2.                                                  1
23   Third Level1 under Second Level2.                               22
24      Third level2 under Second Level2.                               22

What I want to do is dynamically build a structure. The above data would need to be organized in the following hierarchy:

Top Level.
    Second Level1. 
                Third Level under Second Level1.
    Second Level2.
                Third Level under Second Level2.
                Third Level under Second Level2.

The data could be anything and there is no limit to the depth of the hierarchy. I'm looking for a programmatic way of organizing this data into a structure no matter what is returned from the query. I'm writing this in ColdFusion so if you have CF examples that would be nice but pseudocode would be fine. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2024-11-12 16:59:29

调用该函数并传入0作为parentID和q_query的查询结果。

<cffunction name="generateArray" returntype="Array">
    <cfargument name="parentID" type="Numeric" required="yes" default=0 >
    <cfargument name="q_query" type="query" required="yes">

    <cfquery name="getChildren" dbtype="query">
        SELECT * 
        FROM q_query
        WHERE parentid = <cfqueryparam value="#parentID#" cfsqltype="CF_SQL_INTEGER">
        ORDER BY sequence;
    </cfquery>

    <cfset treeArray = ArrayNew(1)>
    <cfloop query="getChildren">
            <cfquery name="checkForChildren" dbtype="query">
                SELECT * 
                FROM q_query
                WHERE parentid = <cfqueryparam value="#getChildren.id#" cfsqltype="CF_SQL_INTEGER">
            </cfquery>

            <cfset treeStruct = structNew()>
            <cfset treeStruct["id"] = getChildren.id>
            <cfset treeStruct["name"] = getChildren.name>
            <cfset treeStruct["children"] = ArrayNew(1)>
            <cfif checkForChildren.recordcount gt 0>
                <cfinvoke component="cfcs.thisCfc" method="GenerateArray" parentID="#Val(getChildren.ID)#" q_query="#q_query#" returnvariable="children">
                <cfset treeStruct["children"] = children>
            </cfif>
            <cfset temp = ArrayAppend(treeArray, treeStruct)>
    </cfloop>
    <cfreturn treeArray>
</cffunction>

Call this function and pass in 0 for the parentID and the query result for q_query.

<cffunction name="generateArray" returntype="Array">
    <cfargument name="parentID" type="Numeric" required="yes" default=0 >
    <cfargument name="q_query" type="query" required="yes">

    <cfquery name="getChildren" dbtype="query">
        SELECT * 
        FROM q_query
        WHERE parentid = <cfqueryparam value="#parentID#" cfsqltype="CF_SQL_INTEGER">
        ORDER BY sequence;
    </cfquery>

    <cfset treeArray = ArrayNew(1)>
    <cfloop query="getChildren">
            <cfquery name="checkForChildren" dbtype="query">
                SELECT * 
                FROM q_query
                WHERE parentid = <cfqueryparam value="#getChildren.id#" cfsqltype="CF_SQL_INTEGER">
            </cfquery>

            <cfset treeStruct = structNew()>
            <cfset treeStruct["id"] = getChildren.id>
            <cfset treeStruct["name"] = getChildren.name>
            <cfset treeStruct["children"] = ArrayNew(1)>
            <cfif checkForChildren.recordcount gt 0>
                <cfinvoke component="cfcs.thisCfc" method="GenerateArray" parentID="#Val(getChildren.ID)#" q_query="#q_query#" returnvariable="children">
                <cfset treeStruct["children"] = children>
            </cfif>
            <cfset temp = ArrayAppend(treeArray, treeStruct)>
    </cfloop>
    <cfreturn treeArray>
</cffunction>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文