0

In my VF page I have 7 sections that represent days in a week. Every section has "Add multiple timesheet item" button. When that button is clicked, new row shows up with fields to fill(Project, Number of working hours..). So, I have 7 buttons with same action, and when I click, for example, on button in 1st section, action executes in every section, and I don't want that. I want executing only in section where button was clicked. This is my code in VF page:

<apex:page Controller="TimesheetController">
<apex:form >
<apex:pageBlock >
    
    <apex:pageBlockSection title="Monday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                       <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                   </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}"/>
            
        </apex:outputPanel>
    </apex:pageBlockSection>
        
    <apex:pageBlockSection title="Tuesday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}" />
        </apex:outputPanel>
    </apex:pageBlockSection>
   
    
    <apex:pageBlockSection title="Wednesday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}"/>
        </apex:outputPanel>
    </apex:pageBlockSection>
    
    
    <apex:pageBlockSection title="Thursday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable> 
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}" />
        </apex:outputPanel>
    </apex:pageBlockSection>
    
    
    <apex:pageBlockSection title="Friday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}" />
        </apex:outputPanel>
    </apex:pageBlockSection>
    
   
    <apex:pageBlockSection title="Saturday" columns="1" >
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/>   
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}" />
        </apex:outputPanel>
    </apex:pageBlockSection>
   
    <apex:pageBlockSection title="Sunday" columns="1">
        <apex:pageBlockSectionItem >
            <apex:pageBlockTable value="{!listItems}" var="sheet_item">
                <apex:column headerValue="Assignment">
                    <apex:inputField value="{!sheet_item.Assignment__c}"/>
                </apex:column>
                <apex:column headerValue="Number of Hours">
                    <apex:inputField value="{!sheet_item.Number_of_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputTextarea value="{!sheet_item.Description__c}" rows="5" cols="30"/> 
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSectionItem>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Add Timesheet Item" action="{!addItem}"/>
        </apex:outputPanel>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Contentment, PTO" columns="1">
        <br/>
        <apex:inputField label="Mood" value="{!timesheet.Mood__c}"/>
        <br/>
        <apex:inputTextarea label="Upcoming PTO" value="{!timesheet.Upcoming_PTO__c}" rows="5" cols="30"/> 
        <br/>
        <apex:inputTextarea label="Comment" value="{!timesheet.Comment__c}" rows="5" cols="30"/>
        <br/>
        <apex:outputPanel layout="block" html-align="center">
            <apex:commandButton value="Submit" action="{!saveDetails}" style="float:centre" />
        </apex:outputPanel>
    </apex:pageBlockSection>

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

And this is my Controller:

public class TimesheetController{
    
    Timesheet_Item__c item = new Timesheet_Item__c();
    public list<Timesheet_Item__c> listItems{get; set;}
    public Timesheet__c timesheet{get; set;} 

    public TimesheetController () {
        timesheet = new Timesheet__c();
        listItems = new list<Timesheet_Item__c>();
        listItems.add(item);
    }
 
    public void addItem() {
        Timesheet_Item__c sheet_item = new Timesheet_Item__c();
        listItems.add(sheet_item);
    }

    public PageReference saveDetails() {
        for(Integer i=0; i<listItems.size(); i++) {
            insert listItems;
        }
        insert timesheet;
        return null;
    }
    
}
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107

1 Answers1

0

It's not "action executes in every section". You render same variable 7 times. If you enter hours into it as 1,2,3,4,5,6,7 I expect it'll save 7 because that's the last time this is mentioned on the form, this field's value will "win".

You need a list of lists:

List<List<Timesheet_Item__c>> l = new List<List<Timesheet_Item__c>>{
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>(),
    new List<Timesheet_Item__c>()
};

and then in the button action you can pass extra <apex:param> with number from 0 to 6, saying to which one add new item. Just watch out, <apex:commandButton> is bit stupid when passing parameters.

Or maybe a Map<String, List<Timesheet_Item__c>> = new Map<String, List<Timesheet_Item__c>>{ 'Monday' => new List<Timesheet_Item__c>(), ...}; Same principle, pass the map key in the button.

eyescream
  • 18,088
  • 2
  • 34
  • 46