2014-10-06 2 views
0

Я пытаюсь взять данные JSON, предоставленные контроллером Laravel, и отобразить его в jQuery jTable. Таблица получает данные, когда она запрашивает их, но никогда не отображает их в таблице.jTable Не отображает данные JSON

У меня есть JTable установки со следующим кодом:

$('#items').jtable({ 
     title: 'Items', 
     useBootstrap: true, 
     actions: { 
      listAction: '{{ action('[email protected]', $invoice->id) }}', 
      createAction: '{{ action('[email protected]') }}', 
      updateAction: '{{ action('[email protected]') }}', 
      deleteAction: '{{ action('[email protected]') }}' 
     }, 
     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; 
} 

Вот JSON предназначен для listAction:

{"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) 
+0

Конечно он не работает, потому что ваш смешивание PHP с JavaScript. –

ответ

0

Я думаю, проблема в том, что это listAction, и вы возвращаете запись, когда вы ее сохранили. Вы должны попробовать изменения в index метода:

$res['Record'] = $items; 

к:

$res['Records'] = $items; 
Смежные вопросы