组合列表中的多个对象 [英] Combine multiple objects within a list
问题描述
我希望使用 apex 来创建各种 sObject 类型的记录的单个列表,以显示到自定义的 Visualforce 页面.我想知道是否有办法在一个列表中组合多个对象(案例、机会、帐户等).如果没有,您如何使用 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
,规定了 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
:
<apex:pageBlockTable value="{!ContactsAndCases}" var="item">
<apex:column value="{!item.TheContact.LastName}" />
<apex:column value="{!item.TheCase.CaseNumber}" />
</apex:pageBlockTable>
这篇关于组合列表中的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!