Salesforce Visualforce不读取元素下方的变量

Salesforce Visualforce不读取元素下方的变量,salesforce,visualforce,apex,Salesforce,Visualforce,Apex,好的,所以标题很混乱,这个问题很可能太复杂了,因为我对整个VF/Apex都不熟悉,不知道合适的术语。 这个想法是当用户选择另一个选项时,控制器将连接文本框的值!将其他字符串插入自定义对象。 问题是我希望输入文本框位于选择菜单下方,但如果我将其置于菜单下方,则用户输入不再可读 这项工作: <apex:inputText value="{!otherString}" id="otherText" /> <apex:selectCheckboxes layout="P

好的,所以标题很混乱,这个问题很可能太复杂了,因为我对整个VF/Apex都不熟悉,不知道合适的术语。 这个想法是当用户选择另一个选项时,控制器将连接文本框的值!将其他字符串插入自定义对象。 问题是我希望输入文本框位于选择菜单下方,但如果我将其置于菜单下方,则用户输入不再可读

这项工作:

    <apex:inputText value="{!otherString}" id="otherText" />
    <apex:selectCheckboxes layout="PageDirection" value="{!other}" id="standardMap" required="true">                    
          <apex:selectOptions value="{!shareTypes}" />
    </apex:selectCheckboxes>
输出:其他:用户键入的模型

这不起作用:

    <apex:selectCheckboxes layout="PageDirection" value="{!other}" id="standardMap" required="true">                    
          <apex:selectOptions value="{!shareTypes}" />
    </apex:selectCheckboxes>
    <apex:inputText value="{!otherString}" id="otherText" />
输出:Other:null为空,因为它无法读取文本框

相关控制器代码:

List<String> other = new List<String>{};
public String otherString{get;set;}

      public List<String> getOther() {
          return other;
      }  

      public void setOther(List<String> other) {

           Set<String> mySet = new Set<String>();
           mySet.addAll(other);
           if (mySet.Contains('Other'))
           {        

              String result = String.join(other, ', ' );
              control.Share_Types__c = result + ': ' +otherString;

           }
           else
           {
                String result = String.join(other, ', ' );
                control.Share_Types__c = result;
           }
       }
但是如果我把它放在菜单下面,用户输入就不再可读了

您的代码依赖于getter和setter方法的执行顺序,如果启用调试日志记录或启动开发人员控制台,您将看到get/set语句的顺序与表达式在页面上的显示顺序相匹配。。。但你可能会看到有东西不止一次开火

Salesforce建议不要依赖get/set-fire的顺序,应用程序不应该关心setOther是调用一次还是十次,因此在Setter中不应该有复杂的查询作为副作用

我假设您已经有了一些绑定到apex:commandButton或apex:commandLink的操作方法-您应该将此逻辑从setter移到该方法。您是否有自己的方法,或者您正在使用StandardController的{!save}作为示例

进一步阅读:

检查带有红色感叹号的警告 无耻的插头:P 编辑-您提到您正在考虑一些提交,而用户无需单击任何按钮。检查这样的东西是否接近你所需要的

public class Checkboxes{

    public List<SelectOption> options {get;private set;}
    public List<String> selected {get;set;}

    public Boolean otherSelected {get; private set;}
    public String otherOption {get;set;}

    public transient String output {get; private set;}

    public Checkboxes(){
        options = new List<SelectOption>{
            new SelectOption('Apex', 'Apex'),
            new SelectOption('JavaScript', 'JavaScript'),
            new SelectOption('Visualforce', 'Visualforce'),
            new SelectOption('Other', 'Other')
        };
        selected = new List<String>();
        otherSelected = false;
    }

    public void skillsChanged(){
        try{
            Set<String> temp = new Set<String>();
            temp.addAll(selected);

            otherSelected = temp.contains('Other');

            if(otherSelected){
                output =  'Other : ' + (String.isBlank(otherOption) ? '<not specified>' : otherOption);
            } else {
                output = String.join(selected, ', ');
            }
            System.debug(output);
        } catch(Exception e){
            ApexPages.addMessages(e);
        }
    }
}

<apex:page controller="Checkboxes" tabStyle="Account">
<apex:pageMessages />
<apex:form>
    <apex:pageblock id="block">
        <apex:pageBlockButtons location="top">
            <apex:actionStatus id="status">
                <apex:facet name="start"><img src="/img/loading.gif" alt="Loading" title="Loading"/></apex:facet>
            </apex:actionStatus>
        </apex:pageBlockButtons>

        <apex:pageBlockSection title="input" columns="1">
            <apex:selectCheckboxes label="Your skills" value="{!selected}" required="true">                    
                <apex:selectOptions value="{!options}" />
                <apex:actionSupport action="{!skillsChanged}" event="onchange" status="status" rerender="messages, block"/>
            </apex:selectCheckboxes>

            <apex:inputText label="Please Specify" value="{!otherOption}" rendered="{!otherSelected}">
                <apex:actionSupport action="{!skillsChanged}" event="onchange" status="status" rerender="messages, text"/>
            </apex:inputText>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="output">
            <apex:outputText value="{!output}" id="text"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

我使用的是我自己的apex类,不是标准的控制器,但它没有到submit按钮的方法链接。唯一的问题是,我试图使用该对象的输入动态地使第二个对象可见或隐藏。所以我不想等到他们点击submit或其他按钮来切换元素的可见性。