开发者

How to add onclick selection to rows of wicket TreeTable?

I'm working with a TreeTable (from wicket-extensions) and I'd like to be able 开发者_Go百科to select a row by clicking anywhere within it instead of the usual behavior of clicking the link in one cell to select the row. I understand this should be possible by adding an AjaxEventBehavior("onclick") to the component representing the row, but I can't seem to find any methods where the row component is exposed.


I figured out a solution after. The row element is available in the populateTreeItem method from TreeTable. When you're creating your treetable, override this method like so:

@Override
protected void populateTreeItem(final WebMarkupContainer item, final int level) {
    super.populateTreeItem(item, level);
    item.add(new AjaxEventBehavior("onclick") {
        @Override
        protected void onEvent(final AjaxRequestTarget target) {
            final TreeNode node = ((TreeNode) item.getDefaultModelObject());
            rowClickSelect(node);
        });
    }
};

Generally useful in adding behaviors to rows. In my case, I'll have to do some more overriding to reconcile this toggle-on-click behavior with the clicks that are supposed to expand/contract nodes as well as link clicks.

Just toggling selection again in these cases has the unfortunate effect of briefly toggling the node in and out of the unwanted state, which is not ideal. Instead, override the onJunctionLinkClicked and onNodeLinkClicked methods, which will be touched by a click event before it gets to the onClick behavior we just set-up in populateTreeItem:

@Override
protected void onJunctionLinkClicked(final AjaxRequestTarget target, final TreeNode node) {
    super.onJunctionLinkClicked(target, node);
    skipNextRowClick();
}

@Override
protected void onNodeLinkClicked(final AjaxRequestTarget target, final TreeNode node) {
    super.onNodeLinkClicked(target, node);
    skipNextRowClick();
}

Finally, add the methods skipNextRowClick and rowClickSelect:

/**
 * Ensure the next call to rowClickSelect() will have no effect.
 */
private void skipNextRowClick() {
    this.skipNextClickSelect = true;
}

private void rowClickSelect(final TreeNode node) {
    if (this.skipNextClickSelect) {
        this.skipNextClickSelect = false;
        return;
    }
    // select on click row
    final boolean isSelected = Log4jPanel.this.treeTable.getTreeState().isNodeSelected(node);
    treeTable.getTreeState().selectNode(node, !isSelected);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