9 Replies Latest reply on Jul 11, 2007 9:12 AM by orribl

    Textfield looses focus during reRendering - how to avoid?

      Hi,

      I have a problem with the focus after reRender a area.

      I have a a4j:outputPanel, which contains a table with a list of textfields:

      <a4j:outputPanel id="sumFields" >
       <h:dataTable value="#{bean.fields}" var="field">
       <h:column rendered="#{field.render}">
       <h:inputText value="#{field.value}">
       <a4j:support reRender="sumFields" event="onkeyup"/>
       </h:inputText>
       </h:column>
       </h:dataTable>
      </a4j:outputPanel>


      The goal is to render a column depending on an attribute (field.render). This attribute, in turn, is dependent from the input of the textField.

      Actually this works as expected, but when the panel is reRendered the current textfield looses the focus and I don't know how to avoid this....

      Any ideas?

      Thanks in advance...

        • 1. Re: Textfield looses focus during reRendering - how to avoid
          ilya_shaikovsky

          Works as expected :) input which was focused - deleted from the DOM and inserted again. So scroll lost.

          The solution is to use "focus" attribute which should be defined as id of the component that should get focus after request completed.

          • 2. Re: Textfield looses focus during reRendering - how to avoid

            I don't know how to provide the componente with a unique id...
            When i try to use an attribute from the field object i get the error:

            component identifier must not be a zero-length String


            What version of ajax4jsf/myfaces? are you using?

            • 3. Re: Textfield looses focus during reRendering - how to avoid
              ilya_shaikovsky

              you need to specify the id as id="someId"

              • 4. Re: Textfield looses focus during reRendering - how to avoid

                Hi ilya_shaikovsky,

                I don't know how to do that...
                For example, I have 10 fields in my fields-array, the only way how to provide each field with a unique id is to read some attribute from the field-object.
                I can't just write id="unique" cause i loop through the array to create the fields...
                So if i would write:

                <h:column rendered="#{field.render}">
                 <h:inputText id="unique" value="#{field.value}">
                 <a4j:support focus="unique" reRender="sumFields" event="onkeyup"/>
                 </h:inputText>
                </h:column>

                gives all textfields the same id.
                But when I try something like:
                <h:column rendered="#{field.render}">
                 <h:inputText id="#{field.unique}"value="#{field.value}">
                 <a4j:support focus="#{field.unique}" reRender="sumFields" event="onkeyup"/>
                 </h:inputText>
                </h:column>

                the error
                java.lang.IllegalArgumentException: component identifier must not be a zero-length String

                occurs...

                • 5. Re: Textfield looses focus during reRendering - how to avoid
                  ilya_shaikovsky

                  1) id could not be EL binded

                  h:inputText id="unique" value="#{field.value}">
                  


                  is the right way - it will be unique because the row id will be added by column.

                  • 6. Re: Textfield looses focus during reRendering - how to avoid

                    Its strange, I tried a simplified example:

                    <a4j:outputPanel>
                    <h:inputText id="tmp">
                    <a4j:support focus="tmp" reRender="sumFields" event="onkeyup" />
                    </h:inputText>
                    </a4j:outputPanel>

                    It worked with firefox, but not with the ie...

                    The other example with the datatable doesn't work at all, also when i define an id...
                    What version of ajax4jsf / myfaces are you using...?

                    Thanks

                    • 7. Re: Textfield looses focus during reRendering - how to avoid

                      The only one workaround is DO NOT update itself.

                      • 8. Re: Textfield looses focus during reRendering - how to avoid

                        Hi SergeySmirnov,

                        do you have any idea how to do that? My first idea was to add an actionListener to the a4j:support-Tag:

                        <a4j:support event="onkeyup" actionListener="#{sessionBackingBean.checkSpartenField}" reRender="#{sessionBackingBean.reRenderFields}"/>

                        and wire the reRender-Value with an ArrayList in my Backing Bean.
                        After that I wanted to read the id from the Component which caused the action-event and exclude this component from rendering:

                        public void checkSpartenField(ActionEvent e){
                        
                         //ArrayList with ids for the component which should be rendered
                         this.reRenderFields = new ArrayList();
                        
                         //Get the trigger of the event
                         UIComponent parent = ((UIComponent)e.getSource());
                        
                         //Get the dataTable
                         UIComponent table = ((UIComponent)e.getSource()).getParent().getParent().getParent().getParent().getParent();


                        Up to this point everything works fine, but when I try now to acces the rows, I don't know how I can acchieve this cause I can only acces the static component tree...

                        Any ideas...?

                        • 9. Re: Textfield looses focus during reRendering - how to avoid

                          Nobody an idea how to solve that problem?
                          So I'm assuming that it's not possible to focus the field after it is reRendered...