2017-07-31 94 views
1

我试图将JSON传递给dataTable。将客户端JSON传递给dataTable - 语法?

我在脚本的另一个位置加载JSON,然后截断我需要的部分,并将它传递到应该呈现表的函数中。 功能是:

function populateCasesTable(d){ 
     var json = d.cases; 
     var cols = [ 
      { "data" : json.service_area}, 
      { "data" : json.presenting_issue }, 
      { "data" : json.referred_by }, 
      { "data" : json.date_opened }, 
      { "data" : json.case_status }, 
      { "data" : json.preferred_staff } 
     ]; 

     console.log(json); 

     $('#cases_table').DataTable({ 
      "data": json, 
      "columns": cols 
     }); 
    } 

HTML为表是

<table id='cases_table' width="100%"> 
    <thead> 
    <tr> 
     <th>Service Area</th> 
     <th>Presenting Issue</th> 
     <th>Referred by</th> 
     <th>Date opened</th> 
     <th>Status</th> 
     <th>Preferred Staff</th> 
    </tr> 
    </thead> 

</table> 

的输出的console.log返回对象的适量(12)和一个单一的对象看起来例如:

0 Object: 
    action:9 
    case_status:"Open" 
    date_created:"11/07/2017" 
    date_opened:"11/07/2017" 
    file:5 
    id:31646 
    issues:8 
    preferred_staff:"Doc Kuran" 
    presenting_issue:"Anxiety Disorder" 
    referral:0 
    referred_by:"Academic staff" 
    serviceAreaId:14 
    service_area:"Disability Services" 
    staff:0 

我得到正确数量的页面伴随着神秘的'DataTables警告:表id = cases_table - 请求未知的参数'0'行0,列0.欲了解更多信息关于此错误,请参阅http://datatables.net/tn/4'弹出窗口。

我确定这是一个配置问题,但我无法解决这个问题。

我错过了什么?

谢谢。

回答

0

我找到了。 Cols必须设置如下:

var cols = [ 
      { "data" : 'service_area'}, 
      { "data" : 'presenting_issue' }, 
      { "data" : 'referred_by' }, 
      { "data" : 'date_opened' }, 
      { "data" : 'case_status' }, 
      { "data" : 'preferred_staff' } 
     ];