2015-03-02 137 views
1

我有這樣的代碼:JSON源數據表

testdata = [{ 
    "hasresults": true, 
    "resultscount": 5, 
    "dob": null, 
    "chart": { 
     "rows": [ 
      { 
       "chart": "BAR000", 
       "firstname": "RUSSELL", 
       "lastname": "BARON" 
      }, 
      { 
       "chart": "BAR001", 
       "firstname": "BRUSELL", 
       "lastname": "BARON" 
      }, 
      { 
       "chart": "BAR002", 
       "firstname": "GARY", 
       "lastname": "BARON" 
      } 
     ] 
    } 
}]; 

$('#test').dataTable({ 
    "aaData": testdata, 
     "aoColumns": [{ 
     "mDataProp": "chart" 
    }, { 
     "mDataProp": "firstname" 
    }, { 
     "mDataProp": "lastname" 
    }] 
}); 

有人可以幫助我這是爲什麼不工作?看來,如果我去掉下面,將工作:

"hasresults": true, 
"resultscount": 5, 
"dob": null, 
"chart": { 

Not working fiddle

Working fiddle

+0

嘗試切換' 「DOB」:這對JSON資源類型]:null'到' 「DOB」 empty'見(HTTP:/ /www.tutorialspoint.com/json/json_data_types.htm) – CalebB 2015-03-02 19:31:59

+0

試過但不起作用。我在想mDataProp應該有chart.rows.chart,但它也不起作用。 – aldrin 2015-03-02 20:56:22

+0

它給你的具體例外是什麼? – CalebB 2015-03-02 22:08:59

回答

2

你只需要解決testdata的正確方法。 testdata是一個數組,持有一個對象,它有另一個對象chart,持有一個數組rows

$('#test').dataTable({ 
    "aaData": testdata[0].chart.rows, //<------ 
    "aoColumns": [{ 
     "mDataProp": "chart" 
    }, { 
     "mDataProp": "firstname" 
    }, { 
     "mDataProp": "lastname" 
    }] 
}); 

你的代碼在這裏工作 - >http://jsfiddle.net/j1fvL96e/

+0

謝謝。在mDataProp中關閉使用它,但它應該如您所示在aaData中。 – aldrin 2015-03-03 16:31:13