I implemented a Search for a simple ListView but could be used also for any
DataView.
This solution also highlights the found String if you add the class
'highlighted' to you CSS.
The table part:
final WebMarkupContainer table = new WebMarkupContainer("table");
table.setOutputMarkupId(true);
table.add(new TableBehavior().condensed().striped());
add(table);
final TextField<String> searchQuery = new
TextField<String>("searchQuery", Model.of(""));
searchQuery.add(new AjaxFormComponentUpdatingBehavior("onkeyup") {
private static final long serialVersionUID =
314186226258079912L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
target.add(table);
target.appendJavaScript(
"$('.searchResult').find('td:contains(' +
$('.searchQuery').val() + ')').each(function () {\n"
+ "
$(this).html($(this).html().replace($('.searchQuery').val(), '<span
class=\"highlighted\">' + $('.searchQuery').val() + '</span>'));\n"
+ " });");
}
});
add(searchQuery);
IModel<List<ActionJournal>> filteredHistoryEntries = new
AbstractReadOnlyModel<List<ActionJournal>>() {
private static final long serialVersionUID =
6375514913998162927L;
@Override
public List<ActionJournal> getObject() {
if ((searchQuery.getInput() == null) ||
StringUtils.EMPTY.equals(searchQuery.getInput())) {
return model.getObject();
} else {
List<ActionJournal> filtered = model.getObject();
CollectionUtils.filter(filtered, new
HistoryFilter(searchQuery.getInput()));
return filtered;
}
}
};
ListView<ActionJournal> historyEntries = new
ListView<ActionJournal>("historyEntries", filteredHistoryEntries) {
private static final long serialVersionUID =
389308185477622395L;
@Override
protected void populateItem(ListItem<ActionJournal> item) {
item.setDefaultModel(new
CompoundPropertyModel<ActionJournal>(item.getModel()));
item.add(new DateLabel("created", new CcaDateConverter()));
item.add(new Label("createdByLogin"));
item.add(new Label("contactChannel.name"));
item.add(new Label("action.name"));
item.add(new Label("reason.name"));
item.add(new Label("actionText"));
}
};
table.add(historyEntries);
The Predicate for comparsion (you need to replace the evaluation to check for
your object fields)
private final class HistoryFilter implements Predicate {
private final String searchQuery;
private HistoryFilter(String searchQuery) {
this.searchQuery = searchQuery;
}
@Override
public boolean evaluate(Object object) {
if (object instanceof ActionJournal) {
ActionJournal entry = (ActionJournal) object;
String q = searchQuery.toLowerCase();
if ((entry.getCreatedByLogin() != null)
&&
StringUtils.contains(entry.getCreatedByLogin().toLowerCase(), q)) {
return true;
} else if ((entry.getActionText() != null)
&&
StringUtils.contains(entry.getActionText().toLowerCase(), q)) {
return true;
} else if ((entry.getContactChannel() != null)
&&
StringUtils.contains(entry.getContactChannel().getName().toLowerCase(), q)) {
return true;
} else if ((entry.getAction() != null)
&&
StringUtils.contains(entry.getAction().getName().toLowerCase(), q)) {
return true;
} else if ((entry.getReason() != null)
&&
StringUtils.contains(entry.getReason().getName().toLowerCase(), q)) {
return true;
}
}
return false;
}
}
Marvin Richter
-----Original Message-----
From: Sandor Feher [mailto:[email protected]]
Sent: Thursday, February 13, 2014 10:45 AM
To: [email protected]
Subject: Re: AjaxFormSubmitBehavior and FilterForm problem
Yes, I use TextFilteredPropertyColumn and FilterToolbar.
So it might better to use js to solve this.
--
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/AjaxFormSubmitBehavior-and-FilterForm-problem-tp4664422p4664429.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]