2013-03-06 40 views
0

我有一个使用standardSetController分页的visualforce页面。在页面上,我还添加了按状态过滤......即。新的,工作的,关闭的等等。现在,我正在为每个状态值执行count()以显示页面上的状态计数,但是这需要查询每个状态,这是低效的。我希望以更有效的方式获得状态计数。获得记录状态计数的更有效方法

这里是我的控制器:

public with sharing class myController { 

public ApexPages.StandardSetController setCtrl{get; set;} 
public String projId { get; set; } 
public String clientName { get; set; } 
private List<Ticket__c> TicketList = new List<Ticket__c>(); 

public myController() 
{ 
    projId = ApexPages.currentPage().getParameters().get('id'); 
    clientName = [Select Client__r.Name From Project__c Where Id =: projId].Client__r.Name; 
    setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select Ticket_Number__c, Name, Status__c, Description__c, Description_of_Resolution__c, CreatedDate from Ticket__c Where Project__c =: projId ORDER BY CreatedDate DESC])); 
    ticketList = (List<Ticket__c>)setCtrl.getRecords(); 
    setCtrl.setPageSize(5); 
} 

public Integer getNewStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'New']; 
} 

public Integer getWorkingStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Working']; 
} 

public Integer getResolvedStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Resolved']; 
} 

public Integer getClosedStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Closed']; 
} 

public Integer getCancelledStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Cancelled']; 
} 

public Integer getReopenedStatusCount() { 
    return [select count() from Ticket__c Where Project__c =: projId and status__c = 'Reopened']; 
} 

public Boolean hasNext { 
    get { 
     return setCtrl.getHasNext(); 
    } 
} 

public Boolean hasPrevious { 
    get { 
     return setCtrl.getHasPrevious(); 
    } 
    set; 
} 

public Integer pageNumber { 
    get { 
     return setCtrl.getPageNumber(); 
    } 
    set; 
} 

public Integer totalPages { 
    get { 
     totalPages = math.ceil((Double)setCtrl.getResultSize()/setCtrl.getPageSize()).intValue(); 
     return totalPages; 
    } 
    set; 
} 

public void first() { 
    setCtrl.first(); 
} 

public void last() { 
    setCtrl.last(); 
} 

public void previous() { 
    setCtrl.previous(); 
} 

public void next() { 
    setCtrl.next(); 
} 


public List<Ticket__c> getTickets() 
{ 
    return (List<Ticket__c>)setCtrl.getRecords(); 
} 

public void filterStatus() { 
    string myParam = apexpages.currentpage().getparameters().get('myParam'); 
    setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select Ticket_Number__c, Name, Status__c, Description__c, Description_of_Resolution__c, CreatedDate from Ticket__c Where Project__c =: projId AND Status__c =: myParam ORDER BY CreatedDate DESC])); 
    setCtrl.setPageSize(5); 
} 
} 

这里是VF代码:

   <div class="contact-form"> 


        <apex:outputPanel id="contactForm"> 
         <apex:form > 
          <ul class="filteredView"> 
           <li> 
            <apex:outputLink value="/tickets?id={!projId}">View All</apex:outputLink> 
           </li> 
           <li> 
            ({!NewStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" New" rerender="contactForm"> 
             <apex:param name="myParam" value="New"/> 
            </apex:commandLink> 
           </li> 
           <li> 
            ({!WorkingStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" Working" rerender="contactForm"> 
             <apex:param name="myParam" value="Working"/> 
            </apex:commandLink> 
           </li> 
           <li> 
            ({!ResolvedStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" Resolved" rerender="contactForm"> 
             <apex:param name="myParam" value="Resolved"/> 
            </apex:commandLink> 
           </li> 
           <li> 
            ({!ClosedStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" Closed" rerender="contactForm"> 
             <apex:param name="myParam" value="Closed"/> 
            </apex:commandLink> 
           </li> 
           <li> 
            ({!CancelledStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" Cancelled" rerender="contactForm"> 
             <apex:param name="myParam" value="Cancelled"/> 
            </apex:commandLink> 
           </li> 
           <li id="last"> 
            ({!ReopenedStatusCount}) 
            <apex:commandLink action="{!filterStatus}" value=" Reopened" rerender="contactForm"> 
             <apex:param name="myParam" value="Reopened"/> 
            </apex:commandLink> 
           </li> 
          </ul> 
          <h5> 
           TICKETS 
          </h5> 
          <table width="95%" border="1" cellpadding="5"> 
           <tr align="left"> 
            <th style="width:25px;">Ticket#</th> 
            <th style="width:200px;">Subject</th> 
            <th>Description</th> 
            <th style="width:50px;">Status</th> 
            <th style="width:75px;"></th> 
           </tr> 
           <apex:repeat value="{!tickets}" var="ticket"> 
            <tr> 
             <td>{!ticket.Ticket_Number__c}</td> 
             <td>{!ticket.Name}</td> 
             <td>{!ticket.Description__c}</td> 
             <td>{!ticket.Status__c}</td> 
             <td><apex:outputLink value="/detail?id={!projId}&ticketId={!ticket.Id}">View Details</apex:outputLink></td> 
            </tr> 
           </apex:repeat> 
          </table> 
          <apex:outputPanel rendered="{!(tickets.empty == false)}" styleClass="pagination" > 
           <ul> 
            <li><apex:commandLink id="first" value="First" action="{!first}" rendered="{!hasPrevious}" /></li> 
            <li><apex:commandLink id="prev" value="Prev" action="{!previous}" rendered="{!hasPrevious}" /></li> 
            <li><apex:commandLink id="next" value="Next" action="{!next}" rendered="{!hasNext}" /></li> 
            <li><apex:commandLink id="last" value="Last" action="{!last}" rendered="{!hasNext}" /></li> 
            <li><apex:outputText value="Page {!pageNumber} of {!totalPages}" /></li> 
           </ul> 
          </apex:outputPanel> 
         </apex:form> 
        </apex:outputPanel>      
       </div> 
     </div> 

感谢。

回答

0

你可以得到各状态下的计数的所有状态的单一SOQL查询

[select status__c, count(id) from Ticket__c Where Project__c =: projId group by status__c] 

然后,您可以通过现有的吸气剂暴露的结果,暴露了查询结果,并将其通过绑定到UI一个中继器。

+0

所以,这是一个返回AggregateResult:'公开名单 getStatusCount(){ \t \t返回[选择status__c,计数(ID)从Ticket__c其中Project__c =:PROJID组由status__c]; \t}'所以,使用中继器结合:'<顶点:重复值= “!{StatusCount}” VAR = “计数”> \t \t \t \t \t \t \t \t <顶点:outputLabel值=“{计数.Status__c}“/> \t \t \t \t \t \t \t'我如何识别字段?状态和数量? – Dman100 2013-03-06 21:50:03

+0

有一个称为动态Visualforce的功能可以帮助解决这个问题,但我没有使用它,所以不知道细节。 – superfell 2013-03-07 03:10:43

相关问题