如果函数与调用者不在同一文档中,则将 JStree 包裹在函数中并传递变量将不起作用

发布于 2024-12-10 09:00:56 字数 1162 浏览 12 评论 0原文

如果我的 JStree 初始化函数与调用者位于同一文件中,则一切正常,但如果我将函数调用移动到 html 文档,它不会返回任何内容。这是我现在想到的代码:

function cattree(treeID, treeAncestors, treeCurrent) {
        alert(treeID + treeAncestors + treeCurrent);
        $("#"+treeID).jstree({ 
            "core" : { 
                "initially_load" : treeAncestors,
            },

            "ui" :{
                "select_limit" : 1,
                "initially_select" : [treeCurrent],
            },
            "json_data" : {
                "progressive_render" : true,
                "progressive_unload" : true,
                "ajax" : {
                    "url" : "/taxonomy/catjson",
                    "dataType": "text json",
                    "data" : function (n) { 
                        return { id : n.attr ? n.attr("id") : 0 }; 
                    }
                }
            },
            "plugins" : [ "themes", "json_data", "ui" ],
        });
}

当我将其移动到文档时,我认为它不会等待具有功能的文档加载。

   $(document).ready(function(){
        cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
   }
   );

If my JStree initialisation function is in the same file as caller everything works, but if I move function call to html document it does not return anything. Here is the code I came up up to now:

function cattree(treeID, treeAncestors, treeCurrent) {
        alert(treeID + treeAncestors + treeCurrent);
        $("#"+treeID).jstree({ 
            "core" : { 
                "initially_load" : treeAncestors,
            },

            "ui" :{
                "select_limit" : 1,
                "initially_select" : [treeCurrent],
            },
            "json_data" : {
                "progressive_render" : true,
                "progressive_unload" : true,
                "ajax" : {
                    "url" : "/taxonomy/catjson",
                    "dataType": "text json",
                    "data" : function (n) { 
                        return { id : n.attr ? n.attr("id") : 0 }; 
                    }
                }
            },
            "plugins" : [ "themes", "json_data", "ui" ],
        });
}

When I move it to document I think it does not wait for document with function to load.

   $(document).ready(function(){
        cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
   }
   );

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

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

发布评论

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

评论(1

木緿 2024-12-17 09:00:56

这是想出来的:

外部文件中的 cattree init

(function () {
    if(jQuery && jQuery.jstree && jQuery.cattree) { return; }
    (function ($) {
        $.cattree = function(treeID, treeAncestors, treeCurrent) {
            //alert(treeID + treeAncestors + treeCurrent);
            //tree initialization function to possibly allow one or more tree on page.
            $("#"+treeID).jstree({ 
                "core" : { 
                    "initially_load" : treeAncestors,
                },

                "ui" :{
                    "select_limit" : 1,
                    "initially_select" : treeCurrent,
                },
                "json_data" : {
                    "progressive_render" : true,
                    "progressive_unload" : true,
                    "ajax" : {
                        "url" : "/taxonomy/catjson",
                        "dataType": "text json",
                        "data" : function (n) { 
                            return { id : n.attr ? n.attr("id") : 0 }; 
                        }
                    }
                },
                "plugins" : [ "themes", "json_data", "ui" ],
            });

        }
    })(jQuery);
})();

以及 head 部分中的代码或我认为位于所有 html 之下:

$(document).ready(
    function(){
    $.cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
    }
);

目前它工作得很好。我认为问题在于该函数在加载所有内容之前被调用。与之前将 Jquery.cattree 添加到 if 语句一样,它无法将 $.cattree 识别为函数。

Here is what came up with:

cattree init in external file

(function () {
    if(jQuery && jQuery.jstree && jQuery.cattree) { return; }
    (function ($) {
        $.cattree = function(treeID, treeAncestors, treeCurrent) {
            //alert(treeID + treeAncestors + treeCurrent);
            //tree initialization function to possibly allow one or more tree on page.
            $("#"+treeID).jstree({ 
                "core" : { 
                    "initially_load" : treeAncestors,
                },

                "ui" :{
                    "select_limit" : 1,
                    "initially_select" : treeCurrent,
                },
                "json_data" : {
                    "progressive_render" : true,
                    "progressive_unload" : true,
                    "ajax" : {
                        "url" : "/taxonomy/catjson",
                        "dataType": "text json",
                        "data" : function (n) { 
                            return { id : n.attr ? n.attr("id") : 0 }; 
                        }
                    }
                },
                "plugins" : [ "themes", "json_data", "ui" ],
            });

        }
    })(jQuery);
})();

And the code in head section or I think below all html:

$(document).ready(
    function(){
    $.cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
    }
);

At the moment it works quite nicely. And I think the issue was that the function was called before everything is loaded. As before adding Jquery.cattree to if statement it was failing to recognize $.cattree as a function.

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