将Json转换为Jquery DataTable

5

我一直在尝试将Json响应显示在Jquery数据表格上,但没有成功。基本上,一旦服务器返回Json响应,我希望它显示在表格上。我检查了Json并且它似乎是有效的Json响应。

JSON响应

[
    {
        "pk": 7,
        "model": "softwareapp.software",
        "fields": {
            "city": "miami",
            "submitted_by": [],
            "description": "test",
            "title": "test",
            "zipcode": "test",
            "rating_votes": 0,
            "state": "fl",
            "address": "test",
            "rating_score": 0,
            "business_size": [
                5
            ],
            "slug": "test",
            "developer": "test"
        }
    },
    {
        "pk": 8,
        "model": "softwareapp.software",
        "fields": {
            "city": "",
            "submitted_by": [],
            "description": "",
            "title": "test2",
            "zipcode": "",
            "rating_votes": 0,
            "state": "",
            "address": "",
            "rating_score": 0,
            "business_size": [
                5
            ],
            "slug": "test2",
            "developer": ""
        }
    },
    {
        "pk": 10,
        "model": "softwareapp.software",
        "fields": {
            "city": "",
            "submitted_by": [],
            "description": "",
            "title": "test3",
            "zipcode": "",
            "rating_votes": 0,
            "state": "",
            "address": "",
            "rating_score": 0,
            "business_size": [
                6
            ],
            "slug": "test3",
            "developer": ""
        }
    }
]

这里是Jquery函数。
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">

<script>
$(document).ready(function() {

  /*var kaboohAPI = '{% url software-list-ajax %}';*/
  jsondata = [];
  $('#filterform').on('submit',function(e){
    e.preventDefault();
    var query = $('#filterform').serialize();
        $.ajax({
                type:'GET',
                url: '{% url software-list-ajax %}',
                datatype: 'json',
                data: query,
                success: function(data){
                 console.log(data);
                   $('#example').dataTable({
                            'aaData': data,
                            "aaColumns":[
                                {"mData":"title"},
                                {"mData":"developer"}
                            ],

                        });
                }/* response processing function ends */
            });/* ajax function ends */



        });
});
</script>
2个回答

5

有没有一种方法可以在客户端修改JSON响应,使其可被Datatable读取? - shaytac
太准了!我把我的AJAX源代码转换成了对象数组,现在它能正常工作:) - Nigilan

0

mavroscy 这里是我在我的项目中使用它的方式。

Html

     <div style="width: 100%;">
            <div class="table-responsive">
                 <table id="exampleMy" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0">
                        <thead>
                            <tr>
                               <th>Name</th>
                               <th>dob</th>
                               <th>gender</th>
                               <th>mobile</th>
                               <th>email</th>
                             </tr>
                         </thead>
                         <tbody></tbody>
                 </table>
             </div>
        </div>

JS

<link href="~/Scripts/DataTables/DataTables1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="~/Scripts/DataTables/Responsive-2.2.1/css/responsive.dataTables.min.css" rel="stylesheet" />

<script src="~/Scripts/DataTables/DataTables1.10.16/js/jquery.dataTables.min.js"></script>
<script src="~/Scripts/DataTables/Responsive-2.2.1/js/dataTables.responsive.min.js"></script>

function DataList(ReqID) {

    $(document).ready(function () {
    getData();

})

  var getData() = function () {

    $.ajax({
        url: '/Controller/Action/',
        data: "{ 'prefix': '" + ReqID + "'}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: function (response) {

            BindDataTable(response);

        }

    })

}

 var BindDataTable = function (response) {

    $("#exampleMy").DataTable({
        "responsive": true,
        "aaData": response,
        "aoColumns": [

            { "mData": "name" },
            { "mData": "dob" },
            { "mData": "gender" },
            { "mData": "mobile" },
            { "mData": "email" }               

        ]
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接