合并列表中的多个对象 [英] Combine multiple objects within a list

查看:66
本文介绍了合并列表中的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用顶点创建显示到自定义visualforce页面的各种sObject类型的记录的单个列表.我想知道是否有一种方法可以在单个列表中组合多个对象(案例,机会,帐户等).如果没有,如何使用visualforce代码将第二个对象附加到列表中的第一个对象?有最佳实践吗?

I'm looking to create a single list of records of various sObject types using apex to display to a custom visualforce page. I'm wondering if there is a way to combine multiple objects (case, opportunity, account, etc) within a single list. If not, how do you append the second object to the first in a list using visualforce code? Is there a best practice?

谢谢

因此,我可以使用更多的协助来完成此操作.我写了这个:

So I could use a little more assistance completing this. I have written this:

public class MyController {

    public List<ContactAndCase> ContactsAndCases { get; set; }

    public Opportunity TheOpp{
        get {
        TheOpp = [select Id FROM Opportunity];
        return TheOpp;
        }
        set;
     }
     public Case TheCase{
        get {
        TheCase = [select Id FROM Case];
        return TheCase;
        }
        set;
     }
} 

如何填写ContactsAndCases列表?

How do I fill the ContactsAndCases List?

推荐答案

VisualForce组件在单个列表上进行迭代.列表的类型,例如List<Lead>,指示VF组件可以呈现哪些数据.

A VisualForce component iterates over a single list. The type of the List, e.g. List<Lead>, dictates what data can be rendered by a VF component.

为了呈现不同类型的列表,您可以为每种类型使用单独的VF组件,例如:

In order to render Lists of different types, you can either have a separate VF component for each type, e.g.:

<apex:pageBlockTable value="{!contacts}" var="contact">
...
</apex:pageBlockTable>
<apex:pageBlockTable value="{!cases}" var="case">
...
</apex:pageBlockTable>

或者,您可以创建一个包含不同类型实例的新类型.这是一个控制器示例:

Or, you can create a new type which holds instances of the different types. Here's a controller example:

public class MyController {
    public List<ContactAndCase> ContactsAndCases { get; set; }

    public MyController() {
        ContactsAndCases = new List<ContactAndCase>();
        // populate ContactsAndCases list
    }

    public class ContactAndCase {
        public Contact TheContact { get; set; }
        public Case TheCase { get; set; }
    }
}

然后,您可以遍历List<ContactAndCase>:

<apex:pageBlockTable value="{!ContactsAndCases}" var="item">
    <apex:column value="{!item.TheContact.LastName}" />
    <apex:column value="{!item.TheCase.CaseNumber}" /> 
</apex:pageBlockTable>

这篇关于合并列表中的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