Datatables中文网:http://dt.thxopen.com/index.html

尝试:

<table id="sp_table" class="display">
<tbody>
</tbody>
</table> //So you just make the Ajax call "manually" and then use the return to initialise DataTables.
$.ajax( {
"url": 'whatever.php',
"success": function ( json ) {
json.bDestroy = true;
$('#example').dataTable( json );
},
"dataType": "json"
} ); { "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" }
] }
html

<table class="display" id="table"></table>
js $("#table").dataTable({
bJQueryUI:true,
aoColumns:[
{mDataProp:"foo",sTitle:"Foo Title"},
{mDataProp:"bar",sTitle:"Bar Title"}
],
fnServerData: function( sUrl, data, fnCallback){
$.get('data.php', function(res) {
fnCallback({ // process results to match table format
"sEcho":config.sEcho,
"iTotalRecords":res.data.total || res.data.count,
"iTotalDisplayRecords":res.data.count || res.data.total,
"aaData":res.data.list
})
});
}
})
Where data.php is {
data:{
total:200,
count:3,
list:[
{foo:"Foo 1",bar:"Bar 1"},
{foo:"Foo 2",bar:"Bar 2"},
{foo:"Foo 3",bar:"Bar 3"},
]
}
}
I add a new columns by recreating the whole datatable which is not recommended. But here's roughly how I do it:

1. Get column data via ajax

2. Push new column def:

aoColumnDefs.push({
"aTargets": [aoColumnDefs.length]
, "sTitle": sTitle
, "bSearchable": true
, "bVisible": true
, "bSortable": false
, "sClass": "recipe"
}); 3. Push new row data into aaData: $.each(aaData, function (i, rowData) {
rowData.push(newColData[rowData[1]] == undefined ? '' : newColData[rowData[1]]);
}); 4. reinitialize DataTable Removing a column: 1. Get the index into aaData and aoColumnDefs of the column to remove by the column index of the column to remove: var dataIndex = myConstants.numFixedColumns + index; 2. Remove array element at the index obtained in #1. $.each(aaData, function (i, data) {
data.splice(dataIndex, 1);
}); 3. Resequence aTargets: $.each(aoColumnDefs, function (i, column) {
column.aTargets = [i];
}); 4. reinitialize DataTable
I've got DataTables working fine on my page- the only problem I have is dynamically declaring the table headers. I'm using an existing JSON object instead of using DataTables built in AJAX functions (for various reasons). Here's what I've tried so far...

