gpt4 book ai didi

java - 如何在 Wicket 口中创建日期过滤列属性?

转载 作者:行者123 更新时间:2023-11-29 03:27:30 25 4
gpt4 key购买 nike

我有一个 Grid 和一个由 IColumns 组成的 List。我可以添加 TextFilteredPropertyColumn,这很管用!但是我怎样才能添加日期过滤的属性列呢?如果你能给我一些例子,那就太好了!谢谢!

最佳答案

我使用 Wicket 1.6 为您制作了以下示例,说明如何使用 TextFitleredPropertyColumn 在 java.util.Date 类型上添加过滤器。

请参阅我的示例中最重要的部分:

new TextFilteredPropertyColumn<FilterPage.Entity, FilterPage.Entity, FilterPage.Entity>(Model.of("Finish Date"), dataProvider.getFilterState(), "finish") {

private static final long serialVersionUID = 1L;

@Override
public Component getFilter(String componentId, FilterForm<?> form) {
@SuppressWarnings({ "unchecked", "rawtypes" })
TextFilter<Date> filter = new TextFilter(componentId, getFilterModel(form), form);
filter.getFilter().add(new DatePicker());
return filter;
}

}

完整示例

FilterPage.html(标记):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" dir="ltr" lang="cs" xmlns:wicket="http://wicket.apache.org">
<head>
<title>Filter Page</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="screen.css" />
</head>
<body>
<div>

<h1>Filter Page</h1>

<div>
<div wicket:id="filterResult">
Start: <wicket:container wicket:id="start" /><br/>
Finish: <wicket:container wicket:id="finish" /><br/>
Note: <wicket:container wicket:id="note" /><br/>
</div>
<form wicket:id="filterForm">
<div wicket:id="goAndClear"></div>
<table wicket:id="table"></table>
</form>
</div>

</div>
</body>
</html>

FilterPage.java(代码):

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.GoAndClearFilter;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilter;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilteredPropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
* @author Martin Strejc
*
*/
public class FilterPage extends WebPage {

private static final long serialVersionUID = 1L;

public FilterPage() {
super();
MySortableDataProvider dataProvider = new MySortableDataProvider();
FilterForm<Entity> filterForm = new FilterForm<>("filterForm", dataProvider);
List<IColumn<Entity, Entity>> columns = new LinkedList<>();
// default filter by auto convert detection
columns.add(new TextFilteredPropertyColumn<FilterPage.Entity, FilterPage.Entity, FilterPage.Entity>(Model.of("Start Date"), dataProvider.getFilterState(), "start"));

columns.add(new TextFilteredPropertyColumn<FilterPage.Entity, FilterPage.Entity, FilterPage.Entity>(Model.of("Finish Date"), dataProvider.getFilterState(), "finish") {

private static final long serialVersionUID = 1L;

@Override
public Component getFilter(String componentId, FilterForm<?> form) {
@SuppressWarnings({ "unchecked", "rawtypes" })
TextFilter<Date> filter = new TextFilter(componentId, getFilterModel(form), form);
filter.getFilter().add(new DatePicker());
return filter;
}

});

// default filtering of String
columns.add(new TextFilteredPropertyColumn<FilterPage.Entity, FilterPage.Entity, FilterPage.Entity>(Model.of("Note"), dataProvider.getFilterState(), "note"));
DefaultDataTable<Entity, Entity> table = new DefaultDataTable<>("table", columns, dataProvider, Integer.MAX_VALUE);
FilterToolbar filterToolbar = new FilterToolbar(table, filterForm, dataProvider);
table.addTopToolbar(filterToolbar);
filterForm.add(table);
filterForm.add(new GoAndClearFilter("goAndClear", filterForm));
add(filterForm);
add(new MyFilterResult("filterResult", filterForm.getModel()));

}

/**
* My own sortable data provider implementation,
* this implementation just provide a fixe list of data
* without any real sorting.
*
* @author Martin Strejc
*
*/
public static class MySortableDataProvider extends SortableDataProvider<Entity, Entity> implements IFilterStateLocator<Entity> {

private static final long serialVersionUID = 1L;

private final List<Entity> list;
private List<Entity> filteredList;

private Entity filterState = new Entity();

/**
* The default constructor that creates the list
*/
public MySortableDataProvider() {
super();
list = new ArrayList<>(5);
list.add(new Entity(new GregorianCalendar(2013, 5, 3).getTime(), new GregorianCalendar(2013, 5, 10).getTime(), "the first field"));
list.add(new Entity(new GregorianCalendar(2013, 5, 3).getTime(), new GregorianCalendar(2013, 5, 13).getTime(), "the second field"));
list.add(new Entity(new GregorianCalendar(2013, 7, 3).getTime(), new GregorianCalendar(2013, 8, 12).getTime(), "the middler field"));
list.add(new Entity(new GregorianCalendar(2013, 8, 3).getTime(), new GregorianCalendar(2013, 8, 10).getTime(), "the field no. 4"));
list.add(new Entity(new GregorianCalendar(2013, 11, 3).getTime(), new GregorianCalendar(2013, 12, 2).getTime(), "the last field"));
}

public List<Entity> createFilteredList() {
List<Entity> ret = new LinkedList<>();
for(Entity e : list) {
if(filterState != null) {
if(filterState.getStart() != null && !filterState.getStart().equals(e.getStart())) {
continue;
}
if(filterState.getFinish() != null && !filterState.getFinish().equals(e.getFinish())) {
continue;
}
if(filterState.getNote() != null && !filterState.getNote().equals(e.getNote())) {
continue;
}
}
ret.add(e);
}
return ret;
}

public List<Entity> getFilteredList() {
if(filteredList == null) {
filteredList = createFilteredList();
}
return filteredList;
}

@Override
public Iterator<? extends Entity> iterator(long first, long count) {
return getFilteredList().iterator();
}

@Override
public long size() {
return getFilteredList().size();
}

@Override
public IModel<Entity> model(Entity object) {
return Model.of(object);
}

@Override
public Entity getFilterState() {
return filterState;
}

@Override
public void setFilterState(Entity state) {
this.filterState = state;
}

@Override
public void detach() {
super.detach();
filteredList = null;
}

}

public static class MyFilterResult extends WebMarkupContainer {

private static final long serialVersionUID = 1L;

/**
* @param id
* @param model
*/
public MyFilterResult(String id, IModel<Entity> model) {
super(id, CompoundPropertyModel.of(model));
add(new Label("start"));
add(new Label("finish"));
add(new Label("note"));
}


}

/**
* The entity class represent any entity
* that's usable as a model object
*
* @author Martin Strejc
*
*/
public static class Entity implements Serializable {

private static final long serialVersionUID = 1L;

private Date start;
private Date finish;
private String note;


/**
* Default constructor
*/
public Entity() {
super();
}


/**
* Constructor from all entity fields
*
* @param start
* @param finish
* @param note
*/
public Entity(Date start, Date finish, String note) {
super();
this.start = start;
this.finish = finish;
this.note = note;
}


/**
* @return the start
*/
public Date getStart() {
return start;
}
/**
* @param start the start to set
*/
public void setStart(Date start) {
this.start = start;
}
/**
* @return the finish
*/
public Date getFinish() {
return finish;
}
/**
* @param finish the finish to set
*/
public void setFinish(Date finish) {
this.finish = finish;
}
/**
* @return the note
*/
public String getNote() {
return note;
}
/**
* @param note the note to set
*/
public void setNote(String note) {
this.note = note;
}

}

}

关于java - 如何在 Wicket 口中创建日期过滤列属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20270330/

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