简体   繁体   English

在一个页面中以表格形式显示来自Salesforce VF页面中多个对象的数据

[英]Display data from multiple objects in a Salesforce VF page in one pageblocktable

I have a requirement to display records from 2 objects (Account & Contact) in a single pageblocktable in a Salesforce VF page, Account records followed by Contact records. 我需要在Salesforce VF页面的单个pageblocktable中显示2个对象(客户和联系人)的记录,然后是客户记录和联系人记录。 I know that we can achieve this using a wrapper class, but all the examples I came across talked about displaying checkbox or displaying columns vertically(not horizontally) from different objects in a pageblocktable. 我知道我们可以使用包装器类来实现此目的,但是我遇到的所有示例都谈到了显示复选框或从pageblocktable中的不同对象垂直(而不是水平)显示列。

Would appreciate any pointers/code samples. 将不胜感激任何指针/代码示例。 Thanks!! 谢谢!!

        Object    Name                  Phone           Email

Record 1 - Account - Account Name - Account Phone -Account Email 记录1-帐户-帐户名称-帐户电话-帐户电子邮件

Record 2 - Contact - Contact First Name - Contact Phone -Contact Email 记录2-联系人-联系人名字-联系人电话-联系人电子邮件

I found a dirty way of doing this. 我发现这样做的肮脏方式。 This may not be the best answer, but you can achieve the required functionality as follows. 这可能不是最佳答案,但是您可以按以下方式实现所需的功能。 Create a two dimensional array and store different object's data as strings. 创建一个二维数组,并将不同对象的数据存储为字符串。 Then iterate over the array in visualforce page to display the data. 然后遍历visualforce页面中的数组以显示数据。

public class MixedObjectVFController {

public static List<List<String>> getObjectList(){
    List<List<String>> strList = new List<List<String>>();
    List<Account> acc = [select name,phone from account limit 2];

    for(account a : acc){
        List<String> tempList = new List<String>();
        tempList.add('account');
        tempList.add(a.name);
        tempList.add(a.phone);
        strList.add(tempList);
    }
    List<contact> cList = [select name,phone from contact limit 2];
    for(contact a : cList){
        List<String> tempList = new List<String>();
        tempList.add('contact');
        tempList.add(a.name);
        tempList.add(a.phone);
        strList.add(tempList);
    }
    return strList;
} }

and Visualforce page 和Visualforce页面

<apex:page controller="MixedObjectVFController" >
<apex:pageBlock>
    <apex:pageBlockSection>
        <apex:pageBlockTable value="{!objectList}" var="item">
            <apex:column headerValue="Object" value="{!item[0]}" />
            <apex:column headerValue="Name" value="{!item[1]}" />
        </apex:pageBlockTable> 
    </apex:pageBlockSection>
</apex:pageBlock>

Make sure to check for nulls while using the indices. 确保使用索引时检查是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM