使用RequestContex执行,使用WidgetVar为Bean中的Primefaces组件设置焦点 [英] Set Focus for Primefaces Component in Bean with WidgetVar with RequestContex Execute

查看:791
本文介绍了使用RequestContex执行,使用WidgetVar为Bean中的Primefaces组件设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试过了:


在使用Primefaces RequestContex函数调用另一个组件后,是否有设置组件焦点的方法?

  RequestContex.getCurrentInstance()。execute(PF('WidgetVar')。focus();); 

 <$ ()()())。getWidgetVar())。focus();); 

我没有收到错误,但没有任何反应。



我使用Primefaces 4.0.3和MyFaces 2.0.2



编辑示例代码

Bean

  import java.io.Serializable; 
import java.util.logging.Logger;

import javax.annotation.PostConstruct;

import org.primefaces.context.RequestContext;

public class NavigationBean实现Serializable {

private static final long serialVersionUID = 1L;
私有字符串输入;

public NavigationBean(){}

@PostConstruct $ b $ public void init(){



public void goNext(){
Logger.getAnonymousLogger()。warning(GONEXT);
RequestContext.getCurrentInstance()。scrollTo(w3);



public String getInput(){
return input;
}

public void setInput(String input){
this.input = input;




$ b $ x $ X $
$ b

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // EN
http:// www。 w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

< html xmlns =http://www.w3.org/1999/xhtml
xmlns:ui =http://java.sun.com/jsf/facelets
xmlns:h =http://java.sun.com/jsf/html
xmlns:f =http://java.sun.com/jsf/core
xmlns:p =http://primefaces.org/ui>
< h:head>


< / h:头>

< h:body>

< h:form id =form>
< h:inputText id =input1value =#{navigationBean.input}widgetVar =w1>< / h:inputText>
< h:inputText id =input2value =#{navigationBean.input}widgetVar =w2>< / h:inputText>
< h:inputText id =input3value =#{navigationBean.input}widgetVar =w3>< / h:inputText>
< h:inputText id =input4value =#{navigationBean.input}widgetVar =w4>< / h:inputText>

< / h:表格>


< / h:body>

< / html>


解决方案

您可以使用<从primefaces的p:focus> 标签,我用它在运行时设置焦点如下:

.xhtml代码:

 < p:focus id =focusIDfor =#{yourBean.focusProperty}/> 

.java code:

  focusProperty =componentIDToSetFocus; 

不要忘记更新< p:focus>的ID。 标记


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

i tried:

RequestContex.getCurrentInstance().execute("PF('WidgetVar').focus();");

and

RequestContex.getCurrentInstance().execute("(((InputText) event.getComponent()).getWidgetVar()).focus();");

I dont get an error but nothing happens.

Did i miss something or is this not possible?

I use Primefaces 4.0.3 and MyFaces 2.0.2

EDIT Example Code

Bean

import java.io.Serializable;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;

import org.primefaces.context.RequestContext;

public class NavigationBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private String input;

    public NavigationBean() {}

    @PostConstruct
    public void init() {

    }

    public void goNext() {
        Logger.getAnonymousLogger().warning("GONEXT");
        RequestContext.getCurrentInstance().scrollTo("w3");

    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

}

XHTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>


</h:head>

<h:body >

<h:form id="form">
<h:inputText id="input1" value="#{navigationBean.input}" widgetVar="w1"></h:inputText>
<h:inputText id="input2" value="#{navigationBean.input}" widgetVar="w2"></h:inputText>
<h:inputText id="input3" value="#{navigationBean.input}" widgetVar="w3"></h:inputText>
<h:inputText id="input4" value="#{navigationBean.input}" widgetVar="w4"></h:inputText>
    <p:commandButton value="focus" action="#{navigationBean.goNext()}" />

</h:form>


</h:body>

</html>

解决方案

You can use <p:focus> tag from primefaces, I used it for setting focus at run time as follows :

.xhtml code:

<p:focus id="focusID" for="#{yourBean.focusProperty}" />

.java code:

focusProperty="componentIDToSetFocus";

don't forget to update the ID of <p:focus> tag

这篇关于使用RequestContex执行,使用WidgetVar为Bean中的Primefaces组件设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