Domanda

Can anyone see what is wrong with this loop? I am not a coldfusion developer but I am doing something for our absent developer. I am trying to get the loop to stop after 10 iterations but it is not happening. The CMS I am using is Mura. Thanks.

                    <cfset limit = 1>
                    <cfloop condition="iterator.hasNext()">
                        <cfif limit LTE 10>
                        <cfoutput>
                            <cfset item = iterator.next()>
                                <tr>
                                    <td>#item.getId()#</td>
                                    <td>#item.getTitle()#</td>
                                </tr>
                         </cfoutput>    
                         </cfif>
                        <cfset limit = limit + 1>
                    </cfloop>
È stato utile?

Soluzione

While Ben's answer will work, the best option is to tell the Mura iterator how many iterations to do before beginning the loop.

<cfset iterator.setNextN(10) />
<cfloop condition="iterator.hasNext()">
    <cfset item = iterator.next()>
        <cfoutput>
            <tr>
                <td>#item.getId()#</td>
                <td>#item.getTitle()#</td>
            </tr>
        </cfoutput>    
</cfloop>

Typically it defaults to 10, so somewhere in your settings or code it must be set to more.

Altri suggerimenti

I would just check for limit GTE 10 and use CFBREAK to terminate the loop early.

<cfset limit = 0>
<cfloop condition="iterator.hasNext()">
    <cfoutput>
        <cfset item = iterator.next()>
            <tr>
                <td>#item.getId()#</td>
                <td>#item.getTitle()#</td>
            </tr>
     </cfoutput>    
    <cfset limit++>
    <cfif limit GTE 10>
        <cfbreak>
    </cfif>
</cfloop>

There's also another option with <cfloop>:

<cfloop from="1" to="10" index="ii">
    <cfif iterator.hasNext()>
        <cfset item = iterator.next() />
        <cfoutput>
            <tr>
                <td>#item.getId()#</td>
                <td>#item.getTitle()#</td>
            </tr>
        </cfoutput>
    <cfelse>
        <cfbreak />
    </cfif>
</cfloop>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top