在primefaces自动完成事件中将值重置为null [英] Reset value to null in primefaces autocomplete event

查看:106
本文介绍了在primefaces自动完成事件中将值重置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动完成事件,一旦选择一个值,该事件就会正确触发.我希望在擦除文本框中的值并将其重置为null时触发另一个事件.我当时在考虑使用onChange属性,但是遇到了问题,因此我恢复了原来的代码.

I have an autocomplete event that fires correctly once a value is selected. I want another event to fire once I erase the value in the textbox and reset the value to null. I was thinking of using the onChange attribute but I was having issues so I reverted back to my original code.

<p:autoComplete id="deviceAuto" dropdown="true" scrollHeight="250" 
                value="#{summaryReportController.device.nickname}" 
                forceSelection="true" 
                completeMethod="#{summaryReportController.deviceComplete}">
    <p:ajax event="itemSelect" 
        listener="#{summaryReportController.handleDeviceSelect}" 
        update="printThis" />  
</p:autoComplete> 

public void handleDeviceSelect(SelectEvent event) {
    String deviceSelect = event.getComponent().getId();
    if (deviceSelect.equalsIgnoreCase("deviceAuto")) {
        Device selectedDevice = deviceMgr.getDevicebyNickname(device.getNickname());
        setDevice(selectedDevice);
    }
    updateInterface();
}

推荐答案

修改自动完成文本字段的文本内容时,将在后备bean上调用搜索方法(aka.completeMethod).如果您得到一个空字符串,可以将值重置为null.

When you modify the text content of the AutoComplete textfield, the search method (aka. completeMethod) will be called on the backing bean. You can reset the value to null there if you get an empty string.

后备豆

// insert getter and setter for the device property ...

/** Search for devices by name */
public List<String> deviceComplete(String search) {
    if (StringUtils.isBlank(search)) {
        setDevice(null);  // textfield was cleared, reset device value!
        return Collections.emptyList();
    } else {
        // search for devices ...
        return deviceNames;
    }
}

请注意,我使用了 Apache Commons

Note that I used Apache Commons StringUtils.isBlank(String) to check if the string was null or did only contain whitespace characters.

JSF视图

在您的XHTML文件中,您可能想收听任何Ajax事件以更新您的视图-或者您自己弄清楚自己需要的事件(模糊,更改等):

In your XHTML file you probably want to listen to any Ajax event to update your view -- or you figure out the event you need (blur, change, whatever) yourself:

<p:autoComplete ...>
    <p:ajax event="itemSelect" listener="..." update="..." />
    <p:ajax process="@this" update="..." />
</p:autocomplete>

我希望这会有所帮助.

另一种 可能类似于搜索文本字段旁边的清除"或重置"按钮,以使用户清楚将要清除的值.

An alternative could be something like a "clear" or "reset" button next to the search textfield to make it clear to the user that the value will be cleared.

这篇关于在primefaces自动完成事件中将值重置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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