Visualforce 中的 sObject?

发布于 2024-12-03 04:07:43 字数 166 浏览 5 评论 0原文

我有一个顶点控制器,它构建一个要在数据表中显示的列表。该列表组合了不同的对象,因此我创建的变量是一个列表,

假设该列表中的所有对象都有一个“external__c”字段。我如何告诉 Visualforce 渲染这个字段?使用 {!obj.external__c} 将不起作用,因为它是 sObject。

I have an apex controller which builds up a list to be displayed in a datatable. The list combines different object, so the variable I create is a List

Say all the objects in this list have an "external__c" field. How do I tell the visualforce to render this field? Using {!obj.external__c} will not work since it is an sObject.

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

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

发布评论

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

评论(2

太傻旳人生 2024-12-10 04:07:43

如果您有 SObject 列表,则可以使用 obj.get('external__c') 获取公共字段,但通常必须将结果转换为能够使用它的类型。

您可以在代码中创建一个自定义类,您可以在其中填充各种对象:

// inside the controller do this:
public class COutputObject
{
    private SObject sObj = null;
    public  string  strField get {return (string)sObj.get('external__c'); }

    public COutputObject(SObject s)
    {
        sObj = s;
    }
}

// -- snip --

// then have a list of these which you'll loop over in the page
public list<COutputObject> liObjects = new list<COutputObject>();

// fill this with data
for(CustomObj__c sCustom : [select Id, external__c from CustomObj__c limit 200])
{
    liObjects.add(new COutputObject(sCustom));
    // etc.

for(CustomObj2__c sCustom : [select Id, external__c from CustomObj2__c limit 200])
{
    liObjects.add(new COutputObject(sCustom));
    // etc.

不能 100% 确定我的 getter 语法是否正确,但它很接近;)希望这将帮助您实现您所追求的目标!

If you've got a list of SObjects you can get a common field using obj.get('external__c') though you generally have to cast the result to a type to be able to use it.

You can create a custom class in code which you can fill with various objects:

// inside the controller do this:
public class COutputObject
{
    private SObject sObj = null;
    public  string  strField get {return (string)sObj.get('external__c'); }

    public COutputObject(SObject s)
    {
        sObj = s;
    }
}

// -- snip --

// then have a list of these which you'll loop over in the page
public list<COutputObject> liObjects = new list<COutputObject>();

// fill this with data
for(CustomObj__c sCustom : [select Id, external__c from CustomObj__c limit 200])
{
    liObjects.add(new COutputObject(sCustom));
    // etc.

for(CustomObj2__c sCustom : [select Id, external__c from CustomObj2__c limit 200])
{
    liObjects.add(new COutputObject(sCustom));
    // etc.

Not 100% sure if my syntax on the getter is correct, but it's close ;) Hopefully this will help you achieve what you're after!

柠栀 2024-12-10 04:07:43

假设列表属性在控制器中像这样声明:Public List; ColdOnes { 得到;放; }。在 Visualforce 中,您可以通过控制器中的属性名称引用啤酒...{!ColdOnes}。以下内容大部分取自 Visualforce 指南,但我对其进行了调整以适应我们的 thist 淬灭主题:)

<apex:dataTable value="{!ColdOnes}" var="co" id="theTable" rowClasses="odd,even" styleClass="tableClass">

    <apex:facet name="caption">table caption</apex:facet>

    <apex:facet name="header">table header</apex:facet>

    <apex:facet name="footer">table footer</apex:facet>

    <apex:column>

            <apex:facet name="header">Beer Name</apex:facet>

        <apex:facet name="footer">column footer</apex:facet>

        <apex:outputText value="{!co.name}"/>

    </apex:column>
    <apex:column>

            <apex:facet name="header">Alcohol Volume</apex:facet>

        <apex:facet name="footer">column footer</apex:facet>

        <apex:outputText value="{!co.alcohol_volume__c}"/>

    </apex:column>
</apex:dataTable>

请注意,如果您在代码中使用查询值设置 ColdOnes,则需要选择您想要的字段输出到您的 Visualforce 中。所以:

ColdOnes=[select name, alcohol_volume__c from Beer__c where blahblahblah];

好吧,我要去喝一品脱。希望有帮助!

Let's say the list property is declared like so in your controller: Public List<Beer__c> ColdOnes { get; set; }. Well in Visualforce, you reference the beers by their property name in the controller... {!ColdOnes}. The following is mostly taken from the Visualforce guide, but I have adapted it to suit our thist-quenching subject matter :)

<apex:dataTable value="{!ColdOnes}" var="co" id="theTable" rowClasses="odd,even" styleClass="tableClass">

    <apex:facet name="caption">table caption</apex:facet>

    <apex:facet name="header">table header</apex:facet>

    <apex:facet name="footer">table footer</apex:facet>

    <apex:column>

            <apex:facet name="header">Beer Name</apex:facet>

        <apex:facet name="footer">column footer</apex:facet>

        <apex:outputText value="{!co.name}"/>

    </apex:column>
    <apex:column>

            <apex:facet name="header">Alcohol Volume</apex:facet>

        <apex:facet name="footer">column footer</apex:facet>

        <apex:outputText value="{!co.alcohol_volume__c}"/>

    </apex:column>
</apex:dataTable>

Just be aware that if you are setting ColdOnes with a queried value in your code, you need to select the fields you intend to output in your Visualforce. So:

ColdOnes=[select name, alcohol_volume__c from Beer__c where blahblahblah];

Well, I'm off for a pint. Hope that helps!

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