如何将 onclick 选择添加到 wicket TreeTable 的行?

发布于 2024-11-04 23:11:16 字数 161 浏览 3 评论 0原文

我正在使用 TreeTable(来自 wicket-extensions),我希望能够通过单击行中的任意位置来选择行,而不是单击一个单元格中的链接来选择行的通常行为。我知道这应该可以通过向表示行的组件添加 AjaxEventBehavior("onclick") 来实现,但我似乎找不到公开行组件的任何方法。

I'm working with a TreeTable (from wicket-extensions) and I'd like to be able 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-11-11 23:11:16

我后来想出了一个解决办法。 row 元素可在 TreeTablepopulateTreeItem 方法中使用。当您创建树表时,请像这样重写此方法:

@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);
        });
    }
};

通常在向行添加行为时有用。就我而言,我必须做更多的重写才能使这种单击切换行为与应该扩展/收缩节点的单击以及链接单击相协调。

在这些情况下,仅再次切换选择会产生不幸的效果,即短暂地将节点切换到不需要的状态,这是不理想的。相反,请重写 onJunctionLinkClickedonNodeLinkClicked 方法,这些方法将在点击事件到达我们刚刚设置的 onClick 行为之前被触及在 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();
}

最后,添加方法 skipNextRowClickrowClickSelect

/**
 * 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文