单击建议后,inputtextarea是否会触发任何事件? [英] Does inputtextarea trigger any event after a suggestion is clicked?

查看:109
本文介绍了单击建议后,inputtextarea是否会触发任何事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个inputtextarea,可以在输入查​​询后调用completeMethod ...,它工作正常,并且正在显示建议,但是我需要捕获ajax事件才能知道用户选择了哪个建议.有可能吗?

I have an inputtextarea which calls a completeMethod after a query is entered... that is working fine and the suggestions are displaying, but I woul'd need to catch the ajax event in order to know which suggestion the user has picked. Is it possible?

推荐答案

您正试图像这样使用ajax itemSelect事件:

You are trying to use the ajax itemSelect event like so:

<h:form>
    <p:inputTextarea widgetVar="textarea" completeMethod="#{myBean.complete}">
        <p:ajax event="itemSelect" listener="#{myBean.onSelect}" />
    </p:inputTextarea>
</h:form>

托管bean方法:

public List<String> complete(String filter) {
    List<String> result = new ArrayList<String>();
    for (int i = 0; i < 10; i++)
        result.add(filter + i);
    return result;
}

public void onSelect(SelectEvent<String> e) {
    System.out.println(e.getObject());
}

这在Primefaces 7.0(可能更早)中不起作用,因为 inputTextarea的渲染器失败添加clientBehavior配置,该配置自7.1版起已固定.

This does not work in Primefaces 7.0 (and probably earlier) because the renderer of the inputTextarea fails to add the clientBehavior configuration which is fixed as of Version 7.1.

在浏览器的javascript控制台中输入:

In the browser javascript console input:

PF('textarea').cfg.behaviors
> undefined

结果undefined表示缺少客户端行为配置.

The result undefined indicates missing client behavior configuration.

您可以通过重写InputTextareaRenderer.encodeScript()方法来解决此问题:

You can work around this by overriding the InputTextareaRenderer.encodeScript() method:

package my.package;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.inputtextarea.InputTextareaRenderer;
import org.primefaces.expression.SearchExpressionFacade;
import org.primefaces.util.WidgetBuilder;

public class CustomInputTextareaRenderer extends InputTextareaRenderer {

    @Override
    protected void encodeScript(FacesContext context, InputTextarea inputTextarea) throws IOException {
        String clientId = inputTextarea.getClientId(context);
        boolean autoResize = inputTextarea.isAutoResize();
        String counter = inputTextarea.getCounter();

        WidgetBuilder wb = getWidgetBuilder(context);
        wb.init("InputTextarea", inputTextarea.resolveWidgetVar(), clientId).attr("autoResize", autoResize)
                .attr("maxlength", inputTextarea.getMaxlength(), Integer.MAX_VALUE);

        if (counter != null) {
            UIComponent counterComponent = SearchExpressionFacade.resolveComponent(context, inputTextarea, counter);

            wb.attr("counter", counterComponent.getClientId(context)).attr("counterTemplate",
                    inputTextarea.getCounterTemplate(), null);
        }

        if (inputTextarea.getCompleteMethod() != null) {
            wb.attr("autoComplete", true).attr("minQueryLength", inputTextarea.getMinQueryLength())
                    .attr("queryDelay", inputTextarea.getQueryDelay())
                    .attr("scrollHeight", inputTextarea.getScrollHeight(), Integer.MAX_VALUE);
        }

        // additional line enabling AJAX 'itemSelect' event handling.
        encodeClientBehaviors(context, inputTextarea);

        wb.finish();
    }
}

在faces配置中,添加渲染器:

In faces config, add the renderer:

<faces-config>
  ...
  <render-kit>
      <renderer>
          <component-family>org.primefaces.component</component-family>
          <renderer-type>org.primefaces.component.InputTextareaRenderer</renderer-type>
          <renderer-class>my.package.CustomInputTextareaRenderer</renderer-class>
      </renderer>
  </render-kit>
</faces-config>

快速指示它可以在javascript控制台中运行:

Quick indication that it works in javascript console:

PF('textarea').cfg.behaviors
> Object { itemSelect: itemSelect() }

这篇关于单击建议后,inputtextarea是否会触发任何事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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