简体   繁体   English

jQuery 数据表 - 来自 AJAX 响应的表头

[英]jQuery Datatables - Table header from AJAX response

I am retrieving a dataset that will include different lengths of rows via the Datatables ajax request.我正在检索一个数据集,该数据集将通过 Datatables ajax 请求包含不同长度的行。 For example, one response might be:例如,一种响应可能是:

... ['jan', 'feb', 'march', 'april'] ...

where as a different response would only be:不同的回应只会是:

... ['jan', 'feb'] ...

Most examples that I have seen have the table headers already hard-coded into the HTML.我见过的大多数示例都已将表格标题硬编码到 HTML 中。 What I want to do is to dynamically update the headers and their respective column data.我想要做的是动态更新标题及其各自的列数据。

I was hoping to be able to do something like this where I am able to update the headers with dataSrc, but by that time the datatable has already been created and fails to fill in the rest of the table correctly.我希望能够做这样的事情,我可以用 dataSrc 更新标题,但到那时数据表已经创建并且无法正确填写表格的其余部分。

....
table = $('#datatables').DataTable( {
    "ajax": {
        "url": "/foo/bar/",
        "type": "GET",
        dataSrc: function (json) { 
            populate_table_header(json)
            return json.blah;
        }
},
...

I am perfectly ok with destroying and re-creating the table, I just need to know how to create the table while using the AJAX response data for new headers.我完全可以销毁和重新创建表,我只需要知道如何在将 AJAX 响应数据用于新标头时创建表。

Edit编辑

Here's an example这是一个例子

   {  
  "data":[  
     ...
     [  
        "8290808-123123",
        "Boo Far",
        "bar@fu.com",
        "Other",
        "12",
        "21390",
        "123",
        "-",
        "-"
     ],
     [  
        "123123-032489",
        "Foo Bar",
        "foo@bar.com",
        "Name",
        "1",
        "2",
        "50",
        "-",
        "-"
     ],
     ...
  ],
  "header":[  
     "Owner",
     "Subscription",
     "Oct '16",
     "Nov '16",
     "Dec '16",
     "Jan '17",
     "Feb '17"
  ],


}

Is this possible without making a separate AJAX call before initializing the datatable?在初始化数据表之前不进行单独的 AJAX 调用,这可能吗?

try this answers.试试这个答案。 datatables.net 数据表.net

allan's say艾伦说

Good to hear you got it working :-)很高兴听到你成功了:-)

If you can not solve the problem like this add live jsfidde demo to may helpfull you.如果您不能解决这样的问题,请添加实时 jsfidde 演示可能会对您有所帮助。 Good luck with job.祝工作顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM