0

在我的 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;
    }
    
}
4

1 回答 1

0

这不是“在每个部分都执行操作”。您渲染相同的变量 7 次。如果您将小时数输入为 1,2,3,4,5,6,7,我希望它会节省 7,因为这是表格上最后一次提到这一点,该字段的值将“获胜”。

您需要一个列表列表:

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>()
};

然后在按钮操作中,您可以传递额外<apex:param>的数字,从 0 到 6,说明向哪个添加新项目。请注意,传递参数时<apex:commandButton> 有点愚蠢。

或者可能是Map<String, List<Timesheet_Item__c>> = new Map<String, List<Timesheet_Item__c>>{ 'Monday' => new List<Timesheet_Item__c>(), ...};相同的原理,在按钮中传递地图键。

于 2020-11-19T18:02:17.170 回答