w2ui grid从api获取记录

时间:2018-05-17 16:49:53

标签: javascript plugins w2ui

这是我第一次尝试在我的项目中实现w2ui库。我有这个代码,我正试图从远程URL获取记录;

 <script type="text/javascript">
   $(function () {
    $('#grid').w2grid({
        name: 'grid',

      url:'http://api/payments.php',

       columns: [
        { field: 'name', caption: 'name', size: '30%' },

        { field: 'ref', caption: 'First Name', size: '30%' },
        { field: 'pay_date', caption: 'Last Name', size: '30%' },
        { field: 'amount', caption: 'Email', size: '40%' }


        ]

    });
});

api返回此json输出:

      [{"Key":"12;2TgBAACHBg==10;20897568710;","id":6,"amount":"50","pay_date":"2018-05-17T00:00:00Z","applicant_id":116,"paid_by":"sami","pay_type":"cash","ref":"NSC170621001","name":"Sekyi Quainoo"},{"Key":"12;2TgBAACHBw==10;20897569590;","id":7,"amount":"70","pay_date":"2018-05-17T00:00:00Z","applicant_id":119,"paid_by":"nii","pay_type":"cash","ref":"NSC170725119","name":"Adwoa Sam"}]

1 个答案:

答案 0 :(得分:0)

您需要更改返回的JSON以符合W2ui的预期。

  

网格需要来自服务器的JSON格式的数据,如下所述。 JSON结构将使用jQuery.extend方法合并到网格中。这意味着您可以从服务器返回任何支持的网格属性,并将应用它。

{
    "status"  : "success",
    "total"   : 36,
    "records" : [
        { "recid": 1, "field-1": "value-1", ... "field-N": "value-N" },
        ...
        { "recid": N, "field-1": "value-1", ... "field-N": "value-N" }
    ]
}

来源:http://w2ui.com/web/docs/1.5/grid/overview#data-structures