Wicket 6:使用LinkPropertyColumn

时间:2016-12-12 10:22:14

标签: java datatable wicket

我想生成一个只包含一个数据表的页面。在此数据表中,一列应包含一个链接,该链接在用户单击时执行某些操作。 使用LinkPropertyColumn的各种实现似乎是一项直接的任务。在测试了许多稍微不同的实现之后,我决定使用提供的here的implmentation。所有实现都提供了相同的结果。使用正确的文本生成的表格完全正常,但列中生成的链接不可点击。当我点击它们时没有任何反应。鼠标悬停时,它们也没有下划线,尽管它们以蓝色表示为常用链接。当我添加一些我发现的文本时,永远不会调用AjaxLink的onClick方法。如果有人能给我一个提示,我真的很感激。

这是我的Page class:

@AuthorizeInstantiation("CHART_CONFIG_READ")
public class PrChartListPage extends PrBasePage {

private static final long serialVersionUID = 1L;
public static final Logger LOG = LoggerFactory.getLogger(PrChartListPage.class);

@SpringBean
private IPrChartDSP chartDSPRealizer;


@SuppressWarnings("rawtypes")
private SingleSortState sortState = new SingleSortState();

private PrDataTable<PrChartVO, String> mChartDataTable;

private PrProjectVO project;


/**
 * Constructor
 */
public PrChartListPage(PageParameters params) {
    super(params);

    project=getActiveProject();


    // Deal with the ChartDataTable form
    Form<PrChartVO> lChartDataTableForm = new Form<PrChartVO>("chartDataTableForm");
    //      MetaDataRoleAuthorizationStrategy.authorize(lChartDataTableForm, RENDER, "USER");
    add(lChartDataTableForm);

    add(new Label("subTitle", new ResourceModel("chart.title", "Chart Configurator")));


    // Create and add the Chart list table to the page.
    addChartDataTable();

}

private void addChartDataTable() {
    List<IColumn<PrChartVO, String>> lColumns = new ArrayList<IColumn<PrChartVO, String>>();

    PrLinkPropertyColumn<PrChartVO> lChartNameColumn = new PrLinkPropertyColumn<PrChartVO>(
            new ResourceModel("chart.headerName"), "chart_name", "chart_name"){
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(Item<ICellPopulator<PrChartVO>> cellItem, String componentId,
                        IModel<PrChartVO> model, AjaxRequestTarget target) {
                    // TODO Auto-generated method stub
                    Object o = model.getObject();
                    if(o instanceof PrChartVO){
                        PrChartVO chart = (PrChartVO)o;
                        System.err.println("Chart: "+ chart.getChart_name()+" "+chart.getId());
                    }
                    else
                        System.err.println("Object is no chart!!! -> " + o.getClass().getName());

                }
    };
    lColumns.add(lChartNameColumn);


    PropertyColumn<PrChartVO, String> lChartTitleColumn = new PropertyColumn<PrChartVO, String>(new ResourceModel(
            "chart.headerTitle"), "title_text", "title_text");
    lColumns.add(lChartTitleColumn);

    PropertyColumn<PrChartVO, String> lChartTypeColumn = new PropertyColumn<PrChartVO, String>(new ResourceModel(
            "chart.headerChartType"), "chartTypeVO.type", "chartTypeVO.type");
    lColumns.add(lChartTypeColumn);

    PropertyColumn<PrChartVO, String> lXaxisTypeColumn = new PropertyColumn<PrChartVO, String>(
            new ResourceModel("chart.xaxisType"), "xAxisVO.name", "xAxisVO.name");
    lColumns.add(lXaxisTypeColumn);

    mChartDataTable = new PrDataTable<PrChartVO, String>("chartDataTable", lColumns, new ChartDataProvider(), PrConstants.pageSize, "100%");

    mChartDataTable.setOutputMarkupId(true);
    //      MetaDataRoleAuthorizationStrategy.authorize(mChartDataTable, RENDER, "USER");
    add(mChartDataTable);

}

@Override
protected void onBeforeRender() {

    super.onBeforeRender();
}

private final class ChartDataProvider implements ISortableDataProvider<PrChartVO, String> {

