【问题标题】:Datatables using JSON response使用 JSON 响应的数据表
【发布时间】:2011-10-11 13:07:21
【问题描述】:

我正在尝试将来自 Google 购物的 JSON 响应加载到使用 JQuery 插件 DataTables 格式化的 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)
   }
   });

   }
   });
   });
   });

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

<!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>

【问题讨论】:

  • 已修复。我忘了删除强制表格显示为空的空方法

标签: javascript jquery html json


【解决方案1】:

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

有一个example here

【讨论】:

  • 拉里,谢谢。我正在从 Google 购物 API 中提取数据,那么该方法应该放在哪里?
  • 您应该进行以下更改:(1)您不需要将数据格式化为表格行(dataTable会为您完成),因此删除所有代码并(2)替换调用$('#example').append(dataTable).hide().fadeIn('slow'); 并调用$('#example').dataTable.fnAddData(),参数如我链接的示例所示。
  • 拉里,谢谢。我已经编辑了我的代码,但现在我收到了这个错误: Uncaught TypeError: Cannot read property 'asSorting' of undefined $.fn.dataTablejquery.dataTables.js:7365 c.extend.eachjq​​uery.min.js:30 c.fn .c.eachjq​​uery.min.js:24 $.fn.dataTablejquery.dataTables.js:6923(匿名函数)google-example.php:90 c.extend.eachjq​​uery.min.js:30 $.ajax.successgoogle-example .php:75 bjquery.min.js:124 c.extend.ajax.A.(匿名函数)jquery.min.js:125(匿名函数)
  • 您在文档准备功能中正确构建了表格吗?您需要一个带有标题部分的基表来接收您发布的数据,并且必须像之前调用 .dataTable() 一样“初始化”它。
  • Larry,我已经更新了我的代码,请看一下。非常感谢您的帮助。现在数据根本不显示,但正在加载到表格中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2016-09-06
  • 2017-09-23
  • 2021-11-09
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多