目前我正在尝试实现TableTree。但不知何故,它不会扩展节点。我没有发现任何关于如何发生的事情。我的演示设置包含应该作为我的根节点的子节点呈现的数据。
单步执行wicket代码显示节点永远不会将其状态更改为EXPANDED
根元素显示OrderWrapper
- 实体,其集合为OrderItem
。
这是相关代码。如果您需要更多信息,我会尽力提供。
包含TableTree的Panel:
public class CustomerDetailPanel extends Panel implements CustomerSelecting {
@SpringBean
OrderService orderService;
@SpringBean
OrderWrapperDao orderWrapperDao;
public CustomerDetailPanel(String id) {
super(id);
init();
}
public CustomerDetailPanel(String id, IModel<?> model) {
super(id, model);
init();
}
private List<? extends IColumn<CustomerDetailDto, String>> createColumns() {
List<IColumn<CustomerDetailDto, String>> result = new LinkedList<>();
result.add(new TreeColumn<CustomerDetailDto, String>(Model.of("Datum")));
result.add(new PropertyColumn<CustomerDetailDto, String>(Model
.of("Amount"), "amount"));
//more columns...
return result;
}
private void init() {
WebMarkupContainer orders;
Customer c = (Customer) this.getDefaultModelObject();
if (c != null && c.getId() != null) {
CustomerDetailProvider providerForCustomer = orderService
.getProviderForCustomer(c);
List<? extends IColumn<CustomerDetailDto, String>> columns = createColumns();
DefaultTableTree<CustomerDetailDto, String> tree = new DefaultTableTree<CustomerDetailDto, String>(
"orders", columns, providerForCustomer, 20);
orders = tree;
} else {
orders = new WebMarkupContainer("orders");
}
add(orders);
}
@Override
public void performStepCustomerSelected(AjaxRequestTarget target,
Customer customer) {
// Nothing to do yet
}
}
生成ISortableTreeProvider的服务:
@Service
public class OrderService {
public class CustomerDetailProvider implements
ISortableTreeProvider<CustomerDetailDto, String> {
IModel<Customer> customerModel;
ISortState<String> sortState;
public CustomerDetailProvider(Customer customer) {
this(new DefaultLoadableDetachableModel<>(customerDao, customer));
}
public CustomerDetailProvider(IModel<Customer> customerModel) {
this.customerModel = customerModel;
sortState = new SingleSortState<String>();
sortState.setPropertySortOrder("dateofOrderBegin",
SortOrder.ASCENDING);
LOG.info("CustomerDetailProvider created");
}
@Override
public void detach() {
customerModel.detach();
}
@SuppressWarnings("unchecked")
@Override
public Iterator<? extends CustomerDetailDto> getChildren(
CustomerDetailDto node) {
List<OrderItem> childrenList = orderItemDao.findByParentId(
node.getOrderWrapperId()).list();
return transformItemToDto(childrenList).iterator();
}
@Override
public Iterator<? extends CustomerDetailDto> getRoots() {
List<OrderWrapper> rootList = orderWrapperDao
.getForCustomer(customerModel.getObject());
return transformWrapperToDto(rootList).iterator();
}
@Override
public ISortState<String> getSortState() {
return sortState;
}
@Override
public boolean hasChildren(CustomerDetailDto node) {
return node.getOrderWrapperId() != null;
}
@Override
public IModel<CustomerDetailDto> model(final CustomerDetailDto object) {
return new AbstractReadOnlyModel<CustomerDetailDto>() {
@Override
public CustomerDetailDto getObject() {
return object;
}
};
}
private List<CustomerDetailDto> transformItemToDto(
List<OrderItem> childrenList) {
List<CustomerDetailDto> resultList = new LinkedList<CustomerDetailDto>();
DateConverter dateConverter = new DateConverter();
BigDecimalCurrencyConverter currencyConverter = new BigDecimalCurrencyConverter();
Locale locale = Locale.getDefault();
for (OrderItem orderItem : childrenList) {
CustomerDetailDto nextElement = new CustomerDetailDto();
nextElement.setComment("");
nextElement.setDateOfOrderBegin(dateConverter.convertToString(
orderItem.getDateOfCreation(), locale));
nextElement.setTotalValue(currencyConverter.convertToString(
orderItem.getTotalValue(), locale));
nextElement.setBeerName(orderItem.getBeer().getName());
nextElement.setAmount(orderItem.getAmount() + "");
nextElement.setOrderItemId(orderItem.getId());
resultList.add(nextElement);
}
return resultList;
}
private List<CustomerDetailDto> transformWrapperToDto(
List<OrderWrapper> rootList) {
List<CustomerDetailDto> resultList = new LinkedList<CustomerDetailDto>();
DateConverter dateConverter = new DateConverter();
BigDecimalCurrencyConverter currencyConverter = new BigDecimalCurrencyConverter();
Locale locale = Locale.getDefault();
for (OrderWrapper orderWrapper : rootList) {
CustomerDetailDto nextElement = new CustomerDetailDto();
nextElement.setComment(orderWrapper.getComment());
nextElement.setDateOfOrderBegin(dateConverter.convertToString(
orderWrapper.getDateOfOrderBegin(), locale));
nextElement.setOrderWrapperId(orderWrapper.getId());
nextElement.setTotalValue(currencyConverter.convertToString(
orderWrapper.getTotalValue(), locale));
nextElement.setUserName(orderWrapper.getUser().getUserName());
resultList.add(nextElement);
}
return resultList;
}
}
private static final Logger LOG = Logger.getLogger(OrderService.class);
@Autowired
private CustomerDao customerDao;
@Autowired
private OrderItemDao orderItemDao;
@Autowired
private OrderWrapperDao orderWrapperDao;
public OrderService() {
}
public CustomerDetailProvider getProviderForCustomer(Customer c) {
return new CustomerDetailProvider(c);
}
}
DataTransferObject:
public class CustomerDetailDto {
private String amount;
private String beerName;
private String comment;
private String dateOfOrderBegin;
private Long orderItemId;
private Long orderWrapperId;
private String totalValue;
private String userName;
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CustomerDetailDto)) {
return false;
}
CustomerDetailDto other = (CustomerDetailDto) obj;
return other.getOrderItemId() == this.getOrderItemId()
&& other.getOrderWrapperId() == this.getOrderWrapperId();
}
@Override
public int hashCode() {
if (this.getOrderItemId() != null) {
return this.getOrderItemId().hashCode();
}
if (this.getOrderWrapperId() != null) {
return this.getOrderWrapperId().hashCode();
}
return super.hashCode();
}
@Override
public String toString() {
return dateOfOrderBegin;
}
//Getters and setters...
}
提前感谢您的帮助,请原谅我崎岖的英语。
SAM
答案 0 :(得分:0)
毕竟我得到了它。
我的错误似乎是使用AbstractReadOnlyModel
。将功能更改为
@Override
public IModel<CustomerDetailDto> model(final CustomerDetailDto object) {
return Model.of(object);
}
一切正常。感谢阅读这篇文章的每个人。
答案 1 :(得分:0)
阅读ITreeProvider#model():
Important note: The model must implement {@link Object#equals(Object)} and {@link Object#hashCode()} !
模型为您完成此操作,但不是这样做AbstractReadOnlyModel。