dougou5844 2016-05-26 15:28
浏览 63
已采纳

带有JSON的DataTable数据集

I'm trying to use DataTable to show all fields of my JSON, but I'm not understand how to use it.

I just need how to populate dataset correctly to read data.

<script>
        <?php
            var jqxhr = $.ajax({url: api_ricerca_ingredienti, type: "GET",dataType: "json", data: {all: 1, ln : "it",completo:"-1",conteggio: 1}} )
              var max=json.items.length;
              for (i=0;i<max;i++){
                var el=json.items[i];
              }
              $(".risultato_ricerca").click(function() {
                carica_ingrediente($(this).attr('data-id'));
              });
            })
            <?php }?>

            var dataSet = [
              /*HOW?*/
            ];

            $(document).ready(function() {
              $('#example').DataTable( {
                data: dataSet,
                columns: [
                  { title: "Nome" },
                  { title: "Stato" },
                  { title: "Home" },
                  { title: "Utente" },
                  { title: "Mi piace" },
                  { title: "Contributi" }
                ]
              } );
            } );
            </script>
            <table id="example" class="table table-responsive table-hover table-dynamic filter-head"></table>
  • 写回答

2条回答 默认 最新

  • dongmeng1875 2016-05-26 16:06
    关注

    First, two comments about your code. As @Patrick Q stated, your PHP tags are useless: you are writing Javascript, PHP is another things that has nothing to do with your code, so remove <?php and <?php }?>

    Then, i can't understand this code:

    for (i=0;i<max;i++){
       var el=json.items[i];
    }
    

    This for is completely useless. you cycle through every element in json.items without performing any operation. At the end you just store the last json.items in the el (without even using el in your code).

    By the way, your main question: As said in the official documentation, your dataSet must contain an array for every row of your table in the format:

    var dataSet = [
        ["john","italy","home","john",15,20],
        ["john","italy","home","john",15,20]
    ]
    

    This example will create two identical rows with random data inside.

    Assuming every item in your json has name country home user likes contribs fields you will need something like that:

    var dataSet = []; 
    for (i=0;i<json.items.length;i++){
         var el = [json.items[i].name,json.items[i].country, json.items[i].home, json.items[i].user, json.items[i].likes, json.items[i].contribs]
         dataSet[i] = el;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?