如何在jQuery中创建动态多级下拉列表

btc用户

我具有如下所述的动态json结构,需要动态填充html之类的树。可能有子级别,子级别,大级别....

[{"Key":"001","Record":{"PrefcatID":"001","parentid":"0","prefname":"org1"}},{"Key":"002","Record":{"PrefcatID":"002","parentid":"0","prefname":"org2"}},{"Key":"003","Record":{"PrefcatID":"003","parentid":"0","prefname":"org3"}},{"Key":"004","Record":{"PrefcatID":"004","parentid":"001","prefname":"suborg1"}},{"Key":"005","Record":{"PrefcatID":"005","parentid":"001","prefname":"suborg2"}},{"Key":"006","Record":{"PrefcatID":"006","parentid":"002","prefname":"suborg1"}},{"Key":"007","Record":{"PrefcatID":"007","parentid":"004","prefname":"subsuborg1"}}]


OrgID   OrgName        parentID
001    org1           0 -----th top
002    org2           0
003    org3           0
004    suborg1        001
005    suborg2       001
006    suborg1       002
007    subsuborg1    004 

像上面一样,需要创建任意数量的级别

我正在尝试使用ul li在屏幕上显示的代码,我需要在下拉列表中显示javascript代码:

    var menu = "<ul>";
    menu += fun_filldropdown(response, 0, menu);
    menu += "</ul>";
    $("#dropdown").html(menu);


function fun_filldropdown(response, parentid,menu)
{

    var menu = "";      
    var filtered = $.grep(response, function (el) {
        return el.Record.parentid == parentid.toString();
    });
    //alert(JSON.stringify(filtered));


    $.each(filtered, function(i, item) {


        if(item.Record.prefname !== undefined)
            {
            menu += "<li>"+item.Record.prefname+"</li>";    
            }

        if(response !== undefined)
        menu += "<ul>"+fun_filldropdown(response,item.Record.PrefcatID)+"</ul>";


    });


    return menu;

}

结果截图:

在此处输入图片说明

有人可以帮助我放入下拉菜单吗?

KNVB

您可以在这里详细了解。

它工作正常:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="http://www.dynamicdrive.com/dynamicindex1/uldropdown.js"></script>
        <link rel="stylesheet" href="http://www.dynamicdrive.com/dynamicindex1/uldropdown.css"/>
    </head>
    <body>
        <div id="dropdown" class="uldropdown">
        </div>
        <textarea id="output" style="width: 90%;height:100px;margin-top:1em"></textarea>
        <script>
            var response=[{"Key":"001","Record":{"PrefcatID":"001","parentid":"0","prefname":"org1"}},{"Key":"002","Record":{"PrefcatID":"002","parentid":"0","prefname":"org2"}},{"Key":"003","Record":{"PrefcatID":"003","parentid":"0","prefname":"org3"}},{"Key":"004","Record":{"PrefcatID":"004","parentid":"001","prefname":"suborg1"}},{"Key":"005","Record":{"PrefcatID":"005","parentid":"001","prefname":"suborg2"}},{"Key":"006","Record":{"PrefcatID":"006","parentid":"002","prefname":"suborg1"}},{"Key":"007","Record":{"PrefcatID":"007","parentid":"004","prefname":"subsuborg1"}}];
            var menu = "<div class=\"titletext\">Select a Value</div><ul>";
            menu += fun_filldropdown(response, 0, menu);
            menu += "</ul>";
            $("#dropdown").html(menu);
            dropdown1 = new uldropdown({
                dropid: 'dropdown', // id of menu DIV container
                overlay: true, // true = drop down, false = expanding menu
                onSelect($selected){ // when user selects a value
                    $('#output').val('Selected Text: ' + $selected.text() + '\n\n' + 'Selected Value: ' + parseInt($selected.attr('key')))
                    console.log($selected.text()+","+parseInt($selected.attr("key")))
                }
            });
            function fun_filldropdown(response, parentid,menu)
            {
                var menu = "";      
                var filtered = $.grep(response, function (el) {
                    return el.Record.parentid == parentid.toString();
                });
                //alert(JSON.stringify(filtered));


                $.each(filtered, function(i, item) {


                    if(item.Record.prefname !== undefined)
                        {
                        menu += "<li><a key=\""+item.Key+"\">"+item.Record.prefname+"</a></li>";    
                        }

                    if(response !== undefined)
                    menu += "<ul>"+fun_filldropdown(response,item.Record.PrefcatID)+"</ul>";


                });
                return menu;
            }
        </script>
    </body>
</html> 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Excel 中创建动态下拉列表

如何在angularjs中创建动态下拉列表

如何在Laravel中创建简单的动态下拉列表?

如何在Codeigniter中创建动态链式下拉列表

如何在PHP中创建动态下拉列表?

如何在Laravel Blade中创建动态生成的下拉列表

如何动态填充jQuery中的下拉列表?

如何在MS Word中跨表创建多级列表?

多级动态列表jQuery

如何在React中使用Formik创建动态下拉列表?

如何在带有文字和角度的动态下拉列表中创建“子菜单”?

如何在同一表的Codeigniter中创建动态/链接下拉列表

从多级动态集合中添加下拉列表

如何在Python中创建动态列表?

创建动态JQuery以禁用下拉列表

jQuery&KendoUI下拉列表动态创建

如何在 PHP 的动态下拉列表中显示选定的值

如何在Angular FormArray中动态绑定ngSelect下拉列表

如何在动态填充的下拉列表中使用jQuery设置下拉列表的选定文本或值

如何在AngularJS中删除动态创建的下拉选项?

如何在 Angular 8 中创建动态下拉菜单

如何使用jQuery动态隐藏下拉列表中的某些选项

如何在Golang中创建多级地图

如何在 Python 中创建多级 JSON

如何在动态添加的表行中创建级联下拉列表并保存在数据库中

如何从嵌套对象创建动态下拉列表

如何获得动态创建的下拉列表的价值?

您如何在 Google Sheets App Script 中创建第二个动态依赖下拉列表?

如何在Django表单中创建从属下拉列表?