gpt4 book ai didi

java - 通过鼠标单击选择时,Wicket 自动完成文本字段不会更新模型

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:47 25 4
gpt4 key购买 nike

我有 wicket 的 AutoCompleteTextField。要更新模型,我使用“onblur”事件。我需要在“onblur”事件发生后刷新文本字段,因为需要验证。

这是说明问题的代码示例

网页子类:

public class TestPage extends WebPage {

private Integer testField;

public TestPage() {

final List<Integer> allowedValues = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
allowedValues.add(50 + i * 5);
}

final PropertyModel<Integer> testModel = new PropertyModel<Integer>(this, "testField");

final AutoCompleteSettings autoCompleteSettings = new AutoCompleteSettings();
autoCompleteSettings.setShowListOnEmptyInput(true);
autoCompleteSettings.setShowListOnFocusGain(true);

final AutoCompleteTextField<Integer> testInput =
new AutoCompleteTextField<Integer>("testInput", testModel, autoCompleteSettings) {
@Override
protected Iterator<Integer> getChoices(final String input) {
return allowedValues.iterator();
}
};

testInput.setOutputMarkupId(true);
testInput.setMarkupId("testInput");
add(testInput);

testInput.add(new AjaxFormComponentUpdatingBehavior("onblur") {
@Override
protected void onUpdate(final AjaxRequestTarget target) {
target.add(testInput);
}
});
}
}

对应的 HTML:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" xml:lang="en"
lang="en">
<body>

<input type="text" wicket:id="testInput"/>

</body>
</html>

问题是无法通过鼠标点击选择值。

我尝试过使用 OnChangeAjaxBehavior - 通过鼠标点击进行选择,但我不想在每次更改后都执行验证(例如,用户想输入 54,他输入 5 =>验证开始是因为 OnChangeAjaxBehavior 被触发)

我尝试结合使用 AjaxFormComponentUpdatingBehavior("onblur")OnChangeAjaxBehavior 我遇到了同样的问题:无法通过鼠标点击选择值,因为'onblur' 在 'onchange' 之前触发

请注意,如果您注释行 target.add(testInput);,它将按预期工作。

好像和这个Wicket类似issue

它说该问题已针对 6.18.0 版本修复,但我使用的是 Wicket 6.18.0 并且仍然存在此问题。

我们一直在执行从 Wicket 1.4 到 wicket 6 的升级。在 Wicket 1.4 中它运行良好。

请给我任何有关如何解决此问题的建议。非常感谢您的帮助。提前致谢。

最佳答案

我建议你使用 wicket Jquery UI 自动完成框:

HTML:

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<wicket:head>
<title>Wicket - jQuery UI: auto-complete</title>
<style type="text/css">
.ui-autocomplete {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
}
</style>
</wicket:head>
</head>
<body>
<wicket:extend>
<div id="wrapper-panel-frame" class="ui-corner-all">
<form wicket:id="form">
<div>Choose your favorite rock genre: (that starts with your criteria)</div>
<br/>
<input wicket:id="autocomplete" type="text" size="30" title="enter your criteria here"></input><br/>
<br/>
<div wicket:id="feedback" style="width: 360px;"></div>
</form>
</div>
</wicket:extend>
</body>
</html>

Java

package com.googlecode.wicket.jquery.ui.samples.pages.autocomplete;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;

public class DefaultAutoCompletePage extends AbstractAutoCompletePage
{
private static final long serialVersionUID = 1L;
private static final List<String> CHOICES = Arrays.asList("Acid rock", "Alternative metal", "Alternative rock", "Anarcho punk", "Art punk", "Art rock", "Beat music", "Black metal", "Blues-rock", "Britpop", "Canterbury scene",
"Chinese rock", "Christian metal", "Crossover Thrash Metal", "Crust punk", "Crustgrind", "Dark cabaret", "Death metal", "Deathcore", "Deathrock", "Desert rock", "Djent", "Doom metal", "Dream pop", "Drone metal",
"Dunedin Sound", "Electronic rock", "Emo", "Experimental rock", "Folk metal", "Folk rock", "Freakbeat", "Funk metal", "Garage punk", "Garage rock", "Glam metal", "Glam rock", "Goregrind", "Gothic metal", "Gothic rock",
"Grindcore", "Groove metal", "Grunge", "Hard rock", "Hardcore punk", "Heavy metal", "Indie pop", "Indie rock", "Industrial metal", "Industrial rock", "J-Rock", "Jazz-Rock", "Krautrock", "Math rock", "Mathcore",
"Melodic Death Metal", "Melodic metalcore", "Metalcore", "Neo-psychedelia", "New Prog", "New Wave", "No Wave", "Noise pop", "Noise rock", "Noisegrind", "Nu metal", "Paisley Underground", "Pop punk", "Pop rock", "Pornogrind",
"Post-Britpop", "Post-grunge", "Post-hardcore", "Post-metal", "Post-punk", "Post-punk revival", "Post-rock", "Power metal", "Power pop", "Progressive metal", "Progressive rock", "Psychedelic rock", "Psychobilly", "Punk rock",
"Raga rock", "Rap metal", "Rap rock", "Rapcore", "Riot grrrl", "Rock and roll", "Rock en Español", "Rock in Opposition", "Sadcore", "Screamo", "Shoegazer", "Slowcore", "Sludge metal", "Soft rock", "Southern rock", "Space Rock",
"Speed metal", "Stoner rock", "Sufi rock", "Surf rock", "Symphonic metal", "Technical Death Metal", "Thrash metal", "Thrashcore", "Twee Pop", "Unblack metal", "World Fusion");

public DefaultAutoCompletePage()
{
// Form //
final Form<Void> form = new Form<Void>("form");
this.add(form);

// FeedbackPanel //
final FeedbackPanel feedback = new JQueryFeedbackPanel("feedback");
form.add(feedback.setOutputMarkupId(true));

// Auto-complete //
form.add(new AutoCompleteTextField<String>("autocomplete", new Model<String>()) {

private static final long serialVersionUID = 1L;

@Override
protected List<String> getChoices(String input)
{
List<String> choices = new ArrayList<String>();
String inputLowerCase = input.toLowerCase();

int count = 0;
for (String choice : CHOICES)
{
if (choice.toLowerCase().startsWith(inputLowerCase))
{
choices.add(choice);

// limits the number of results
if (++count == 20)
{
break;
}
}
}

return choices;

//
// Equivalent to:
// return ListUtils.startsWith(input, CHOICES);
//
}

@Override
protected void onSelected(AjaxRequestTarget target)
{
info("Your favorite rock genre is: " + this.getModelObject());
target.add(feedback);
}
});
}
}

您可以根据需要更改此组件

如果不合适,请检查以下链接:

http://www.7thweb.net/wicket-jquery-ui/autocomplete/DefaultAutoCompletePage;jsessionid=7916524E210E1245C95DCCB697DE6182?0

关于java - 通过鼠标单击选择时,Wicket 自动完成文本字段不会更新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com