JSF + Dynatree 制作树表示

发布于 2024-12-02 05:38:00 字数 190 浏览 0 评论 0原文

我想画一棵在 JSF 中生成的树。

我认为我应该使用 DynaTree 并避免使用组件库,尽管看到解决方案的复杂性我开始重新考虑。

我想我可以使用 Javascript 的隐藏输入传递树的字符串表示形式,然后在那里构建树。

有没有我没有想到的更好的解决方案?

使用JSF2.0

I'd like to draw a tree that is generated in JSF.

I thought I'd use DynaTree and avoid using component libraries, although i'm starting to have second thoughts seeing the complexity of the solution.

I thought I could pass a String representation of the tree using a hidden input to Javascript and then build the tree there.

Is there a better solution that I had not thought of?

Using JSF2.0

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

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

发布评论

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

评论(2

人生百味 2024-12-09 05:38:00

我最终所做的是生成一个表示树的 JSON 字符串(以 dynatree 格式),然后在另一端(浏览器)简单地使用 JSON.Parse() 来生成一棵树。

相关代码是:
Java 树节点:

class GroupTreeNode {
   public getNodes();
   public getGroupId();
   public getName();
}

生成树字符串:

public String generateTreeString()
{
    GroupTreeNode[] root = getGroupTreeBean()
            .getGroupsTreeRoot("groupTree");

    StringBuilder sb = new StringBuilder();

    sb.append("[");

    for (int i = 0; i < root.length; i++)
    {
        genSubTree(root[i], sb);
    }

    sb.append("]");

    return sb.toString();
}

private void genSubTree(GroupTreeNode node, StringBuilder sb)
{
    // Check if the last character is a '}' in which case we need to add a comma
    char[] chararray = new char[1];
    sb.getChars(sb.length()-1, sb.length(), chararray, 0);
    if (chararray[0] == '}') {
        sb.append(", ");
    }

    // Carry on...
    sb.append("{");

    // Group Name
    sb.append("\"title\":\"");
    sb.append(node.getName());
    sb.append("\", ");

    // Group ID (Custom Node Tab)
    sb.append("\"groupID\":\"");
    sb.append(node.getGroupId());
    sb.append("\"");

    // Children (Only if applicable)
    if (node.getNodes().length > 0) {
        sb.append(", \"isFolder\":true, \"children\":[");
        for (int i =0; i < node.getNodes().length; i++)
        {
            genSubTree(node.getNodes()[i], sb);             
        }

        sb.append("]");
    }

    sb.append("}");
}

该字符串通过 JSF 传递到 ,然后是 JavaScript 解析:

    function buildTree(sTree) {
        var builtobj = JSON.parse(sTree);
        return builtobj;
    }


    var jsfString = $("#tree").val();
    console.log("Building a tree with:" + jsfString);

    $("#fleet_tab_tree").dynatree({
        onActivate : function (node) {
            console.log("You activated " + node.data.title + ", with groupID=" + node.data.groupID);
            // Do whatever you want with the node values
        },
        persist : true,
        children : buildTree(jsfString),
        clickFolderMode: 1
    });

What I eventually did is to generate a JSON string that represents the tree (in dynatree format) and then simply use JSON.Parse() on the other side (browser) to make a tree out of it.

The relevant code is:
The Java tree node:

class GroupTreeNode {
   public getNodes();
   public getGroupId();
   public getName();
}

Generate the Tree String:

public String generateTreeString()
{
    GroupTreeNode[] root = getGroupTreeBean()
            .getGroupsTreeRoot("groupTree");

    StringBuilder sb = new StringBuilder();

    sb.append("[");

    for (int i = 0; i < root.length; i++)
    {
        genSubTree(root[i], sb);
    }

    sb.append("]");

    return sb.toString();
}

private void genSubTree(GroupTreeNode node, StringBuilder sb)
{
    // Check if the last character is a '}' in which case we need to add a comma
    char[] chararray = new char[1];
    sb.getChars(sb.length()-1, sb.length(), chararray, 0);
    if (chararray[0] == '}') {
        sb.append(", ");
    }

    // Carry on...
    sb.append("{");

    // Group Name
    sb.append("\"title\":\"");
    sb.append(node.getName());
    sb.append("\", ");

    // Group ID (Custom Node Tab)
    sb.append("\"groupID\":\"");
    sb.append(node.getGroupId());
    sb.append("\"");

    // Children (Only if applicable)
    if (node.getNodes().length > 0) {
        sb.append(", \"isFolder\":true, \"children\":[");
        for (int i =0; i < node.getNodes().length; i++)
        {
            genSubTree(node.getNodes()[i], sb);             
        }

        sb.append("]");
    }

    sb.append("}");
}

The String is passed through JSF into an <h:inputText id="tree" style="display:none" /> and then the JavaScript parsing:

    function buildTree(sTree) {
        var builtobj = JSON.parse(sTree);
        return builtobj;
    }


    var jsfString = $("#tree").val();
    console.log("Building a tree with:" + jsfString);

    $("#fleet_tab_tree").dynatree({
        onActivate : function (node) {
            console.log("You activated " + node.data.title + ", with groupID=" + node.data.groupID);
            // Do whatever you want with the node values
        },
        persist : true,
        children : buildTree(jsfString),
        clickFolderMode: 1
    });
╰沐子 2024-12-09 05:38:00

我同意你的观点,dynatree 是一个强大且经过验证的组件。相反,一些 JSF 原生树组件的功能较少。

我已经启动了 dynatree 的适配器。你可以在这里找到源代码 https://github.com/nithril/dynatree-for-jsf

欢迎评论!

I agree with you than dynatree is a robust and a proven component. On the contrary of some JSF native tree components that have less features.

I have start an adapter for dynatree. You could find the source here https://github.com/nithril/dynatree-for-jsf

Comments are welcome!

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