错误代码警告:对嵌套菜单的数组进行分组
哇。我被要求为公司的树形菜单添加第三层。以下是我发现大约 6 个月前创建的内容。虽然它有效,但我不可能向它添加另一个 cfloop hack。我所有的感觉都告诉我这只是糟糕的代码并重做。
我无法更改数据库,也无法通过查询以外的任何其他方式返回结果。现在你知道它必须是这样的,我在这里有什么选择???
数据库中的每条记录都没有关系,下面的代码仅查看小部件的名称,并根据以下内容确定它是否是父项:
widgetName 的示例
1= 合规性:
2= 合规性:文档库:
3=合规性:文档库:
上述所有公司都表示小部件 2 应该向下 2 级。看看代码,你会发现所做的事情非常糟糕......
<cfoutput>
<ul id="root" class="menu">
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 1>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
<cfset wordToFind = local.widgets.getwidgetName()>
<ul id='#local.widgets.getwidgetID()#'>
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 2 AND #find(wordToFind,local.widgets.getwidgetName())#>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
</li>
</cfif>
</cfloop>
</ul>
</cfif>
</cfloop>
</ul>
</cfoutput>
Wow. I have been asked to add a 3rd level to a tree menu for a company. Below is what I found had created about 6 months ago. And while it works, there is no way I am going to add another cfloop hack to it. All my feelings tell me this just bad code and to redo.
I cannot change the databased nor have the results returned in anything else other than a query. now you know the way it has to be, what are my options here???
Each record in the DB has NO relationship, the code below just looks at the name of the widget and works out if it is a parent or not based on the :
examples of widgetName
1= Compliance:
2= Compliance: Document Library:
3= Compliance: Document Library: All Firms
so above says that widget 2 should be 2 levels down. look at the code you will see in a very bad way what has been done....
<cfoutput>
<ul id="root" class="menu">
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 1>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
<cfset wordToFind = local.widgets.getwidgetName()>
<ul id='#local.widgets.getwidgetID()#'>
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 2 AND #find(wordToFind,local.widgets.getwidgetName())#>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
</li>
</cfif>
</cfloop>
</ul>
</cfif>
</cfloop>
</ul>
</cfoutput>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用递归函数来执行此操作,这将为您提供无限的未来兼容性。我不确定您正在使用的数据是什么样的,但看起来应该是可能的。
I would use a recursive function to do this, that would give you unlimited future compatibility. I'm not sure what the data you are working with looks like, but it seems as though it should be possible.