cfdirectory loop限制结果

时间:2016-06-13 02:58:48

标签: coldfusion cfloop cfdirectory

我'我发现很难理解这一点。如何将结果限制为50。假设在目录中我有1000个文件,如何限制它以便只循环50个文件。

<cfdirectory action="list" directory="#ExpandPath('/downloaded/')#" name="listRoot" filter="*.xml" recurse="false" sort="datelastmodified asc">
<cfoutput>
   <cfloop query="listRoot" from="1" to="50" index="i">
           ....
   </cfloop>
</cfoutput>

当我运行上面的代码时,我收到以下错误消息

  

标签CFLOOP的属性验证错误。

2 个答案:

答案 0 :(得分:6)

如果您查看完整的错误消息,则其中包含答案(强调我的):

  

它有一个无效的属性组合:from,index,query,to。 可能的组合

     
      
  • 必需属性:'查询'。可选属性:'endrow,startrow'
  •   
  • ...
  •   
  • 必需属性:'from,index,'。可选属性:'step'
  •   

代码试图混合两种不同类型的循环:查询循环和从/循环。这不是一个有效的组合。您可以使用query循环 from/to循环,但不能同时使用。{/ p>

话虽如此,由于目标是显示输出,因此实际上不需要cfloop。只需将cfoutput与“startRow”和“maxRows”属性一起使用:

   <cfoutput query="listRoot" startRow="1" maxRows="50">
       #name#<br>
   </cfoutput>

如其他答案中所述,最新版本的CF也支持for ...in loops

<cfscript>
   for (row in listRoot) {
      writeOutput("<br>Debug: name value = "& row.name );
   }
</cfscript>

答案 1 :(得分:2)

您可以使用以下命令访问查询中的特定行:

Refused to display 'https://accounts.google.com/o/oauth2/auth?client_id=<KEY>' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. I understand that this error is because the Oauth2 doesn't expect the origin. But the Origin is not http, if file know... (file:///android_asset/www/index.html). The GoogleAPI Console doesn't accept 'file' protocol... just 'http'.

要执行query[columnName][rowIndex]而不是from to loop,请转到:

each loop