从Javascript源创建数据表

时间:2015-05-28 18:53:29

标签: javascript jquery json datatables

我正在尝试基于填充了JSON数据的javascript var的输入构建数据表(1.10.7)。虽然我已成功使用带有ajax源的数据表,但我从未试图提供自己的变量,但在这种情况下我需要。

这是我的变量:

@Override
protected void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Make this activity, full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Hide the Title bar of this activity screen
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

...

}

根据DataTables' page on Javascript-Sourced Data

json = [
 {"dateReceived":"2015-01-01","designCustomer":"MULTITEST 1","designCustomerLocation":"SUNNYVALE, CA"},
 {"dateReceived":"2016-04-05","designCustomer":"MULTITEST 2","designCustomerLocation":"SUNNYVALE, CA"},
 {"dateReceived":"1982-04-18","designCustomer":"MULTITEST 3","designCustomerLocation":"SUNNYVALE, CA"}
 ]

我对他们的测试字符串和我的差异感到困惑:

This is achieved using the dataDT option in the initialisation object, passing in an array of data to be used (like all other DataTables handled data, this can be arrays or objects using the columns.dataDT option).

即,他们的测试变量不包含var dataSet = [ ['Trident','Internet Explorer 4.0','Win 95+','4','X'], ['Trident','Internet Explorer 5.0','Win 95+','5','C'], ['Trident','Internet Explorer 5.5','Win 95+','5.5','A'], ['Trident','Internet Explorer 6','Win 98+','6','A'], ['Trident','Internet Explorer 7','Win XP SP2+','7','A'] ,而我的测试变量不包含field names

以下是我为尝试解析变量json而编写的代码:

var table = $('#ltc-table').DataTable( {    
    "data" : json,        
    "columns" : [
      { "title" : "designCustomer" },
      { "title" : "designCutomerLocation" },
      { "title" : "dateReceived" },
  ],
  "lengthMenu": [ 25, 50, 101 ],
  "oLanguage": {
  "sSearch": "Filter Results: "
  }
});

根据我上面链接的页面,我似乎正确地执行了此操作,但我收到了错误:

Uncaught Error: DataTables warning: table id=ltc-table - Requested unknown parameter '0' for row 0.

任何提示?

1 个答案:

答案 0 :(得分:2)

如果您想使用objects as data source而不是数组,则需要使用json = [ {"dateReceived":"2015-01-01","designCustomer":"MULTITEST 1","designCustomerLocation":"SUNNYVALE, CA"}, {"dateReceived":"2016-04-05","designCustomer":"MULTITEST 2","designCustomerLocation":"SUNNYVALE, CA"}, {"dateReceived":"1982-04-18","designCustomer":"MULTITEST 3","designCustomerLocation":"SUNNYVALE, CA"} ]; var table = $('#ltc-table').DataTable( { "data" : json, "columns" : [ { "data" : "designCustomer" }, { "data" : "designCustomerLocation" }, { "data" : "dateReceived" } ], "lengthMenu": [ 25, 50, 101 ], "oLanguage": { "sSearch": "Filter Results: " } }); 选项绑定列:

if (!HttpContext.User.IsInRole("SuperAdmin") || HttpContext.User.IsInRole("ReadOnlyAdmin"))
{
    await UserManager.AddToRoleAsync(userDetails.Id, "User");
}

请参阅demo