谁将JSON对象加载到Datatable中?

时间:2014-02-05 08:14:56

标签: javascript jquery json datatable

我的DataTable函数在body标签html中,如果可能我不想使用AJAX

$(document).ready(function() {
    $('#test_table').dataTable( {
        "aaData": JSON_obj,
        "aoColumns": [
            {"mDataProp": "name"},
            {"mDataProp": "chance"},
            {"mDataProp": "multi"},
            {"mDataProp": "frequency"}
        ]
    }); 
});

JSON_obj(表格mongodb):

{"52efa4c17bc16b98d47b8480":{"_id":{"$id":"52efa4c17bc16b98d47b8480"},"chance":8,"frequency":600,"id":3,"multi":100,"name":"Jerboa","xp_reward":1},"52efa4c17bc16b98d47b8481":{"_id":{"$id":"52efa4c17bc16b98d47b8481"},"chance":50,"frequency":600,"id":2,"multi":10,"name":"Partridge","xp_reward":1},"52efa4c17bc16b98d47b8482":{"_id":{"$id":"52efa4c17bc16b98d47b8482"},"chance":100,"frequency":20,"id":1,"multi":1,"name":"Dabb","xp_reward":1}}
你能帮帮我吗? 原谅我糟糕的英语!

1 个答案:

答案 0 :(得分:0)

var data = JSON.parse('{"52efa4c17bc16b98d47b8480":{"_id":{"$id":"52efa4c17bc16b98d47b8480"},"chance":8,"frequency":600,"id":3,"multi":100,"name":"Jerboa","xp_reward":1},"52efa4c17bc16b98d47b8481":{"_id":{"$id":"52efa4c17bc16b98d47b8481"},"chance":50,"frequency":600,"id":2,"multi":10,"name":"Partridge","xp_reward":1},"52efa4c17bc16b98d47b8482":{"_id":{"$id":"52efa4c17bc16b98d47b8482"},"chance":100,"frequency":20,"id":1,"multi":1,"name":"Dabb","xp_reward":1}}';

$("#attr").dataTable().fnAddData(data["52efa4c17bc16b98d47b8480"]);
$("#attr").dataTable().fnAddData(data["52efa4c17bc16b98d47b8481"]);
$("#attr").dataTable().fnAddData(data["52efa4c17bc16b98d47b8482"]);