首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jquery datatables -从json获取列

jquery datatables -从json获取列
EN

Stack Overflow用户
提问于 2011-12-29 16:01:49
回答 2查看 21K关注 0票数 5

在jquery Datatables中,可以使用服务器端脚本定义列吗?我需要这样的东西

必须从服务器加载带有日期的列。那么列数可以改变。

EN

回答 2

Stack Overflow用户

发布于 2011-12-29 18:33:49

扩展一下卡迈勒·迪普·辛格所说的:

您可以动态创建表,然后对其应用数据表以获得数据表的功能。

代码语言:javascript
运行
复制
// up in the html
<table id="myDatatable" class="... whatever you need..."></table>

然后:

代码语言:javascript
运行
复制
// in the javascript, where you would ordinarily initialize the datatable
var newTable = '<thead><tr>'; // start building a new table contents

// then call the data using .ajax()
$.ajax( {
    url: "http://my.data.source.com",
    data: {}, // data, if any, to send to server
    success: function(data) {
        // below use the first row to grab all the column names and set them in <th>s
        $.each(data[0], function(key, value) {
            newTable += "<th>" + key + "</th>";
        });
        newTable += "</tr></thead><tbody>";                  

        // then load the data into the table
        $.each(data, function(key, row) {
             newTable += "<tr>";
              $.each(row, function(key, fieldValue) {
                   newTable += "<td>" + fieldValue + "</td>";
              });
             newTable += "</tr>";
        });
       newTable += '<tbody>';

       $('#myDatatable').html(newTable); // replace the guts of the datatable's table placeholder with the stuff we just created. 
    }
 });

 // Now that our table has been created, Datatables-ize it
 $('#myDatatable').dataTable(); 

注意,您可以像往常一样将设置放入.dataTable()中,而不是'sAjaxSource‘或任何相关的数据获取函数--这是将数据表应用到已经存在的表中,这是我们动态创建的表。

好吧,这是一种老生常谈的方式,但它应该是可行的。

目前还没有一种内置的方法来动态地对数据表执行此操作。查看此处:https://github.com/DataTables/DataTables/issues/273

票数 5
EN

Stack Overflow用户

发布于 2011-12-29 21:19:13

我想我找到你要找的东西了

我会粘贴一些代码+发布一个链接到一个类似的Q‘,在那里你会得到更多的信息…

代码语言:javascript
运行
复制
$.ajax( {
    "url": 'whatever.php',
    "success": function ( json ) {
        json.bDestroy = true;
        $('#example').dataTable( json );
     },
    "dataType": "json"
} );

其中json类似于下面的内容:

代码语言:javascript
运行
复制
{

"aaData": [
[ "2010-07-27 10:43:08", "..."], [ "2010-06-28 17:54:33", "..."],
[ "2010-06-28 16:09:06", "..."], [ "2010-06-09 19:15:00", "..."]
] ,

 "aaSorting": [
  [ 1, "desc" ]
 ],

 "aoColumns": [
   { "sTitle": "Title1" },
   { "sTitle": "Title2" }
 ]

}

这里有一个指向原始线程的链接

Column definition via JSON array (ajax)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8665309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档