开发者

How to set the focus after a zone update in Tapestry5

开发者 https://www.devze.com 2023-02-04 05:25 出处:网络
I have a zone that contains a form that contains a loop.When someone changes a text field within the loop, it updates the entire form and loop on the onKeyUp event.

I have a zone that contains a form that contains a loop. When someone changes a text field within the loop, it updates the entire form and loop on the onKeyUp event.

I开发者_如何学C am trying to find a way to tell Tapestry to return the focus to the text field that was last updated. I think I have this working, but the zone update appears to make the text field lose focus immediately after the focus is set. I can set the focus to a field that is outside of the zone without a problem, so it appears to be the zone update that is causing the issue.

Any suggestions on how to handle this?


Sounds like you need to set the focus after the zone is done reloading. Whenever I need to do a task like that I set up an observer that listens for the Tapestry zone-updated event in Javascript:

$('formZone').observe(Tapestry.ZONE_UPDATED_EVENT, function(event) {
    // set the focus
});   

Hope this helps.


I know this is an old thread, but what I did was:

Using jquery:

<t:zone t:id="formZone">
<t:form .... class="formId" t:zone="formZone>
.
.
</t:form>

jQuery.noConflict();
(function($) { 
  $(document).ready(function() {
      $(".formId").find("input, select").filter(function(){
          if($(this).val() == ""){           
             return true;
          }
        }).first().focus();

  });
})(jQuery);


</t:zone>

This will get focus on first empty input or select. Of course you have to include jquery on your page. noConflict makes sure you wont have problems with prototype.


The Form component has an autofocus parameter which is true by default, causing a bit of JavaScript to be rendered with the form. The JS sets the focus to the first form element after the form has loaded.

Setting autofocus to false disables this behaviour:

<form t:type="Form" t:autofocus="false">
0

精彩评论

暂无评论...
验证码 换一张
取 消