连接SearchTableAction头与服务器端搜索的正确方法是什么?(现在示例使用LocalListDataStore并通过SearchFilter在客户端过滤它,常规GWT将使用CellTables和AsyncDataProvider)
现在获胜的选项(这似乎并不理想)将复制SearchTableAction UI设置在构造函数中,并创建一个新的类,将在服务器端调用完成后调用LocalDataStore上的setData。
你实际上不需要复制UI,搜索行动表标题过滤器将火灾事件时改变,任何这些事件数据存储可以听和读搜索过滤器从事件和使用它来加载/过滤数据,在本地LocalDatStore这和过滤数据,远程数据存储将读取这些过滤器并将它们发送到服务器,服务器将过滤数据,返回结果给客户端。
下面是一个示例远程数据存储
package org.dominokit.domino.ui.sample.store;
import org.dominokit.domino.ui.sample.client.Formats;
import org.dominokit.domino.ui.sample.shared.store.CanLoadItems;
import org.dominokit.domino.ui.sample.shared.store.LoadContext;
import org.dominokit.domino.ui.datatable.events.SearchEvent;
import org.dominokit.domino.ui.datatable.events.SortEvent;
import org.dominokit.domino.ui.datatable.events.TableEvent;
import org.dominokit.domino.ui.datatable.events.TablePageChangeEvent;
import org.dominokit.domino.ui.datatable.model.Filter;
import org.dominokit.domino.ui.datatable.model.FilterTypes;
import org.dominokit.domino.ui.datatable.store.DataChangedEvent;
import org.dominokit.domino.ui.datatable.store.DataStore;
import org.dominokit.domino.ui.datatable.store.StoreDataChangeListener;
import org.dominokit.domino.ui.pagination.HasPagination;
import org.gwtproject.i18n.client.DateTimeFormat;
import java.util.*;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.datatable.events.SearchEvent.SEARCH_EVENT;
import static org.dominokit.domino.ui.datatable.events.SortEvent.SORT_EVENT;
public class RemoteDataStore<T> implements DataStore<T> {
private List<StoreDataChangeListener<T>> listeners = new ArrayList<>();
private HasPagination pagination;
private CanLoadItems<T> uiHandlers;
private String propertyName;
private String sortDirection;
private int activePage = 0;
private Map<String, String> searchProperties = new HashMap<>();
private List<T> items;
public RemoteDataStore(CanLoadItems<T> uiHandlers) {
this.uiHandlers = uiHandlers;
}
@Override
public void onDataChanged(StoreDataChangeListener<T> dataChangeListener) {
listeners.add(dataChangeListener);
}
@Override
public void removeDataChangeListener(StoreDataChangeListener<T> dataChangeListener) {
listeners.remove(dataChangeListener);
}
@Override
public void load() {
uiHandlers.load(getLoadContext(), loadResult -> {
this.items = loadResult.getResourceList();
if (nonNull(loadResult.getPage())) {
pagination.updatePagesByTotalCount(loadResult.getPage().getTotalElements(), loadResult.getPage().getSize());
pagination.gotoPage(loadResult.getPage().getNumber() + 1, true);
}
fireUpdate();
});
}
private LoadContext getLoadContext() {
return new LoadContext(propertyName, sortDirection, activePage, searchProperties);
}
@Override
public void handleEvent(TableEvent event) {
switch (event.getType()) {
case TablePageChangeEvent.PAGINATION_EVENT:
activePage = pagination.activePage() - 1;
load();
break;
case SORT_EVENT:
propertyName = ((SortEvent<?>) event).getColumnConfig().getName();
sortDirection = ((SortEvent<?>) event).getSortDirection().toString();
load();
break;
case SEARCH_EVENT:
searchProperties = new HashMap<>();
List<Filter> filters = ((SearchEvent) event).getFilters();
this.activePage = 0;
for (Filter filter : filters) {
if (!filter.getValues().isEmpty()) {
if (filter.getType().equals(FilterTypes.DATE)) {
String longDate = filter.getValues().get(0);
String formattedDate = DateTimeFormat.getFormat(Formats.DEFAULT_DATE_PATTERN)
.format(new Date(Long.parseLong(longDate)));
searchProperties.put(filter.getFieldName(), formattedDate);
} else {
searchProperties.put(filter.getFieldName(), filter.getValues().get(0));
}
}
}
load();
break;
}
}
private void fireUpdate() {
listeners.forEach(dataChangeListener -> dataChangeListener.onDataChanged(new DataChangedEvent<>(items, items.size())));
}
public HasPagination getPagination() {
return pagination;
}
public void setPagination(HasPagination pagination) {
this.pagination = pagination;
}
public int getActivePage() {
return activePage;
}
public void load(int pageNumber) {
this.activePage = pageNumber;
load();
}
}
这里我们监听事件并将实际的数据加载调用委托给uiHandlers。
请注意,实现远程数据存储取决于服务器实现,因此没有一个远程数据存储适用于所有人,因此使用此示例来实现适用于您的服务器的远程数据存储。
谢谢。(我没有足够的分数在第一个答案上添加评论-所以添加一个新的答案)这是很好的信息!下面是用于服务器通信的GWT Async实现——但是它可以很容易地被重载的load方法替换。
将表添加到页面中,并告诉RemoteDataService如何从服务器加载信息。
SimplePaginationPlugin<Person> simplePaginationPlugin = new SimplePaginationPlugin<>(10);
TableConfig<Person> tableConfig = new TableConfig<>();
tableConfig
.addColumn(ColumnConfig.<Person>create("id", "#").textAlign("right").asHeader()
.setCellRenderer(cell -> TextNode.of(cell.getTableRow().getRecord().getId() + "")))
.addColumn(ColumnConfig.<Person>create("status", "Status").textAlign("center").setCellRenderer(cell -> {
if (cell.getTableRow().getRecord().isActive()) {
return Style.of(Icons.ALL.check_circle()).setColor(Color.GREEN_DARKEN_3.getHex()).element();
} else {
return Style.of(Icons.ALL.highlight_off()).setColor(Color.RED_DARKEN_3.getHex()).element();
}
}))
.addColumn(ColumnConfig.<Person>create("firstName", "Name")
.setCellRenderer(cell -> TextNode.of(cell.getTableRow().getRecord().getName())));
tableConfig
.setFixed(true)
.addPlugin(new SortPlugin<>())
.addPlugin(new HeaderBarPlugin<Person>("Demo table", "this a sample table with all features")
.addActionElement(new HeaderBarPlugin.SearchTableAction<>()))
.addPlugin(simplePaginationPlugin);
;
RemoteDataStore<Person> remoteDataStore = new RemoteDataStore<Person>() {
@Override
public void load() {
greetingService.getPeople(getSearchProperties(), getSortPropertyName(), getSortPropertyDirection(),
new AsyncCallback<ArrayList<Person>>() {
@Override
public void onSuccess(ArrayList<Person> result) {
loadNewItems(result);
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure");
}
});
}
};
remoteDataStore.setPagination(simplePaginationPlugin.getSimplePagination());
DataTable<Person> table = new DataTable<>(tableConfig, remoteDataStore);
layout.getContentPanel()
.appendChild(Card
.create("BASIC TABLE", "By default a table will auto fit columns and allow custom cell content")
.setCollapsible().appendChild(table).element());
//Load the table data on page load
remoteDataStore.load();
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.datatable.events.SearchEvent.SEARCH_EVENT;
import static org.dominokit.domino.ui.datatable.events.SortEvent.SORT_EVENT;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
//import org.dominokit.domino.ui.sample.client.Formats;
import org.dominokit.domino.ui.datatable.events.SearchEvent;
import org.dominokit.domino.ui.datatable.events.SortEvent;
import org.dominokit.domino.ui.datatable.events.TableEvent;
import org.dominokit.domino.ui.datatable.events.TablePageChangeEvent;
import org.dominokit.domino.ui.datatable.model.Filter;
import org.dominokit.domino.ui.datatable.model.FilterTypes;
import org.dominokit.domino.ui.datatable.store.DataChangedEvent;
import org.dominokit.domino.ui.datatable.store.DataStore;
import org.dominokit.domino.ui.datatable.store.StoreDataChangeListener;
import org.dominokit.domino.ui.pagination.HasPagination;
import org.gwtproject.i18n.client.DateTimeFormat;
public abstract class RemoteDataStore<T> implements DataStore<T> {
private List<StoreDataChangeListener<T>> listeners = new ArrayList<>();
private HasPagination pagination;
private String sortPropertyName;
private String sortPropertyDirection;
private Entry<String,String> sortProperties;
private int activePage = 0;
private Map<String, String> searchProperties = new HashMap<>();
private List<T> items;
public RemoteDataStore() {
super();
}
@Override
public void onDataChanged(StoreDataChangeListener<T> dataChangeListener) {
listeners.add(dataChangeListener);
}
@Override
public void removeDataChangeListener(StoreDataChangeListener<T> dataChangeListener) {
listeners.remove(dataChangeListener);
}
public void loadNewItems(List<T> newItems)
{
if(Objects.isNull(getItems()))
{
setItems(new ArrayList<T>());
}
getItems().clear();
getItems().addAll(newItems);
if(Objects.nonNull(pagination))
{
pagination.updatePagesByTotalCount(getItems().size());
pagination.gotoPage(1, true);
}
fireUpdate();
}
@Override
public void handleEvent(TableEvent event) {
switch (event.getType()) {
case TablePageChangeEvent.PAGINATION_EVENT:
setActivePage(pagination.activePage() - 1);
fireUpdate();
break;
case SORT_EVENT:
setSortPropertyName(((SortEvent<?>) event).getColumnConfig().getName());
setSortPropertyDirection(((SortEvent<?>) event).getSortDirection().toString());
load();
break;
case SEARCH_EVENT:
setSearchProperties(new HashMap<>());
List<Filter> filters = ((SearchEvent) event).getFilters();
this.setActivePage(0);
for (Filter filter : filters) {
if (!filter.getValues().isEmpty()) {
if (filter.getType().equals(FilterTypes.DATE)) {
String longDate = filter.getValues().get(0);
String formattedDate = DateTimeFormat.getFormat("dd-MM-yyyy HH:mm:ss")
.format(new Date(Long.parseLong(longDate)));
getSearchProperties().put(filter.getFieldName(), formattedDate);
} else {
getSearchProperties().put(filter.getFieldName(), filter.getValues().get(0));
}
}
}
load();
break;
}
}
private void fireUpdate() {
List<T> updateRecords = getRecordsForPage();
listeners.forEach(dataChangeListener -> dataChangeListener.onDataChanged(new DataChangedEvent<>(updateRecords, updateRecords.size())));
}
private List<T> getRecordsForPage() {
if (nonNull(getPagination())) {
int fromIndex = getPagination().getPageSize() * (getPagination().activePage() - 1);
int toIndex = Math.min(fromIndex + getPagination().getPageSize(), getItems().size());
return new ArrayList<>(getItems().subList(fromIndex, toIndex));
} else {
return new ArrayList<>(getItems());
}
}
public HasPagination getPagination() {
return pagination;
}
public void setPagination(HasPagination pagination) {
this.pagination = pagination;
}
public int getActivePage() {
return activePage;
}
public void load(int pageNumber) {
this.setActivePage(pageNumber);
load();
}
public Map<String, String> getSearchProperties() {
return searchProperties;
}
public void setSearchProperties(Map<String, String> searchProperties) {
this.searchProperties = searchProperties;
}
public void setActivePage(int activePage) {
this.activePage = activePage;
}
public Entry<String,String> getSortProperties() {
return sortProperties;
}
public void setSortProperties(Entry<String,String> sortProperties) {
this.sortProperties = sortProperties;
}
public String getSortPropertyName() {
return sortPropertyName;
}
public void setSortPropertyName(String sortPropertyName) {
this.sortPropertyName = sortPropertyName;
}
public String getSortPropertyDirection() {
return sortPropertyDirection;
}
public void setSortPropertyDirection(String sortPropertyDirection) {
this.sortPropertyDirection = sortPropertyDirection;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
}
@SuppressWarnings("serial")
public class Person implements Serializable {
private int id;
private String name;
private boolean active;
public Person() {
}
public Person(int id, String name, boolean active) {
this.id = id;
this.name = name;
this.active = active;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}