如何使用带有角度数据表的这个json

时间:2016-02-24 08:19:52

标签: javascript angularjs json datatables angular-datatables

这是我的服务器响应。

{
"status": "success",
"data": [{
    "id": null,
    "reportType": "Total Voucher Report",
    "reportModule": "Vouchers",
    "reportData": [{
        "id": "1",
        "voucherPackId": "2",
        "serialNumber": "0",
        "status": "Free",
        "isBlocked": "N",
        "voucherPin": "0",
        "buyDate": null,
        "redeemDate": null,
        "phoneNumber": null,
        "statusCode": null,
        "identifier": "MCM0007532",
        "merchantName": "test1",
        "voucherName": "fddf",
        "expiryDate": "2016-02-24 00:00:00",
        "dealCategory": "Hotels \u0026 Travel",
        "shortDescription": "xvxv",
        "voucherWorth": "33.00"
    }, {
        "id": "2",
        "voucherPackId": "2",
        "serialNumber": "0",
        "status": "Free",
        "isBlocked": "N",
        "voucherPin": "0",
        "buyDate": null,
        "redeemDate": null,
        "phoneNumber": null,
        "statusCode": null,
        "identifier": "MCM0007532",
        "merchantName": "test1",
        "voucherName": "fddf",
        "expiryDate": "2016-02-24 00:00:00",
        "dealCategory": "Hotels \u0026 Travel",
        "shortDescription": "xvxv",
        "voucherWorth": "33.00"
    }, {
        "id": "3",
        "voucherPackId": "2",
        "serialNumber": "0",
        "status": "Free",
        "isBlocked": "N",
        "voucherPin": "0",
        "buyDate": null,
        "redeemDate": null,
        "phoneNumber": null,
        "statusCode": null,
        "identifier": "MCM0007532",
        "merchantName": "test1",
        "voucherName": "fddf",
        "expiryDate": "2016-02-24 00:00:00",
        "dealCategory": "Hotels \u0026 Travel",
        "shortDescription": "xvxv",
        "voucherWorth": "33.00"
    }]
}],
"message": null}

我用它作为,

vm.dtOptions = DTOptionsBuilder
        .newOptions()
        .withOption('ajax', {
            url: config.base_url + 'report/voucher?module=Vouchers&type=Total Voucher Report&merchant=1',
            type: 'POST',
            dataSrc: 'data.data[0].reportData[0]',
        })
        .withOption('processing', true)
        .withOption('serverSide', true)
        .withBootstrap()
        .withPaginationType('full_numbers');

它表示无效的JSON响应。感谢您的亲切帮助。 调试结果:http://debug.datatables.net/urizon

3 个答案:

答案 0 :(得分:1)

dataSrc选项使用以下值:data[0].reportData,如下所示。此外,您还需要删除serverSideprocessing选项,因为您的数据没有正确的服务器端处理模式结构。

您还需要定义列结构,因为您正在使用对象数组作为数据源。

vm.dtOptions = DTOptionsBuilder
        .newOptions()
        .withOption('ajax', {
            url: config.base_url + 'report/voucher?module=Vouchers&type=Total Voucher Report&merchant=1',
            type: 'POST',
            dataSrc: 'data[0].reportData'
        })
        .withBootstrap()
        .withPaginationType('full_numbers');

vm.dtColumns = [
    /* List data properties for each column in the table. */
    DTColumnBuilder.newColumn('id'),
    DTColumnBuilder.newColumn('voucherPackId'),
    DTColumnBuilder.newColumn('serialNumber'),
    DTColumnBuilder.newColumn('status')        
];

答案 1 :(得分:0)

如果您使用解析器,您将收到错误:SyntaxError:JSON.parse:JSON数据第63行第16行中对象的属性值之后的数据结束。所以是你的JSON无效!只需在列表行上添加}即可。因为每个括号都需要关闭。

答案 2 :(得分:0)

确保JSON响应具有Content-Type: application/json标头,否则可能无法正确解析。