2013-03-06 37 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

相關問題