jTable不显示JSON数据

时间:2014-10-06 00:50:55

标签: php jquery json laravel jquery-jtable

我试图获取Laravel控制器提供的JSON数据并将其显示在jQuery jTable中。该表在请求时接收数据,但从不在表中显示。

我使用以下代码进行jTable设置:

$('#items').jtable({
        title: 'Items',
        useBootstrap: true,
        actions: {
            listAction: '{{ action('AdminAjaxItemsController@index', $invoice->id) }}',
            createAction: '{{ action('AdminAjaxItemsController@store') }}',
            updateAction: '{{ action('AdminAjaxItemsController@update') }}',
            deleteAction: '{{ action('AdminAjaxItemsController@destroy') }}'
        },
        fields: {
            id: {
                key: true,
                list: false
            },
            invoice_id: {
                input: function (data) {
                    return '<input type="hidden" name="invoice_id" value="{{ $invoice->id }}" />';
                }
            },
            quantity: {
                title: 'Qty',
                width: '5%'
            },
            name: {
                title: 'Name',
                width: '20%'
            },
            description: {
                title: 'Description',
                width: '35%'
            },
            cost_per_unit: {
                title: 'Price',
                width: '10%',
            },
            unit_label: {
                title: 'Unit Label',
                width: '10%',
            },
            tax: {
                title: 'Tax',
                width: '10%',
            },
            discount: {
                title: 'Discount',
                width: '10%',
            }
        }
    });

这是控制器代码:

public function index($id)
{
    $res = array();
    $items = Invoice::find($id)->items;
    $res['Result'] = "OK";
    $res['Record'] = $items;
    $json = json_encode($res);
    return $json;
}

这是为listAction提供的JSON:

{"Result":"OK","Record":[{"id":"4","quantity":"1.0","invoice_id":"4","name":"Test Item","description":"None","cost_per_unit":"5.0","unit_label":"hour","tax":"0.0","discount":"0.0","created_at":"2014-10-05 19:49:29","updated_at":"2014-10-05 19:49:29"}]}

最后这是JS控制台中的错误:

[Error] TypeError: undefined is not an object (evaluating 'e.length')
    each (jquery-1.10.2.js, line 4)
    _addRecordsToTable (jquery.jtable.js, line 548)
    (anonymous function) (jquery-ui.min.js, line 6)
    completeReload (jquery.jtable.js, line 446)
    success (jquery.jtable.js, line 488)
    success (jquery.jtable.js, line 1192)
    c (jquery-1.10.2.js, line 4)
    fireWith (jquery-1.10.2.js, line 4)
    k (jquery-1.10.2.js, line 6)
    r (jquery-1.10.2.js, line 6)

1 个答案:

答案 0 :(得分:0)

我认为问题在于它是listAction并且在保存时返回记录。您应该尝试更改index方法:

$res['Record'] = $items;

为:

$res['Records'] = $items;