【问题标题】:jTable Not Displaying JSON DatajTable 不显示 JSON 数据
【发布时间】:2014-10-06 00:50:54
【问题描述】:

我正在尝试获取 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)

【问题讨论】:

  • 当然不行,因为你把 PHP 和 javascript 混在一起了。

标签: php jquery json laravel jquery-jtable


【解决方案1】:

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

$res['Record'] = $items;

到:

$res['Records'] = $items;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2012-01-07
    • 2015-05-30
    相关资源
    最近更新 更多