如何在 Visualforce 页面上显示聚合 SOQL 查询的结果? [英] How do I display the results of an aggregate SOQL query on a Visualforce page?
问题描述
我对 Visualforce 很陌生.
I'm very new to Visualforce.
我在这里查看此页面:http://force.siddheshkabe.co.in/2010/11/displaying-aggregate-result-on.html
因此,当我将此代码添加到 VisualForce 页面时:
So when I added this code onto a VisualForce page:
AggregateResult[] groupedResults = [SELECT Name, Days__c FROM Contact WHERE Days__c != ];
for (AggregateResult ar : groupedResults) {
System.debug('Name: ' + ar.get('Name') + '\nDays Taken : ' + ar.get('Days__c') + '\n');
但它所做的只是打印代码而不是执行它.我该怎么办?感谢您的指导.
But all it does is print the code instead of executing it. What should I be doing? Thanks for any guidance.
推荐答案
Apex 代码进入 自定义控制器或控制器扩展.VisualForce 页面是独立于控制器的文件.您引用的页面未显示 VF 页面.另外,我认为您不能将 VF 组件绑定到 AggregateResult
,因此您需要一个包装类.
The Apex code goes into a custom controller or controller extension. The VisualForce page is a separate file from the controller. The page you referenced doesn't show the VF page. Also, I don't think you can bind VF components to AggregateResult
, so you'll need a wrapper class.
这是一些工作代码.
控制器:
public with sharing class TestController {
public Summary[] Summaries { get; set; }
public TestController() {
AggregateResult[] results = [
SELECT Name, Count(Id) Quantity FROM Opportunity GROUP BY Name
];
Summaries = new List<Summary>();
for (AggregateResult ar : results) {
Summaries.add(new Summary(ar));
}
}
// wrapper class to hold aggregate data
public class Summary {
public Integer Quantity { get; private set; }
public String Name { get; private set; }
public Summary(AggregateResult ar) {
Quantity = (Integer) ar.get('Quantity');
Name = (String) ar.get('Name');
}
}
}
VF 页面:
<apex:page controller="TestController">
<apex:form >
<apex:repeat value="{!Summaries}" var="summary">
{!summary.Name}: {!summary.Quantity}<br/>
</apex:repeat>
</apex:form>
</apex:page>
这篇关于如何在 Visualforce 页面上显示聚合 SOQL 查询的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!