簡體   English   中英

JSON對象數組到數據表

[英]Json object array to datatable

我有一個php文件(ads.php),該文件返回由記錄集組成的json對象數組

數組是

[{"hea":{"0":"Kidney Stone Removal"},"id":{"0":"16282238572"},"desc":{"0":"Get treated at top kidney center"},"desc2":{"0":"Take a free advice from our experts"},"url":{"0":"www.ainuindia.com"},"cli":{"0":"1"},"cpc":{"0":"0"},"con":{"0":"0"},"cost":null,"ctr":{"0":"5.26%"},"imp":{"0":"19"},"ap":{"0":"2.2"}}]

而我的Java Srcript是

$("#example1").dataTable();
            $("#groupid").change(function(){
            var oTable = $('#example1').dataTable();
            var grpvalue=$('#groupid').val();
                            $.ajax({
                            type:"post", 
                            dataType : 'json',
                            url:"pages/ads.php", 
                            data:"adgroup="+grpvalue, 
                            success: function(s) {
                            oTable.fnClearTable();
                            for(var i = 0; i < s.length; i++) {
                                oTable.fnAddData([          
                                    s[i]['hea'],
                                    s[i]['id'],
                                    s[i]['desc'],
                                    s[i]['desc2'],
                                    s[i]['url'],
                                    s[i]['cli'],
                                    s[i]['cpc'],
                                    s[i]['con'],
                                    s[i]['cost'],
                                    s[i]['ctr'],
                                    s[i]['imp'],
                                    s[i]['ap']

                                    ]);
                                    }
                                    }
                        });


            });

和HTML數據表是

<table id="example1" class="table table-bordered table-striped num-right-alignct">
                                        <thead>

                                            <tr>
                                                <th  style="text-align: center;">Ad Headline</th>
                                                <th  style="text-align: center;">Ad ID</th>
                                                <th  style="text-align: center;">Ad Description 1</th>
                                                <th  style="text-align: center;">Ad Description 2</th>
                                                <th  style="text-align: center;">URL Appeared</th>
                                                <th  style="text-align: center;">Clicks</th>
                                                <th  style="text-align: center;">CPC</th>
                                                <th  style="text-align: center;">Conversions</th>
                                                <th  style="text-align: center;">CTR %</th>
                                                <th  style="text-align: center;">Impressions</th>
                                                <th  style="text-align: center;">Avg Pos</th>


                                            </tr>
                                        </thead>

                                        <tbody>



                                        </tbody>


                                    </table>

當我檢索這些值時,整個數據僅嵌入到第一列而不是其他列中,如何將這些值放入已填充所有列和行的數據表中?

試試下面的代碼。 希望這會幫助你。

 $("#example1").dataTable();
                $("#groupid").change(function(){
                var oTable = $('#example1').dataTable();
                var grpvalue=$('#groupid').val();
                var col = ["hea", "id", "desc", "desc", "desc2", "url", "cli","cpc","con","cost","ctr","imp","ap"]; 
                                $.ajax({
                                type:"post", 
                                dataType : 'json',
                                url:"pages/ads.php", 
                                data:"adgroup="+grpvalue, 
                                success: function(s) {
                                oTable.fnClearTable();
                                for(var i = 0; i < s.length; i++) {
                                 $("#example1 tr:first td:nth-child("+i+")" ).html(s[i][col[i]]);
                                        }
                                        }
                            });


                });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM