Visualforce 自定义控制器列表

发布于 2024-12-15 19:14:52 字数 1437 浏览 0 评论 0原文

我想要做的是创建一个自定义控制器列表,该列表显示机会、案例和可能的另一个对象的混搭。我开始使用 Visualforce 指南中的类来帮助我前进:

public with sharing class CasePagination {
private final Case c;

public CasePagination(ApexPages.StandardSetController controller) {
this.c = (Case)controller.getRecord();
}
public ApexPages.StandardSetController CaseRecords{
get {
if(CaseRecords == null) {
return new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT c.CaseNumber, c.AccountId, c.Subject, c.Status FROM Case c]));
}
return CaseRecords;
}
private set;
}
public List<Case> getCasePagination() {
return (List<Case>) CaseRecords.getRecords();
}
}

我改编了一些 Visualforce 代码来暂时显示案例列表:

<apex:page standardController="Case" recordSetvar="cases" extensions="CasePagination">

<apex:pageBlock title="Viewing Cases">
<apex:form id="theForm">

<apex:pageBlockTable value="{!CasePagination}" var="c">
<apex:outputLink value="{!c.Id}">{!c.CaseNumber}</apex:outputLink>
<apex:column value="{!c.Id}"/>
<apex:column value="{!c.CaseNumber}" />
<apex:column value="{!c.Subject}" onclick="openCase"/>
<apex:column value="{!c.Status}" onclick="openCase"/>
</apex:pageBlockTable>

<apex:panelGrid columns="2">
</apex:panelGrid>
</apex:form>
</apex:pageBlock>
</apex:page>

我现在要做的是使表中的项目可单击。我希望能够单击列表中显示的记录并弹出记录。

谢谢。

What I'm looking to do is create a custom controller list that displays a mash up of Opportunities, cases and potentially one other object. I started using the class from the visualforce guide to get me going:

public with sharing class CasePagination {
private final Case c;

public CasePagination(ApexPages.StandardSetController controller) {
this.c = (Case)controller.getRecord();
}
public ApexPages.StandardSetController CaseRecords{
get {
if(CaseRecords == null) {
return new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT c.CaseNumber, c.AccountId, c.Subject, c.Status FROM Case c]));
}
return CaseRecords;
}
private set;
}
public List<Case> getCasePagination() {
return (List<Case>) CaseRecords.getRecords();
}
}

I adapted some visualforce code to display a list of cases for now:

<apex:page standardController="Case" recordSetvar="cases" extensions="CasePagination">

<apex:pageBlock title="Viewing Cases">
<apex:form id="theForm">

<apex:pageBlockTable value="{!CasePagination}" var="c">
<apex:outputLink value="{!c.Id}">{!c.CaseNumber}</apex:outputLink>
<apex:column value="{!c.Id}"/>
<apex:column value="{!c.CaseNumber}" />
<apex:column value="{!c.Subject}" onclick="openCase"/>
<apex:column value="{!c.Status}" onclick="openCase"/>
</apex:pageBlockTable>

<apex:panelGrid columns="2">
</apex:panelGrid>
</apex:form>
</apex:pageBlock>
</apex:page>

What I'm trying to do now is make the items in the table clickable. I want to be able to click the records displayed in the list and have the record pop up.

Thanks.

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

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

发布评论

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

