代码之家  ›  专栏  ›  技术社区  ›  jsPlayer

jquery数据表错误?

  •  0
  • jsPlayer  · 技术社区  · 6 年前

    http://datatables.net/tn/1 “。我在网上试过的解决方案对我来说并不适用。所以基本上我测试的是一个本地JSON文件,并使用数据表Ajax方法填充该表。我似乎不知道我做错了什么。

    $(document).ready(function () {
        var request = new XMLHttpRequest();
        request.open("GET", "./test.json", false);
        request.send(null)
        var responseMain = JSON.parse(request.responseText);
        var my_array = responseMain.feed.entry;
        $("#totalnum").html(my_array.length);
    
        var obj_stage = [];
        $.each(my_array, function (index, value) {
            
            obj_stage.push(value.content.F_Form1);
            console.log("inside each", obj_stage)
        });
    
    
        console.log("out side each", obj_stage)
        console.log("what type",typeof obj_stage);
    
    
        $('#myTable').DataTable({
            "ajax": obj_stage,
            "columns": [
                { "data": obj_stage["-flowState"] },
                { "data": obj_stage["-flowState"] },
            ]
        });
    });
    
    //console.log("value",value.content.F_Form1["-flowState"])
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.18/css/jquery.dataTables.min.css">
        <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>
        <script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
        <script src="./test.js"></script>
    </head>
    
    <body>
        <h1 id="totalnum"></h1>
        <table id="myTable" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Stage Name</th>
                    <th>Asap ?</th>
                </tr>
            </thead>
        </table>
    </body>
    
    </html>
    1 回复  |  直到 6 年前
        1
  •  1
  •   Sachi Tekina    6 年前

    您可以直接调用该对象,因为它已经加载到 obj_stage

    { "data": obj_stage["-flowState"] }, { "data": "-flowState" }, 并且只能通过使用数组的索引来访问数组

    $(document).ready(function () {
        var request = new XMLHttpRequest();
        request.open("GET", "./test.json", false);
        request.send(null)
        var responseMain = JSON.parse(request.responseText);
        var my_array = responseMain.feed.entry;
        $("#totalnum").html(my_array.length);
    
        var obj_stage = [];
        $.each(my_array, function (index, value) {
            obj_stage.push(value.content.F_Form1);
        });
        //if(my_array.length === obj_stage.length ){
            $('#myTable').DataTable({
                "data": obj_stage,
                "columns": [
                    { "data": "-flowState" },
                    { "data": "-flowState" },
                ]
            });
        //}
    });