2

I have been searching through the net to see if I can figure out how to number rows in visualforce but I cannot find a way that will automatically increment when a row is added. I also cannot find out how to utilize data that is put into a text box.

I want the users to be able to type in a number that corresponds to an entry in the list and that number will be input into a method called DeleteEntry() that does something like this

public void DeleteEntry()
{
  Queue.remove(textbox1.text); 
}

this is my code

Visualforce Page

<apex:page standardController="Case_Note__c" recordSetVar="MassAddCaseNotes" tabStyle="Case_Note__c" extensions="MassAddCaseNotes">



<apex:sectionHeader title="Mass Add Case Notes" />  
<apex:panelGrid columns="2" cellpadding="5" style="margin-bottom: 15px;">
<apex:form style="width:450px" > 

    <apex:pageBlock title="Case Notes" id="block1">

        <apex:pageBlockSection title="Enter Your Notes " columns="2" id="section1">
            <script> twistSection(document.getElementById('{!$Component.block1.section1}').getElementsByTagName('img')[0]) </script>
            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 15px;">
                <apex:outputLabel for="CaseNote">Notes:  </apex:outputLabel>
                <apex:inputField style="width:300px;height:100px;" value="{!Case_Notes.Note__c}" id="CaseNote"  />
                <apex:outputLabel for="CaseNote">Date:  </apex:outputLabel>
                <apex:inputField value="{!Case_Notes.Date__c}" id="Date" />
            </apex:panelGrid>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Find Cases" columns="1">
            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 5px;"> 
                <apex:outputLabel for="contactFilter">Contact Search: </apex:outputLabel>
                <apex:inputField value="{!filtercase.contactid}" id="contactFilter" />
                <apex:commandButton action="{!selectCases}"   value="Filter" />                  
            </apex:panelGrid>

            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 5px;">          
               <apex:PageBlockTable value="{!cases}"  var="case"  id="case">
                <apex:column >
                    <apex:inputCheckbox value="{!selectedCases[case.Id]}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Case Number" action="{!sortCases}">
                            <apex:param name="productSortField" value="CaseNumber" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.CaseNumber}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Record Type" action="{!sortCases}">
                            <apex:param name="productSortField" value="RecordType.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.RecordType.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Contact" action="{!sortCases}">
                            <apex:param name="productSortField" value="Contact.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Contact.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Account" action="{!sortCases}">
                            <apex:param name="productSortField" value="Account.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Account.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Date Opened" action="{!sortCases}">
                            <apex:param name="productSortField" value="Opened_Date__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Opened_Date__c}" />
                </apex:column>
                           </apex:PageBlockTable> 
                           <apex:commandButton action="{!save}" value="Save To Case Queue" status="retrieveSaveStatus" id="btnSave" immediate="false" />
            </apex:panelGrid>

         </apex:pageBlockSection>
     </apex:pageBlock>
</apex:form> 



<apex:form style="width:400px" > 
     <apex:pageBlock title="Case Queue" >
        <apex:pageBlockButtons location="top" >
            <apex:commandButton action="{!submitCases}" value="Submit Notes"  status="retrieveSaveStatus" id="btnSubmitCases" immediate="false" />
        </apex:pageBlockButtons> 

        <apex:actionStatus layout="block" startText="Saving notes. . . " stopText="" id="retrieveSaveStatus" startStyle="color:green; font-style:italic" stopStyle="color:black;"/>
        <apex:pageMessages showDetail="false" />
                <br/>      
                <br/>
                <apex:pageBlockSection title="Case Queue" columns="1">
                     <apex:pageblocktable value="{!queue}" var="que">
                         <apex:column headervalue="Contact Name" value="{!que.Contact_Name__c}"/>
                         <apex:column headervalue="Case Number" value="{!que.Case__c}"/>
                         <apex:column headervalue="Date" value="{!que.Date__c}"/>
                     </apex:pageblocktable>  

                     <apex:panelGrid columns="2" cellpadding="5" style="margin-bottom: 15px;"> 
                         <apex:commandButton action="{!removelast}" value="remove Last"  id="removelast" immediate="false" />
                         <apex:commandButton action="{!removeall}" value="remove all"  id="removeall" immediate="false" />
                     </apex:panelGrid>
                </apex:pageBlockSection>

    </apex:pageBlock>
</apex:form>  
</apex:panelGrid>
</apex:page>

Apex Controller

public class MassAddCaseNotes {


    public Case_Note__c Case_Notes                    {get; set;}
    public List<Case> cases                           {get; private set;}
    public Map<Id, Boolean> selectedCases             {get; set;}
    public Case filterCase                            {get; set;}    
    public String sortField                           {get; set;}
    public boolean direction                          {get; set;}
    public List<Case_Note__c> queue                   {get; set;}
    private ApexPages.StandardSetController allCases  {get; set;}




