2014-10-06 175 views
0

我試圖把Laravel控制器提供的JSON數據顯示在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。 – 2014-10-06 06:40:08

回答

0

我認爲問題在於它是listAction,並且在保存時返回記錄。你應該嘗試改變index方法:

$res['Record'] = $items; 

到:

$res['Records'] = $items;