错误:下标值 9 无效。必须介于 0 到 8 之间

发布于 2024-12-23 05:46:11 字数 1873 浏览 1 评论 0原文

这是我的 vf 页面,我在 conListBadgeController 中获取 9 条记录,即 conList[0]conList[8]

我使用变量 firstsecond 作为 List 的下标。

我在每个 apex:repeat 中将变量的值增加 2,它从 0 到 2 工作正常。

但是当变量变为 9 时,它会显示如下错误:

下标值 9 无效。必须介于 0 和 8 之间。

如何防止变量自增?

如何在 if .. else 条件下中断代码的执行?

<apex:page standardController="Event__c" extensions="BadgeController" showHeader="false" sidebar="false" renderAs="pdf">

<html>
<head>
</head>
<center>

<apex:variable var="first" value="{!0}" id="id3"/>
<apex:variable var="second" value="{!1}" id="id4"/>

<apex:repeat value="{!conList}" >

<apex:variable var="first" value="{!first+ 2}" id="id1"/>
<apex:variable var="second" value="{!second+ 2}" id="id2"/>



<table><tr><td>

<table BORDER="2" RULES="NONE" FRAME="BOX">
<tr>
<td>
<table height="100px" width="300px" border="1" align="center" cellspacing="30">

<tr>
<td><center><apex:outputField value="{!conList[first].AccountContact__r.FirstName}"/></center></td>
</tr>

</table>
</td>

</tr>

</table></td>

<td></td>
<td>

<table BORDER="2" RULES="NONE" FRAME="BOX">
<tr>
<td>
<table height="100px" width="300px" border="1" align="center" cellspacing="30">


<tr>
<td><center><apex:outputField value="{!conList[second].AccountContact__r.FirstName}"/></center></td>
</tr>


</table>
</td>

</tr>

</table>
</td></tr></table> <br></br><br></br>

</apex:repeat>
</center>
</html>

</apex:page>

This is my vf page, I am getting 9 records in BadgeController in conList, i.e. conList[0] to conList[8].

I am using variables first and second as a subscript to List.

I am incrementing value of variable by 2 in each apex:repeat, it works fine from 0 to 2.

But when variable become 9 it is showing error like:

Subscript value 9 not valid. Must be between 0 and 8 .

How to prevent variable from incrementing?

how to break the execution of code in if .. else condition?

<apex:page standardController="Event__c" extensions="BadgeController" showHeader="false" sidebar="false" renderAs="pdf">

<html>
<head>
</head>
<center>

<apex:variable var="first" value="{!0}" id="id3"/>
<apex:variable var="second" value="{!1}" id="id4"/>

<apex:repeat value="{!conList}" >

<apex:variable var="first" value="{!first+ 2}" id="id1"/>
<apex:variable var="second" value="{!second+ 2}" id="id2"/>



<table><tr><td>

<table BORDER="2" RULES="NONE" FRAME="BOX">
<tr>
<td>
<table height="100px" width="300px" border="1" align="center" cellspacing="30">

<tr>
<td><center><apex:outputField value="{!conList[first].AccountContact__r.FirstName}"/></center></td>
</tr>

</table>
</td>

</tr>

</table></td>

<td></td>
<td>

<table BORDER="2" RULES="NONE" FRAME="BOX">
<tr>
<td>
<table height="100px" width="300px" border="1" align="center" cellspacing="30">


<tr>
<td><center><apex:outputField value="{!conList[second].AccountContact__r.FirstName}"/></center></td>
</tr>


</table>
</td>

</tr>

</table>
</td></tr></table> <br></br><br></br>

</apex:repeat>
</center>
</html>

</apex:page>

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

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

发布评论

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

评论(1

一梦等七年七年为一梦 2024-12-30 05:46:11

我不太确定你在这里问什么,但听起来你只是想在列表中显示交替值。这是 Apex 的工作(如果您真的不想编写任何 Apex 代码,则可能是 JavaScript,但如果您打算在 Force.com 上停留一段时间,您应该了解 Apex)。在 Apex 中,您可以对列表执行任何您喜欢的操作,并显示您需要显示的任何内容,而无需将演示文稿与业务逻辑紧密耦合。来自 Visualforce 开发人员指南:

注意:apex:variable 不支持在 an 内部重新分配
迭代组件,例如 apex:dataTable 或 apex:repeat。这
这样做的结果,例如,将 apex:variable 递增为
计数器,不受支持且未定义。

因此,这里有一个 Apex 示例,它创建两个可供 Visualforce 显示的交替元素列表:

public with sharing class Sample 
{
    public List<Contact> conListOne {get; private set;}
    public List<Contact> conListTwo {get; private set;}

    public Sample()
    {
        conListOne = new List<Contact>();
        conListTwo = new List<Contact>();

        Contact[] tmpConList = [SELECT Id, Name, Account.Name FROM Contact LIMIT 10];
        Integer i = 0;
        while(i < tmpConList.size())
        {
            conListOne.add(tmpConList[i]);
            if((i + 1) < tmpConList.size())
            {
                conListTwo.add(tmpConList[i+1]);
            }
            i += 2;
        }
    }
}

现在,您可以在 Visualforce 中显示两个不同的列表,这些列表具有原始 SOQL 中的偶数/奇数行号:

<apex:pageBlockTable value="{!conListOne}" var="item">
</apex:pageBlockTable>
<apex:pageBlockTable value="{!conListTwo}" var="item">
</apex:pageBlockTable>

或者只需修改 Apex 以生成包含任何内容的单个列表您希望的行顺序并显示它。

希望有帮助。

I'm not exactly sure what you're asking here, but it sounds like you simply want to display alternating values in a list. This is a job for Apex (or perhaps JavaScript if you really don't want to code any Apex, though you should get to know Apex if you plan to remain on Force.com for a while). In Apex you can do whatever you like with lists and display whatever you need to display without tightly coupling your presentation with your business logic. From the Visualforce Developer's Guide:

Note: apex:variable does not support reassignment inside of an
iteration component, such as apex:dataTable or apex:repeat. The
result of doing so, e.g., incrementing the apex:variable as a
counter, is unsupported and undefined.

So here's an Apex sample that creates two lists of alternating elements for Visualforce to display:

public with sharing class Sample 
{
    public List<Contact> conListOne {get; private set;}
    public List<Contact> conListTwo {get; private set;}

    public Sample()
    {
        conListOne = new List<Contact>();
        conListTwo = new List<Contact>();

        Contact[] tmpConList = [SELECT Id, Name, Account.Name FROM Contact LIMIT 10];
        Integer i = 0;
        while(i < tmpConList.size())
        {
            conListOne.add(tmpConList[i]);
            if((i + 1) < tmpConList.size())
            {
                conListTwo.add(tmpConList[i+1]);
            }
            i += 2;
        }
    }
}

Now you can display two different lists in Visualforce that have even/odd row numbers from your original SOQL:

<apex:pageBlockTable value="{!conListOne}" var="item">
</apex:pageBlockTable>
<apex:pageBlockTable value="{!conListTwo}" var="item">
</apex:pageBlockTable>

Or just modify the Apex to produce a single list with whatever row order you wish and display that.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文