将JSON文件数据填充到Array中,然后将Array提供到mmenu插件中

时间:2016-03-22 16:54:37

标签: javascript jquery html json mmenu

我正在尝试将JSON文件(menu.json)读取到数组(myList),以便运行一个函数(PopulateRecords),该函数将使用HTML行填充我的jQuery菜单插件。理想情况下,我只需稍后更新JSON文件即可动态更改菜单选项。

我的JSON文件是menu.json:

{"pavers":
[
    {"display": "Brukstone", "url": "brukstone.html"},
    {"display": "Bulovar", "url": "pavers/bulovar.html"},
    {"display": "Cobble", "url": "pavers/cobble.html"},
    {"display": "Cracovia", "url": "pavers/cracovia.html"},
    {"display": "Filtrapave", "url": "pavers/filtrapave.html"},
    {"display": "Holland", "url": "pavers/holland.html"},
    {"display": "Old Munich", "url": "pavers/oldmunich.html"},
    {"display": "Strassen Classic", "url": "pavers/strassen.html"},
    {"display": "Strassen Bavaria (Tumbled)", "url": "pavers/strassenbavaria.html"},
    {"display": "Strassen Barvaria II (Non-tumbled)", "url": "pavers/strassenbavariaii.html"},
    {"display": "Vavel Stone (Tumbled)", "url": "pavers/vavel.html"},
    {"display": "Vavel Stone II (Non-tumbled)", "url": "pavers/vavelii.html"}
]}

我的HTML

<!DOCTYPE html>
<html>
    <head>
                <!--         -->
                <!-- Sources -->
                <!--         -->
        <link type="text/css" rel="stylesheet" href="css/demo.css" />
        <link type="text/css" rel="stylesheet" href="../dist/css/jquery.mmenu.all.css" />

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript" src="../dist/js/jquery.mmenu.all.min.js"></script>

        <script src="https://cdn.vaadin.com/vaadin-core-elements/latest/webcomponentsjs/webcomponents-lite.min.js"></script>
      <link rel="import"
            href="https://cdn.vaadin.com/vaadin-core-elements/master/vaadin-combo-box/vaadin-combo-box.html">

           <!--                                                  -->
           <!-- Attach function.js which contains PopulateRecords-->
           <!--                                                  -->
        <script type="text/javascript" src="functions.js"></script>

           <!--SETUP for jQuery mmenu plugin-->
        <script type="text/javascript">
            $(function() {
                $('nav#menu').mmenu();
            });
        </script>
    </head>

    <body>
        <nav id="menu">
            <ul>
                <li><a href="#id01">Pavers</a>
                    <div id="id01"></div>
                </li>
            </ul>
        </nav>


        <script type="text/javascript">
            // CALL JSON DATA
            var myList;
            $.getJSON('menu.json')
                .done(function (data) {
                myList = data;
            });

            // POPULATE MENU ITEMS FROM ARRAY
            PopulateRecords("01",myList);
        </script>
    </body>
</html>

调用的PopulateRecords函数位于html头中附加的单独的functions.js文件中。

该功能的javascript是:

function PopulateRecords(id, arr) {
    var out = "<ul>";
    var i;
    for(i = 0; i<arr.length; i++) {
        out += '<li><a href="' + arr[i].url + '">' + arr[i].display + '</a></li>';
    }
    out += "</ul>";
    document.getElementById("id"+id).innerHTML = out;
}

它应该如何工作: PopulateRecords函数(在HTML中的<script>标记中调用)应该传递d​​iv元素的id号,该元素是nav中的占位符(id01)菜单HTML。它还传递一个数组,该数组从menu.json文件(在HTML中的<script>标记中也称为)中填充。然后它会按照jQuery mmenu所需的正确格式(列出项目和href)注入html。

注意:我已成功测试了具有声明的javascript数组的PopulateRecords函数。所以,只要传递一个值为&#39; url&#39;和&#39;显示&#39;。

当然,因为我不熟悉这个概念,这让我相信问题在于我无法解析JSON文件。在关注这个问题之后,我转向堆栈溢出社区寻求帮助。

编辑:正在进行的调试笔记

1)以下是更新的JSON调用。通过将PopulateRecords调用移动到&#39; .done&#39;结果会停止“myList not defined error&#39;”,但仍然不会填充表格( img1 )。

var myList;
        $.getJSON('menu.json')
        .done(function (data) {
        myList = data;
        PopulateRecords("01",myList);
        console.log(myList.pavers);
        console.log(myList);
        console.log(data);
        });

img1 IMG1

2)我还在&#39; .done&#39;中添加了几个console.log调用。用于调试。调用页面时,日志不返回任何内容。

3)调用页面后,在控制台行中对页面使用相同的调用,结果如下( img2 )。 img2 IMG2

1 个答案:

答案 0 :(得分:1)

在调用函数之前,您可能需要等待DOM加载。尝试做

var myList;
$.getJSON('menu.json')
    .done(function (data) {
        myList = data;
        PopulateRecords("01",myList);
        console.log(myList.pavers);
        console.log(myList);
        console.log(data);
    });

$(document).ready(function(){ ... });

看起来像是:

$(document).ready(function(){
    var myList;
    $.getJSON('menu.json')
        .done(function (data) {
            myList = data;
            PopulateRecords("01",myList);
            $('nav#menu').mmenu();
            console.log(myList.pavers);
            console.log(myList);
            console.log(data);
    });
})

仅供我参考

html:

<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<div>
<label id="menuTitle"></label>
<ul id="menu">

</ul>
</div>
<script>
$(document).ready(function(){
    $.getJSON('menu.json').done(function(data){
            $('#menuTitle').html(data.name);
            var lis = ''
            for(var i = 0; i < data.options.length; i++)
                lis += '<li><a href="'+ data.options[i].url+'">' + data.options[i].name + "</a></li>";
            $('#menu').html(lis);
        });
});
</script>
</body>

JSON:

{ 
    "name": "aMenu",
    "options": [
        {
            "name": "option 1", 
            "url": "#"
        },
        {
            "name": "option 2", 
            "url": "#"
        },
        {
            "name": "option 3", 
            "url": "#"
        },
        {
            "name": "option 4", 
            "url": "#"
        }
    ]
}