    private final String selectCasesQuery = 'SELECT Id, CaseNumber, RecordType.Name, Contact.Name, Account.Name, Opened_Date__c, ClosedDate, Program_Location_Assignment__c, Referring_Agency__r.Name ' +
                                            'FROM Case ' +
                                            'WHERE Status=\'Open\' AND RecordTypeId IN (SELECT Id FROM RecordType WHERE SObjectType = \'Case\' AND IsActive = true)';

    public MassAddCaseNotes (ApexPages.StandardSetController controller) 
   {
           queue = new List<case_Note__c>();
           Case_Notes = new Case_Note__c(); 
           filterCase = new Case();
           direction = true;
         //selectCases();
           direction = false;

   }

    public void selectCases() 
    {
        if (sortField == null) sortField = 'CaseNumber';
        String filter = '';
        if (filterCase.RecordTypeId != null) filter += ' AND RecordTypeId = \'' + filterCase.RecordTypeId + '\'';
        if (filterCase.ContactId != null) filter += ' AND ContactId = \'' + filterCase.ContactId + '\'';
        if (filterCase.AccountId != null) filter += ' AND AccountId = \'' + filterCase.AccountId + '\'';

        allCases = new ApexPages.StandardSetController(Database.getQueryLocator(selectCasesQuery + filter +  ' ORDER BY ' + sortField + (direction ? ' ASC' : ' DESC')));
        allCases.setPageSize(1);
        updateCases();

    }


    public void sortCases() 
    {
        direction = !direction;
        selectCases();

    }

    public Boolean hasPrevious {get {return allCases.getHasPrevious();}}
    public Boolean hasNext {get {return allCases.getHasNext();}}

    public void previous() 
    {
        allCases.previous();
        updateCases();
    }

    public void next() 
    {
        allCases.next();
        updateCases();
    }

    private void updateCases() 
    {
        cases = allCases.getRecords();
        selectedCases = new Map<Id, Boolean>();
        for (Case c : cases) 
        {
            selectedCases.put(c.Id, false);
        }
    }


   public void addtoqueue()
   {
        for (Case c : cases) 
        {
            if (selectedCases.get(c.Id)) 
            {

                Case_Note__c items = new Case_Note__c(
                    Case__c = c.Id,
                    Date__c = Case_Notes.Date__c,
                    Contact_Name__c = filtercase.contactid,
                    Note__c = Case_Notes.Note__c 
                );                                
                queue.add(items); 
            }

       }
   }


   public void removelast()
   {
     queue.remove(queue.size()-1);
   }

   public void removeall()
   {
     queue.clear();
   }


   public PageReference save() 
   {       
     addtoqueue();
     return null;
   }


   public PageReference submitCases()
   {
     integer i = 0;
     while(i<queue.size())
     {
        insert queue[i];
        i++;
     }

     queue.clear();

     PageReference pg = new PageReference('/apex/MassAddCaseNotes');
     pg.setRedirect(true);
     return pg;
   }
}
2
  • How is the row being added? Is it via an action taken on your page or do you expect the page to refresh with new entries as they are added to Salesforce?
    – Jagular
    Oct 7 '14 at 18:56
  • They search on the page it's self and add the case to the queue. Oct 7 '14 at 19:02
2

For adding row numbers in pure Visualforce, you can use apex:variable. Like any other variable, you first declare it, then increment it.

<apex:variable var="rowcount" value="{!0}" />

<apex:dataTable ...>
    <apex:column>
        {!rowcount} <apex:variable var="rowcount" value="{!rowcount+1}" />
    </apex:column> ...
 </apex:dataTable>

For the second question, simply declare the a value for use in your controller:

public Integer rowToRemove { get; set; }

Then, in your page, reference the value:

public void removeDesiredRow() {
    if(queue.size()>rowToRemove) {
        queue.remove(rowToRemove);
    }
}

The Visualforce page will note that it's bound to an integer and automatically do conversion for you (and provide a friendly error if the number isn't a number).

In practice, you'll find it easier to use some data-grid library and link the entire thing up with action functions, but if you can't (e.g. company policy against OSS), you can achieve your goals with some fairly simple JavaScript or native Visualforce, although Visualforce tends to be very unresponsive for most demands today.

1
  • Thank you for your help with this. Everything works perfectly. Oct 8 '14 at 15:46
0

I would suggest to use Jquery table to accomplish . It's really and fast as compared to rerendering the VF page.
http://www.datatables.net/

1
  • I will be sure to check out this site. It seems to have really good information. I have never used Jquery; however, it seems very useful and I wouldn't mind incorporating it into my work. Thanks for the advice. Oct 8 '14 at 15:50
0

For everyone's reference, Salesforce doesn't recommend the approach of using to create an iterator that sfdcfox mentions above, and it may not work in < apex:datatable > elements. Salesforce indicates that the functionality may break without notice in future updates. The method that Salesforce recommends and that works OK for elements is to create a wrapper class in Apex that contains an iterator.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.