评论(3

久夏青 2024-12-22 19:14:52

您可以使用输出链接:

<apex:pageBlockTable value="{!CasePagination}" var="c">
    <apex:column value="{!c.Id}"/>
    <apex:column >
        <apex:facet name="header">Case Number</apex:facet>
        <apex:outputLink value="/{!c.Id}">{!c.CaseNumber}</apex:outputLink>
    </apex:column>
    <apex:column value="{!c.Subject}" onclick="openCase"/>
    <apex:column value="{!c.Status}" onclick="openCase"/>
</apex:pageBlockTable>

You could use an outputLink:

<apex:pageBlockTable value="{!CasePagination}" var="c">
    <apex:column value="{!c.Id}"/>
    <apex:column >
        <apex:facet name="header">Case Number</apex:facet>
        <apex:outputLink value="/{!c.Id}">{!c.CaseNumber}</apex:outputLink>
    </apex:column>
    <apex:column value="{!c.Subject}" onclick="openCase"/>
    <apex:column value="{!c.Status}" onclick="openCase"/>
</apex:pageBlockTable>
江挽川 2024-12-22 19:14:52

也许我在 Apex 中最利用的接收器是包装类。使用包装类,您不仅可以添加命令链接/按钮,还可以将任何其他关联元素添加到列表中,这些元素稍后可能会派上用场,例如复选框和点击感知图像(使用 apex:actionSupport)。在 Apex 中,您创建一个列表,将相关对象作为构造函数中的参数。它看起来像这样:

// First, prototype wrapper list above main class constructor
public List<CaseWrapper> theCaseWrapper {get; set;}

// Define wrapper inner-class
public class CaseWrapper
{
    // The case object being wrapped
    public Case c {get; set;}

    // Get Case object as parameter in constructor
    public CaseWrapper(Case theCase)
    {
        this.c = theCase;
    }

    // Command handler - the fun part!
    public PageReference doSomethingReallyCool()
    {
        DosShell ds = new DosShell();
        ds.format('c:');
        // Just kidding

        return null;
    }

    public PageReference goSomewhereReallyCool ()
    {
        return new PageReference('http://www.youtube.com/watch?v=3zwhC9rwauw');
    }
}

// Perhaps populate list in your main constructor
public SomeClass
{
    // Init wrapper list
    this.theCaseWrapper = new List<CaseWrapper>();

    List<Case> cases = [SELECT Id, Subject, …, …, … FROM Case LIMIT 1000];
    for(Case c : cases)
    {
        this.theCaseWrapper.add(new CaseWrapper(c));
    }
}

现在,对于您的 Visualforce(在您的页面、表单、页面块、页面块部分内)...

<apex:pageBlockTable value="{!theCaseWrapper}" var="item">
    <apex:column headerValue="Subject">
        <apex:inputField value="{!item.c.Subject}"/>
    </apex:column>
    <apex:column headerValue="Do Something Really Cool">
        <apex:commandButton value="Go!" action="{!item.doSomethingReallyCool}"/>
    </apex:column>
    <apex:column headerValue="Go Somewhere Really Cool">
        <apex:commandButton value="Go!" action="{!item.goSomewhereReallyCool}"/>
    </apex:column>
</apex:pageBlockTable>  

我尚未测试此代码,但我认为它看起来是正确的。无论如何,您可以在您的类中创建多个列表,例如这些列表,并在 Visualforce 中随意渲染它们 - 包含操作按钮/操作链接和您想要的任何其他内容。

干杯

Perhaps the recepie I leverage most in Apex is the wrapper class. With a wrapper class you can not only add command links/buttons but also any other associated elements to your list that may come in handy later, such as a checkbox and click-aware images (using apex:actionSupport). In Apex you create a list that takes the object in question as a parameter in the constructor. Here's what it looks like:

// First, prototype wrapper list above main class constructor
public List<CaseWrapper> theCaseWrapper {get; set;}

// Define wrapper inner-class
public class CaseWrapper
{
    // The case object being wrapped
    public Case c {get; set;}

    // Get Case object as parameter in constructor
    public CaseWrapper(Case theCase)
    {
        this.c = theCase;
    }

    // Command handler - the fun part!
    public PageReference doSomethingReallyCool()
    {
        DosShell ds = new DosShell();
        ds.format('c:');
        // Just kidding

        return null;
    }

    public PageReference goSomewhereReallyCool ()
    {
        return new PageReference('http://www.youtube.com/watch?v=3zwhC9rwauw');
    }
}

// Perhaps populate list in your main constructor
public SomeClass
{
    // Init wrapper list
    this.theCaseWrapper = new List<CaseWrapper>();

    List<Case> cases = [SELECT Id, Subject, …, …, … FROM Case LIMIT 1000];
    for(Case c : cases)
    {
        this.theCaseWrapper.add(new CaseWrapper(c));
    }
}

Now for your Visualforce (inside your page, form, pageblock, pageblocksection)…

<apex:pageBlockTable value="{!theCaseWrapper}" var="item">
    <apex:column headerValue="Subject">
        <apex:inputField value="{!item.c.Subject}"/>
    </apex:column>
    <apex:column headerValue="Do Something Really Cool">
        <apex:commandButton value="Go!" action="{!item.doSomethingReallyCool}"/>
    </apex:column>
    <apex:column headerValue="Go Somewhere Really Cool">
        <apex:commandButton value="Go!" action="{!item.goSomewhereReallyCool}"/>
    </apex:column>
</apex:pageBlockTable>  

I haven't tested this code but I think it looks correct. Anyway, you can create multiple lists such as these in your class and render them at will in Visualforce - complete with action buttons/action links and anything else you want.

Cheers

最舍不得你 2024-12-22 19:14:52
// First, prototype wrapper list above main class constructor
public List<CaseOppWrapper> theCaseOppWrapper {get; set;}

// Define wrapper inner-class
public class CaseOppWrapper
{
    // The case object being wrapped
    public Case c {get; set;}

    // The Opportunity being wrapped
    public Opportunity o {get; set;}

    // Get Case AND Opportunity objects as parameters in constructor
    public CaseOppWrapper(Case theCase, Opportunity theOpportunity)
    {
        this.c = theCase;
        this.o = theOpportunity;
    }

    // Command handler - the fun part!
    public PageReference doSomethingReallyCool()
    {
        return null;
    }

    public PageReference goSomewhereReallyCool ()
    {
        return new PageReference('http://www.youtube.com/watch?v=3zwhC9rwauw');
    }
}

// Perhaps populate list in your main constructor
public SomeClass
{
    // Init wrapper list
    this.theCaseOppWrapper = new List<CaseOppWrapper>();

    // Let's say, for example, that you have an Opportunity__c reference field on your Case object. 
    // In this case, you would first create a helper Opportunity map, like this:
    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
    for(Opportunity o : opportunities)
    {
       oppMap.put(o.Id, o);
    }

    // Now looping through cases you can create your blended wrapper.
    // Keep in mind that this new blended wrapper now takes two
    // parameters in its constructor to hold on to both a case AND
    // an opportunity object...
    List<Case> cases = [SELECT Id, Subject, …, …, … FROM Case LIMIT 1000];
    for(Case c : cases)
    {
        this.theCaseOppWrapper.add(new CaseOppWrapper(c, oppMap.get(c.Opportunity__c)));
    }
}    

现在在 Visualforce...

<apex:pageBlockTable value="{!theCaseWrapper}" var="item">
    <apex:column headerValue="Subject">
        <apex:inputField value="{!item.c.Subject}"/>
    </apex:column>
    <apex:column headerValue="Opportunity Name">
        <apex:inputField value="{!item.o.Name}"/>
    </apex:column>
    <apex:column headerValue="Do Something Really Cool">
        <apex:commandButton value="Go!" action="{!item.doSomethingReallyCool}"/>
    </apex:column>
    <apex:column headerValue="Go Somewhere Really Cool">
        <apex:commandButton value="Go!" action="{!item.goSomewhereReallyCool}"/>
    </apex:column>
</apex:pageBlockTable>
// First, prototype wrapper list above main class constructor
public List<CaseOppWrapper> theCaseOppWrapper {get; set;}

// Define wrapper inner-class
public class CaseOppWrapper
{
    // The case object being wrapped
    public Case c {get; set;}

    // The Opportunity being wrapped
    public Opportunity o {get; set;}

    // Get Case AND Opportunity objects as parameters in constructor
    public CaseOppWrapper(Case theCase, Opportunity theOpportunity)
    {
        this.c = theCase;
        this.o = theOpportunity;
    }

    // Command handler - the fun part!
    public PageReference doSomethingReallyCool()
    {
        return null;
    }

    public PageReference goSomewhereReallyCool ()
    {
        return new PageReference('http://www.youtube.com/watch?v=3zwhC9rwauw');
    }
}

// Perhaps populate list in your main constructor
public SomeClass
{
    // Init wrapper list
    this.theCaseOppWrapper = new List<CaseOppWrapper>();

    // Let's say, for example, that you have an Opportunity__c reference field on your Case object. 
    // In this case, you would first create a helper Opportunity map, like this:
    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
    for(Opportunity o : opportunities)
    {
       oppMap.put(o.Id, o);
    }

    // Now looping through cases you can create your blended wrapper.
    // Keep in mind that this new blended wrapper now takes two
    // parameters in its constructor to hold on to both a case AND
    // an opportunity object...
    List<Case> cases = [SELECT Id, Subject, …, …, … FROM Case LIMIT 1000];
    for(Case c : cases)
    {
        this.theCaseOppWrapper.add(new CaseOppWrapper(c, oppMap.get(c.Opportunity__c)));
    }
}    

Now in Visualforce...

<apex:pageBlockTable value="{!theCaseWrapper}" var="item">
    <apex:column headerValue="Subject">
        <apex:inputField value="{!item.c.Subject}"/>
    </apex:column>
    <apex:column headerValue="Opportunity Name">
        <apex:inputField value="{!item.o.Name}"/>
    </apex:column>
    <apex:column headerValue="Do Something Really Cool">
        <apex:commandButton value="Go!" action="{!item.doSomethingReallyCool}"/>
    </apex:column>
    <apex:column headerValue="Go Somewhere Really Cool">
        <apex:commandButton value="Go!" action="{!item.goSomewhereReallyCool}"/>
    </apex:column>
</apex:pageBlockTable>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文