现在关注Wicket: FileUploadField with ListView我需要允许用户使用FileUploadField旁边的按钮逐个删除文件。
当文件已经保存时,它可以正常工作,但是当它们不在时,FileUploadField会被重置...
所以我试过这个:
AjaxLink<Void> deleteLink = new AjaxLink<Void>("deleteLink") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
EtdConfigForm parent = (EtdConfigForm)EtdDokumentRowForm.this.getParent().getParent().getParent().getParent().getParent();
EtdDokumentRowPanel panel = (EtdDokumentRowPanel)EtdDokumentRowForm.this.getParent();
parent.removeDokument(panel);
target.prependJavascript("var itemRemove = document.getElementById('"+ panel.getMarkupId() + "');" +
"itemRemove.parentNode.removeChild(itemRemove);" +
"Wicket.$('" + panel.getParent().getParent().getMarkupId() + "').children('"+ panel.getMarkupId()+"').remove();");
target.addComponent(parent);
}
};
add(deleteLink);
但它不起作用......我的意思是ajax片......我做错了什么?
答案 0 :(得分:1)
首先,你应该根据当前组件的位置避免对方法的这种冗长的调用,因为它根本不灵活。
要查找键入的父组件,您可以使用组件的findParent(final Class<Z> c)
方法。
实际上,我无法理解AjaxLink
的位置,但我认为它应该直接位于EtdDokumentRowPanel
FileUploadField
附近。
因此,要删除整个EtdDokumentRowPanel
,首先应将其从RepeatingView
中删除,然后执行删除标记的脚本。当然,如果你把这个面板存放在其他地方(我想在EtdConfigForm
中),那么它也应该从那里删除。
不要在父对象中搜索RepeatingView
,最好通过构造函数将其传输到每个EtdDokumentRowPanel
。
会是这样的:
public class EtdDokumentRowPanel extends Panel
{
...
public EtdDokumentRowPanel ( String id, EtdDokument doc, final RepeatingView view )
{
//your panel should have markup id:
setOutputMarkupId ( true );
...
AjaxLink<Void> deleteLink = new AjaxLink<Void> ( "deleteLink" )
{
@Override
public void onClick ( AjaxRequestTarget target )
{
EtdDokumentRowPanel thisPanel = EtdDokumentRowPanel.this;
// removing row panel from RepeatingView:
view.remove (thisPanel);
// execute this js to remove child from Wicket parent component:
target.prependJavascript (
"Wicket.$('" + view.getParent ().getMarkupId () + "')" +
".removeChild(Wicket.$('" + thisPanel.getMarkupId () +
"'));" );
}
};
add ( deleteLink );
...
}
}