【问题标题】:Multiple buttons with same action具有相同操作的多个按钮
【发布时间】:2021-03-02 17:49:47
【问题描述】:

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

【问题讨论】:

    标签: salesforce apex visualforce salesforce-development


    【解决方案1】:

    这不是“在每个部分都执行操作”。您渲染相同的变量 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>()
    };
    

    然后在按钮操作中,您可以传递额外的&lt;apex:param&gt;,编号从 0 到 6,说明向哪个添加新项目。请注意,&lt;apex:commandButton&gt;is bit stupid 传递参数时。

    也可能是Map&lt;String, List&lt;Timesheet_Item__c&gt;&gt; = new Map&lt;String, List&lt;Timesheet_Item__c&gt;&gt;{ 'Monday' =&gt; new List&lt;Timesheet_Item__c&gt;(), ...};,原理一样,在按钮中传递map键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多