2016-12-27 38 views
1

我正在使用cfloop中的很多项目。我想削减这一点,并添加分页。无论如何,我的cfloop会将阵列限制在前10位?Coldfusion将cfloop限制为10

<cfloop array="#qryItems#" index="index"> 

我试图把它变成没有运气和其他一些东西的条件循环。我有一段时间没有碰过coldfusion,并且有点生锈。谷歌没有帮助哈哈

我已经试过

<cfloop from="1" to="10" array="#qryItems#" index="index"> 

,并试图MAX_ROWS

<cfloop maxrows="10" array="#qryItems#" index="index"> 
每次我得到错误信息

“属性验证错误标签CFLOOP

。 “

+0

您可以用'cfbreak' –

+0

@Lashane它在CFOUTPUT这样一个非常大的数据块的代码,我试图避免对其进行编辑,但我会尝试任何事情。 – zazvorniki

+1

而不是将循环限制为10次迭代,为什么不编辑定义数组的东西只返回10个元素?如果它是由查询定义的,则通过在数据库中执行分页并仅返回请求的页面记录,可以获得更好的性能。 –

回答

2
<cfloop from="1" to="10" index="index"> 
    <!--- Then do your business with array elements qryItems[index], that is, with qryItems[1], qryItems[2],..., qryItems[10] ---> 
</cfloop> 
+0

我试过这个。我刚收到错误“ 标签CFLOOP的属性验证错误。” – zazvorniki

+0

@zazvorniki您一定有其他错误,因为这应该工作。如果您发布您尝试的代码,我们可能会指出问题所在。只需[编辑您的原始问题](http://stackoverflow.com/posts/41350595/edit)并在其中添加代码。 –

+0

@ Miguel-F我没有改变代码中的其他任何东西,代码已经稳定了一年多了。我张贴在我试过的代码上面。 – zazvorniki

1

cfloop的属性没有组合可以实现您的期望。正如BKBK所建议的那样,您需要使用from/to循环来输出选定的一组记录。如果我正确理解你的要求,我会用新的索引变量更新cfloop,然后通过引用数组元素来设置旧变量。

下面两个cfloops输出相同的数据,第二个只显示分页范围内的记录。

<cfset qryItems = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d'] /> 
<cfoutput> 
    <!--- Current loop: Outputs all records ---> 
    <cfloop array="#qryItems#" index="index"> 
     #index# 
    </cfloop> 
    <cfset paginationStart = 1 /> 
    <cfset paginationEnd = 10 /> 
    <!--- Only the range of of records requested ---> 
    <cfloop from="#paginationStart#" to="#paginationEnd#" index="indexNumber"> 
     <cfset index = qryItems[indexNumber] /> 
     <!--- code remain the same ---> 
     #index# 
    </cfloop> 
</cfoutput> 
+0

是的,尽管我认为他们实际上希望'from/to'是动态的,所以可以使用分页代码显示元素X到Y,而不是始终显示前10个元素。 [实施例](http://trycf.com/gist/1833166b313d7602c2fbf991a02fd431/acf2016?theme=monokai)。 – Leigh

+1

Thanks @Leigh我已经走了,并更新了我的示例,以便像在您的示例中那样包含分页变量。 – Twillen