Normal (works):
jQuery('#example').dataTable({
"sPaginationType": "full_numbers",
"aaData": results.DATA ,
"aoColumns": [ {"sTitle": "Name"}, {"sTitle": "County"} ]
}); Give it the JSON (doesn't work):
jQuery('#example').dataTable({
"sPaginationType": "full_numbers",
"aaData": results.DATA ,
"aoColumns": results.COLUMNS
}); Give it a string var (doesn't work):
jQuery.each(results.COLUMNS, function(i, value){
column_names += '{"sTitle": "'+ value + '"}';
if (i+1 != results.COLUMNS.length) column_names += ", ";
}
); // This creates the string '{"sTitle": "NAME"}, {"sTitle": "COUNTY"}' - confirmed in FireBug.
jQuery('#example').dataTable({
"sPaginationType": "full_numbers",
"aaData": results.DATA ,
"aoColumns": [ column_names ]
}); Give it an object var (doesn't work):
function(results){
var columns = new Array();
jQuery.each(results.COLUMNS, function(i, value){
columns.push(column_name={stitle: value})
}
); //Creates the object, confirmed in FireBug
};
jQuery('#example').dataTable({
"sPaginationType": "full_numbers",
"aaData": results.DATA ,
"aoColumns": [ columns ]
}); Help? PS: The code examples were just typed up- there may be some stray commas or something. All the code (and variables) were tested in the browser.
fbasfbas August 2011
I can't see why approach #2 doesn't work unless your aoColumns are not defined correctly.
luketheobscureluketheobscure August 2011
Interesting. Here's the truncated JSON data: {"COLUMNS":["NAME","COUNTY"],"DATA":[[" John Doe","Fresno"],["Billy,"Fresno"],["Tom","Kern"],["King Smith","Kings"]]} Resulting table renders correctly, just without the column names.
fbasfbas August 2011
use the same format as aoColumns (an array of objects) {"COLUMNS":[{ sTitle: "NAME"}, { sTitle: "COUNTY"}],"DATA":[[" John Doe","Fresno"],["Billy,"Fresno"],["Tom","Kern"],["King Smith","Kings"]]} you can use any of the "init options - columns" values listed on http://www.datatables.net/ref or follow the examples at http://www.datatables.net/examples/
luketheobscureluketheobscure August 2011
Thanks so much for helping me out. In the end, I added this right before I declared my dataTable: var columns = [];
jQuery.each(results.COLUMNS, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
}); In case anyone else is looking, this takes the native way that ColdFusion returns JSON columns and puts it into what dataTables expects.

Jquery Datatables 动态列名的更多相关文章

  1. jquery.dataTables动态列--转

    转自 https://www.cnblogs.com/lyeo/p/4765514.html jquery.dataTables  版本1.10.7 直接上代码: 0.table <table ...

  2. jquery.dataTables动态列

    jquery.dataTables  版本1.10.7 直接上代码: 0.table <table id="popReportTable"> <thead> ...

  3. Jquery DataTables相关示例

    一.Jquery-DataTables DataTables 是jquery的一个开源的插件.它具有高度灵活的特性,基于渐进增强的基础,可以为任何表格添加交互.它特性如下: 提供分页,搜索和多列排序: ...

  4. JQuery Datatables(一)

    最近项目中用了Bootstrap的样式风格,控件用了JQuery Datatables,主要有几下几点目标: 实现BootStrap风格的table,使用Ajax获取数据,并有勾选项 可以实现全选,单 ...

  5. jQuery DataTables的简单实现

    DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HTML表格. 主要特点: 1.自动分页处理 2.即时表格数据过滤 3.数 ...

  6. JQuery DataTables学习

    1.Datatables简单介绍 DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,根据的基础逐步增强,这将添加先进的互动控制.支持不论什么HTML表格. 主要特点: 自己主动 ...

  7. jQuery dataTables四种数据来源[转]

    2019独角兽企业重金招聘Python工程师标准>>> 四种数据来源 对于 dataTables 来说,支持四种表格数据来源. 最为基本的就是来源于网页,网页被浏览器解析为 DOM ...

  8. [jQuery]jQuery DataTables插件自定义Ajax分页实现

    前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢. 问题描述 园友是做前端的,产品经理要求他使用jQuery DataTables插件显示一个列表,要实现分 ...

  9. jquery Datatables 行数据删除、行上升、行下降功能演示

    Datatables 是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 官方网站:http://www.datatables.net Datatables ...

随机推荐

  1. Ubuntu系统的安装

    在上一篇博客中,我们已经建立了一个“空白”的虚拟Ubuntu镜像,在这篇博客中,我们将介绍如何安装并进入完整的Ubuntu系统. 写在前面:不同版本的系统在安装过程中,有些操作可能会不同,但是其核心步 ...

  2. Windows下ANSI、Unicode、UTF8字符编码转换

    主意:输入字符串必须是以'\0'结尾,如果输入字符串没有以'\0'结尾,请手动设置,否则转换会有错误. unsigned int EncodeUtil::AnsiToUcs2( char* pAnsi ...

  3. WP8手机解锁时提示“请确保IPOVERUSBSVC服务正常运行”解决方法

    如果你各种重启服务 卸载手机 重装驱动都试过了还不行,请看看你是否安装了Hyper-v或Vitualbox虚拟机,很有可能是虚拟交换机造成的. 我在网络连接属性里看到这个 把它卸载后,解锁成功. 解锁 ...

  4. JQ封装切换滚动功能

    /*---------控制滚动图片v1(作者:SFLYQ)-----------Options 配置对象,(用来配置控制元素的dom位置,初始化一些必要的对象或者方法)DoIni 初始化操作(初始化元 ...

  5. 使用WebBrowser的记录

    第一:新建一个类,用了获取WebBrowser元素的类 //需要引用 Interop.SHDocVw 和 Microsoft.mshtmlpublic class Element { //根据Name ...

  6. javaScript中with的用法

    1 JavaScript中的with语句的作用是为逐级的对象访问提供命名空间式的速写方式, 也就是在指定的代码区域, 直接通过节点名称调用对象 初次接触到with用法,是这样一段代码: 1 2 3 4 ...

  7. php练习6——面向对象编程(打印乘法表)

    要求:编写一个成员函数,从键盘输入一个数(0—9),打印出对应的乘法表 程序:viewChengFB.html chengFB.class.php printChengFB.php   结果  

  8. shell for循环+case的脚本(监控程序状态)

    分享一个shell for循环+case的脚本(监控程序状态) 分享一个for循环+case的脚本(监控程序状态并执行相关操作) ,供大家学习参考. 复制代码代码如下: #/bin/bash set ...

  9. 腾讯面试题 腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?

    腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?  这个题目已经有一段时间了,但是腾讯现在还在用来面试.腾讯第一次面 ...

  10. Entity Framework Code First 数据迁移

    需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...