    private static final long serialVersionUID = 1L;

    @Override
    public Iterator<? extends PrChartVO> iterator(long first, long count) {

        if(project==null)
            System.err.println("Chart Iterator: Project is null");
        List<PrChartVO> lChartList = chartDSPRealizer.loadAllChartVOs(project);
        for (PrChartVO lChartVO : lChartList) {             
            if (lChartVO.getxAxisVO() != null && lChartVO.getxAxisVO().getName() != null) {
                String lXaxisName = new ResourceModel(lChartVO.getxAxisVO().getName()).getObject();                 
                lChartVO.getxAxisVO().setName(lXaxisName);
            }
        }
        return lChartList.subList((int)first, (int)(first + count)).iterator();
    }

    @Override
    public long size() {

        int lRet = 0;
        if(project!=null)
            lRet = chartDSPRealizer.getChartCount(project);
        return lRet;
    }

    @Override
    public IModel<PrChartVO> model(PrChartVO object) {
        return new Model<PrChartVO>(object);
    }

    @Override
    public void detach() {
        // Ignore           
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public ISortState getSortState() {
        return sortState;
    }
}

}

这是LinkPropertyColumn类

public abstract class PrLinkPropertyColumn<T> extends FilteredPropertyColumn<T, String> {

public PrLinkPropertyColumn(IModel<String> displayModel, IModel<T> labelModel) {
    super(displayModel, null);
}

public PrLinkPropertyColumn(final IModel<String> displayModel, final String sortProperty,
        final String propertyExpression) {
    super(displayModel, propertyExpression, sortProperty);
}

public PrLinkPropertyColumn(IModel<String> displayModel, String propertyExpressions) {
    super(displayModel, propertyExpressions);
}

@Override
public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId, IModel<T> rowModel) {
    cellItem.add(new LinkPanel(cellItem, componentId, rowModel));
}

/**
 * Override this method to react to link clicks. Your own/internal row id
 * will most likely be inside the model.
 */
public abstract void onClick(Item<ICellPopulator<T>> cellItem, String componentId, IModel<T> model,
        AjaxRequestTarget target);

public class LinkPanel extends Panel {

    public LinkPanel(final Item<ICellPopulator<T>> cellItem, final String componentId, final IModel<T> model) {
        super(componentId);

        AjaxLink<T> link = new AjaxLink<T>("link") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                System.out.println("This method is never invoked");
                PrLinkPropertyColumn.this.onClick(cellItem, componentId, model, target);
            }
        };
        link.add(new Label("label", getDataModel(model)));
        add(link);
    }
}

/*
 * (non-Javadoc)
 * 
 * @see org.apache.wicket.extensions.markup.html.repeater.data.table.filter.
 * IFilteredColumn#getFilter(java.lang.String,
 * org.apache.wicket.extensions.markup
 * .html.repeater.data.table.filter.FilterForm)
 */
@Override
public Component getFilter(String componentId, FilterForm<?> form) {
    return new TextFilter<T>(componentId, getFilterModel(form), form);
}

protected IModel<T> getFilterModel(final FilterForm<?> form) {
    return new PropertyModel<T>(form.getDefaultModel(), getPropertyExpression());
}
}

我只想提一下我对wicket相对较新(我们使用的是6.19)而且我必须继续一个现有的项目(我没有人再问了)。因此,可能存在一些我不知道的css或其他东西。如果这可能是我理解的原因,如果你能给我一个提示我必须搜索什么样的设置。

1 个答案:

答案 0 :(得分:0)

当我遇到类似问题时,发现父组件正在禁用链接。它们看起来像链接(蓝色文本),但根本没有响应滚动,点击等。