从 Javascript 访问 richfaces 树的选定节点

发布于 2024-09-01 17:36:26 字数 882 浏览 2 评论 0原文

这应该是一个非常简单的问题。我有一个使用 JSF 渲染的 richfaces 树。当用户单击节点时,我希望运行 javascript 函数。不多不少。没有重定向、没有重新提交、没有重新渲染、没有 Ajax。只是普通的旧 JavaScript。

我已经看到树的 onselected 属性,它确实触发了 Javascript 方法。但我当然想知道单击了哪个节点。

这是我到目前为止所拥有的,

<head>
<script type="text/javascript">
function documentClicked(nodeRef)
{
    alert("Node is "+nodeRef);
}

</script>
</head>


    <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" onselected="documentClicked()" >



        <rich:treeNode   iconLeaf="../images/tree/doc.gif"
            icon="../images/tree/doc.gif">

            <h:outputText value="#{document.friendlyName}" />

        </rich:treeNode>

但这不起作用,因为 nodeRef 未定义。我预计回调的第一个参数将是选定的节点,但事实并非如此。

所以问题是:

如何使用 richfaces 树中选定的节点触发 Javascript 函数?

This should be a very simple question. I have a richfaces tree that is rendered using JSF. When the user clicks on a node I want a javascript function to run. Nothing more nothing less. No redirects, no re-submit, no-rerender, no Ajax. Just plain old Javascript.

I have seen the onselected attribute of the tree and it indeed fires a Javascript method. But of course I want to know which node was clicked.

Here is what I have so far

<head>
<script type="text/javascript">
function documentClicked(nodeRef)
{
    alert("Node is "+nodeRef);
}

</script>
</head>


    <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" onselected="documentClicked()" >



        <rich:treeNode   iconLeaf="../images/tree/doc.gif"
            icon="../images/tree/doc.gif">

            <h:outputText value="#{document.friendlyName}" />

        </rich:treeNode>

But this does not work because nodeRef is undefined. I expected that the first argument of the callback would be the selected node but this is not the case.

So the question is this:

How do I fire a Javascript function with the selected node from a richfaces tree?

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

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

发布评论

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

评论(2

素染倾城色 2024-09-08 17:36:26

答案是javascript代码应该在节点级别而不是树级别。

<head>
<script type="text/javascript">
function documentClicked(nodeRef)
{
    alert("Node id is "+nodeRef);
}

</script>
</head>


    <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" >

        <rich:treeNode onclick="documentClicked('#{document.id}')">

            <h:outputText value="#{document.friendlyName}" />

        </rich:treeNode>

The answer is that the javascript code should be on the node level instead of the tree level.

<head>
<script type="text/javascript">
function documentClicked(nodeRef)
{
    alert("Node id is "+nodeRef);
}

</script>
</head>


    <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" >

        <rich:treeNode onclick="documentClicked('#{document.id}')">

            <h:outputText value="#{document.friendlyName}" />

        </rich:treeNode>
蝶舞 2024-09-08 17:36:26

我对 JSF 的经验很少,但是 this(在事件处理程序的范围内)不应该引用所选节点,就像 JavaScript 中通常所做的那样?

更改你的功能并尝试一下:

function documentClicked()
{
    alert("Node is " + this);
}


编辑:以上显然是不正确的。根据我下面的评论,将您的 JSF 更改为

<rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" onselected="documentClicked(this)" >

I have little experience with JSF, but shouldn't this (in the scope of your event handler) be referring to the selected node, as it usually does in JavaScript?

Change your function to this and try it out:

function documentClicked()
{
    alert("Node is " + this);
}


EDIT: the above was obviously not correct. As per my comment below, change your JSF to

<rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" onselected="documentClicked(this)" >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文