在我的 VF 页面中,我有 7 个部分代表一周中的几天。每个部分都有“添加多个时间表项目”按钮。单击该按钮时,将显示新行,其中包含要填写的字段(项目、工作小时数..)。因此,我有 7 个具有相同操作的按钮,例如,当我单击第 1 部分中的按钮时,操作会在每个部分中执行,我不希望这样。我只想在单击按钮的部分执行。这是我在 VF 页面中的代码:
<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>
这是我的控制器:
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;
}
}