Set Focus for Primefaces Component in Bean with WidgetVar with RequestContex Execute

前端 未结 4 1854
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 04:14

Is there a way to set my component focus after an function call to an other component with Primefaces RequestContex?

i tried:

RequestContex.getCurrentIns         


        
4条回答
  •  滥情空心
    2021-01-29 04:39

    Looks like you're under a couple of misconceptions here:

    1. That you can execute Java code in RequestContext API.

      RequestContext.getCurrentInstance().execute("(((InputText) event.getComponent()).getWidgetVar()).Focus();");
      

      You can't. The code above will not work. execute is built for JavaScript only.

    2. The function name you're looking for is focus(). JS is case-sensitive. So Focus <> focus. So I think what you should have there is

      RequestContext.getCurrentInstance().execute("yourClientSideId.focus();");
      

      Where yourClientSideIdshould correspond to the widgetVar attribute of the component you're interested in. I'm not sure what the PF object you're referencing in your code is supposed to be. I've never seen it. Perhaps you meant the PrimeFaces object?

    In the event of failure of the above, you could always use the scrollTo method on RequestContext:

    RequestContext.getCurrentInstance().scrollTo("yourComponentWidgetVar");
    

提交回复
热议问题