javascript - 使用 JSON 响应的数据表

标签 javascript jquery html json

我正在尝试使用 JQuery 插件 DataTables 将来自 Google 购物的 JSON 响应加载到 html 格式的表格中。

我正在将数据附加到表格 div,但它似乎不起作用。

    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Currency</th>
                <th>Price</th>
                <th>Shipping</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
        <tfoot>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Currency</th>
                <th>Price</th>
                <th>Shipping</th>
            </tr>
        </tfoot>
    </table>

    <script>

        var apiKey = "key";
        var country = "US";
        var apiurl = "https://www.googleapis.com/shopping/search/v1/public/products?callback=?";


        $(document).ready(function() { 

          $('#example').dataTable();

            $('.button').click(function (e) {
            $('#example').empty();
        e.preventDefault();

        $.ajax({
        type: "GET",
        url: apiurl,
        dataType: 'jsonp',
            data : 
        {
           key: apiKey, 
               country: country, 
               q: $('[name=myanswer]').val()    
            },
            success: function(data) {
            $.each(data.items, function(i, item){
            if (item.product.images.length > 0) // sanity check
            {
                //global variables
                var link = item.product.images[0]['link'];
                var title = item.product.title;
                var gtin = item.product.gtins[0];

                //price
                var currency = item.product.inventories[0]['currency'];
                var price = item.product.inventories[0]['price'];
                var shipping = item.product.inventories[0]['shipping'];

                var listData = "<li>" + title + gtin + price + currency + shipping + "</li>" + '<img title="' + title + '" src="' + link + '" />';

                var dataTable =
                "<tr>" +
                    "<td>" + '<img title="' + title + '" src="' + link + '" />' + "</td>" +
                    "<td>" + gtin + "</td>" +
                    "<td>" + title + "</td>" +
                    "<td>" + currency + "</td>" +
                    "<td>" + price + "</td>" +
                    "<td>" + shipping + "</td>" +
                    "</tr>";

                        $('#example').append(dataTable).hide().fadeIn('slow');
                        console.log(data)
   }
   });

   }
   });
   });
   });

更新:在 Larry 的帮助下,我设法将数据加载到表中。我知道这是因为底部的数字已填充。但是,数据根本没有显示。

<!DOCTYPE html>
<html>
<head>
  <style>
  #images { padding:0; margin:0; overflow: hidden;}
  #images img { width:200px; height:200px; border:none;}
  td {
  padding-top: 2px;
  padding-bottom: 2px;
  padding-right: 20px;
}
  #example img { width:50px; height: 50px; }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script src="jquery.dataTables.js"></script>
</head>
<body>

<form id="myform">
   <input type="text" name="myanswer" value="test">
   <input type='submit' class="button" name="submitButton" value='submit'>
</form>

<table id="example">
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>etc</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1 Data 1</td>
            <td>Row 1 Data 2</td>
            <td>etc</td>
        </tr>
        <tr>
            <td>Row 2 Data 1</td>
            <td>Row 2 Data 2</td>
            <td>etc</td>
        </tr>
    </tbody>
</table>

<script>

    var apiKey = "key";
    var country = "US";
    var apiurl = "https://www.googleapis.com/shopping/search/v1/public/products?callback=?";


    $(document).ready(function() { 

    $('#example').dataTable();
            $('.button').click(function (e) {

            $('#example').empty();

                e.preventDefault();

                    $.ajax({
                    type: "GET",
                    url: apiurl,
                    dataType: 'jsonp',
                    data : 
                    {
                        key: apiKey, 
                        country: country, 
                        q: $('[name=myanswer]').val()   

                        },
                    success: function(data) {

                         $.each(data.items, function(i, item){

                            if (item.product.images.length > 0) // sanity check
                            {

                            //global variables
                            var link = item.product.images[0]['link'];
                            var title = item.product.title;
                            var gtin = item.product.gtins[0];

                            //price
                            var currency = item.product.inventories[0]['currency'];
                            var price = item.product.inventories[0]['price'];
                            var shipping = item.product.inventories[0]['shipping'];

                            $('#example').dataTable().fnAddData( [
                            title,
                            gtin,
                            price
                            ]);

                            }
                        });

     }
   });
  });



});


</script>
</body>
</html>

最佳答案

假设您的 AJAX 调用正在运行并返回数据,将行附加到 jQuery dataTable 的正确方法不是尝试编辑底层 HTML,而是让 dataTable 通过 dataTable API 调用 fnAddData() 添加行.

有一个example here .

关于javascript - 使用 JSON 响应的数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726507/

相关文章:

javascript - 使用 createDocumentFragment 插入嵌套的 div 结构

javascript - 如何解决悬停时图像链接的问题?

jquery - 复制 + 粘贴事件在 jQuery/Firefox 中不起作用

html - "destination-out"- 类似使用 CSS 的混合行为?

html - 为什么我的相对定位元素顶部/底部不工作/跳到错误的位置?

javascript - 以圆形方式放置瓷砖

javascript - 在谷歌地图上添加自定义 svg 层(api v3)

jquery - 页面重新加载时的 ScrollTop 不起作用(可能是脚本冲突)

javascript - Jquery 更新至 3.1.1 -> 匿名函数

javascript - 为什么 getBoundingClientRect() 将所有值返回为零?