简体   繁体   English

将Json数据作为对象发送到DataTables

[英]Sending Json data as an object into DataTables

I have been trying to use DataTables. 我一直在尝试使用DataTables。 But after my Ajax request i get a Json object which i cannot pass into dataTables. 但是在我的Ajax请求之后,我得到了一个Json对象,该对象无法传递到dataTables中。

The Json object i receive is the following 我收到的Json对象如下

{"data": [{"attributes": {"purchasedate": "04/01/2017", "medication": "meds", "cost": 100.0, "expirydate": "04/03/2017", "quantity": 100.0}, "type": "medical_inventory"}, {"attributes": {"purchasedate": "04/01/2017", "medication": "Extra Meds", "cost": 100.0, "expirydate": "04/02/2017", "quantity": 100.0}, "type": "medical_inventory"}, {"attributes": {"purchasedate": "04/01/2017", "medication": "Extra Super Meds", "cost": 267.0, "expirydate": "04/11/2017", "quantity": 250.0}, "type": "medical_inventory"}], "links": {"self": "/medical_inventory/"}}

Following is my HTML code 以下是我的HTML代码

<table id="myTable" class="display" cellspacing="0" width="90%">
    <thead>
        <tr>
            <th>Medication</th>
            <th>Medication Quantity</th>
            <th>Mediaction Cost</th>
            <th>Purchase Date</th>
            <th>Expiry Date</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Medication</th>
            <th>Medication Quantity</th>
            <th>Mediaction Cost</th>
            <th>Purchase Date</th>
            <th>Expiry Date</th>
        </tr>
    </tfoot>
</table>

The Ajax request i do is the following 我做的Ajax请求如下

$(document).ready(function () {
        $.ajax({
            url : '/api/medical_inventory/',
            type : 'GET',
            dataType : 'json',
            success : function(data) {
                assignToEventsColumns(data);
            }
        });

        function assignToEventsColumns(data) {
            var table = $('#myTable').dataTable({
                "bAutoWidth" : false,
                "aaData" : data,
                "columns" : [ {
                    "data" : "medication"
                }, {
                    "data" : "quantity"
                }, {
                    "data" : "cost"
                }, {
                    "data" : "purchasedate"
                }, {
                    "data" : "expirydate"
                } ]
            })
        }
    });

This is the output i am currently getting 这是我目前得到的输出

Try mapping the data to remove the attributes property of each object 尝试映射数据以删除每个对象的attributes属性

success : function(data) {
   data.data = data.data.map(function(item){
      return item.attributes;
  });
  assignToEventsColumns(data);
}

You were very nearly there: 您几乎在那里:

function assignToEventsColumns(data) {
    var table = $('#myTable').dataTable({
        "bAutoWidth": false,
        "aaData": data.data,
        "columns": [{
            "data": "attributes.medication"
        }, {
            "data": "attributes.quantity"
        }, {
            "data": "attributes.cost"
        }, {
            "data": "attributes.purchasedate"
        }, {
            "data": "attributes.expirydate"
        }]
    })
}

All you were missing was the structure of your JSON, you needed to add attributes. 您所缺少的只是JSON的结构,您需要添加attributes. as well as your data.data :-D.Working JSFiddle here . 以及您的data.data在这里使用JSFiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM