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

尝试:

  1. <table id="sp_table" class="display">
  2. <tbody>
  3. </tbody>
  4. </table>
  5.  
  6. //So you just make the Ajax call "manually" and then use the return to initialise DataTables.
  7. $.ajax( {
  8. "url": 'whatever.php',
  9. "success": function ( json ) {
  10. json.bDestroy = true;
  11. $('#example').dataTable( json );
  12. },
  13. "dataType": "json"
  14. } );
  15.  
  16. {
  17.  
  18. "aaData": [
  19. [ "2010-07-27 10:43:08", "..."], [ "2010-06-28 17:54:33", "..."],
  20. [ "2010-06-28 16:09:06", "..."], [ "2010-06-09 19:15:00", "..."]
  21. ] ,
  22.  
  23. "aaSorting": [
  24. [ 1, "desc" ]
  25. ],
  26.  
  27. "aoColumns": [
  28. { "sTitle": "Title1" },
  29. { "sTitle": "Title2" }
  30. ]
  31.  
  32. }
  1. html
  2.  
  3. <table class="display" id="table"></table>
  4. js
  5.  
  6. $("#table").dataTable({
  7. bJQueryUI:true,
  8. aoColumns:[
  9. {mDataProp:"foo",sTitle:"Foo Title"},
  10. {mDataProp:"bar",sTitle:"Bar Title"}
  11. ],
  12. fnServerData: function( sUrl, data, fnCallback){
  13. $.get('data.php', function(res) {
  14. fnCallback({ // process results to match table format
  15. "sEcho":config.sEcho,
  16. "iTotalRecords":res.data.total || res.data.count,
  17. "iTotalDisplayRecords":res.data.count || res.data.total,
  18. "aaData":res.data.list
  19. })
  20. });
  21. }
  22. })
  23. Where data.php is
  24.  
  25. {
  26. data:{
  27. total:200,
  28. count:3,
  29. list:[
  30. {foo:"Foo 1",bar:"Bar 1"},
  31. {foo:"Foo 2",bar:"Bar 2"},
  32. {foo:"Foo 3",bar:"Bar 3"},
  33. ]
  34. }
  35. }
  1. I add a new columns by recreating the whole datatable which is not recommended. But here's roughly how I do it:
  2.  
  3. 1. Get column data via ajax
  4.  
  5. 2. Push new column def:
  6.  
  7. aoColumnDefs.push({
  8. "aTargets": [aoColumnDefs.length]
  9. , "sTitle": sTitle
  10. , "bSearchable": true
  11. , "bVisible": true
  12. , "bSortable": false
  13. , "sClass": "recipe"
  14. });
  15.  
  16. 3. Push new row data into aaData:
  17.  
  18. $.each(aaData, function (i, rowData) {
  19. rowData.push(newColData[rowData[1]] == undefined ? '' : newColData[rowData[1]]);
  20. });
  21.  
  22. 4. reinitialize DataTable
  23.  
  24. Removing a column:
  25.  
  26. 1. Get the index into aaData and aoColumnDefs of the column to remove by the column index of the column to remove:
  27.  
  28. var dataIndex = myConstants.numFixedColumns + index;
  29.  
  30. 2. Remove array element at the index obtained in #1.
  31.  
  32. $.each(aaData, function (i, data) {
  33. data.splice(dataIndex, 1);
  34. });
  35.  
  36. 3. Resequence aTargets:
  37.  
  38. $.each(aoColumnDefs, function (i, column) {
  39. column.aTargets = [i];
  40. });
  41.  
  42. 4. reinitialize DataTable
  1. 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...
  2.  
  3. Normal (works):
  4. jQuery('#example').dataTable({
  5. "sPaginationType": "full_numbers",
  6. "aaData": results.DATA ,
  7. "aoColumns": [ {"sTitle": "Name"}, {"sTitle": "County"} ]
  8. });
  9.  
  10. Give it the JSON (doesn't work):
  11. jQuery('#example').dataTable({
  12. "sPaginationType": "full_numbers",
  13. "aaData": results.DATA ,
  14. "aoColumns": results.COLUMNS
  15. });
  16.  
  17. Give it a string var (doesn't work):
  18. jQuery.each(results.COLUMNS, function(i, value){
  19. column_names += '{"sTitle": "'+ value + '"}';
  20. if (i+1 != results.COLUMNS.length) column_names += ", ";
  21. }
  22. ); // This creates the string '{"sTitle": "NAME"}, {"sTitle": "COUNTY"}' - confirmed in FireBug.
  23. jQuery('#example').dataTable({
  24. "sPaginationType": "full_numbers",
  25. "aaData": results.DATA ,
  26. "aoColumns": [ column_names ]
  27. });
  28.  
  29. Give it an object var (doesn't work):
  30. function(results){
  31. var columns = new Array();
  32. jQuery.each(results.COLUMNS, function(i, value){
  33. columns.push(column_name={stitle: value})
  34. }
  35. ); //Creates the object, confirmed in FireBug
  36. };
  37. jQuery('#example').dataTable({
  38. "sPaginationType": "full_numbers",
  39. "aaData": results.DATA ,
  40. "aoColumns": [ columns ]
  41. });
  42.  
  43. Help?
  44.  
  45. 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.
  46. fbasfbas August 2011
  47. I can't see why approach #2 doesn't work unless your aoColumns are not defined correctly.
  48. luketheobscureluketheobscure August 2011
  49. Interesting. Here's the truncated JSON data:
  50.  
  51. {"COLUMNS":["NAME","COUNTY"],"DATA":[[" John Doe","Fresno"],["Billy,"Fresno"],["Tom","Kern"],["King Smith","Kings"]]}
  52.  
  53. Resulting table renders correctly, just without the column names.
  54. fbasfbas August 2011
  55. use the same format as aoColumns (an array of objects)
  56.  
  57. {"COLUMNS":[{ sTitle: "NAME"}, { sTitle: "COUNTY"}],"DATA":[[" John Doe","Fresno"],["Billy,"Fresno"],["Tom","Kern"],["King Smith","Kings"]]}
  58.  
  59. 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/
  60. luketheobscureluketheobscure August 2011
  61. Thanks so much for helping me out.
  62.  
  63. In the end, I added this right before I declared my dataTable:
  64.  
  65. var columns = [];
  66. jQuery.each(results.COLUMNS, function(i, value){
  67. var obj = { sTitle: value };
  68. columns.push(obj);
  69. });
  70.  
  71. 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. UVA 11729 - Commando War(贪心 相邻交换法)

    Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...

  2. SQL的定义与使用

    一.SQL的定义 SQL(structured query language)即结构化查询语句,是关系数据库的标准语言. SQL的特点有: 1.综合统一 SQL集数据定义语言DDL.数据操作语言DML ...

  3. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  4. Docker之配置Centos_ssh

    写Dockerfile配置文件 #DockerfileFROM centos:6  #以下命令用在什么镜像中MAINTAINER cuizhipeng <cuizhipeng@126.com&g ...

  5. bootstrap Tooltip换行问题

    bootstrap自身带有tooltip,使用起来很方便,但是美中不足,它的tooltip并不支持换行. 比如我们通过<textarea>输入框传入到数据库的长文本,文本是带有换行符的,但 ...

  6. SQLServer:定时作业

    SQLServer:定时作业: 如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理-SQL Server代理-作业来实现 也快可以定时备 ...

  7. 2016年1月编程语言排行榜:Java荣获2015年度冠军

    Java因于2015年人气增幅最大(+ 5.94%),故获得2015年的TIOBE指数的编程语言奖,同时成为15年年度冠军, Visual Basic.NET(+ 1.51%)和Python(+ 1. ...

  8. 005 Python的数值类型

    005 Python的数值类型 BIF    指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...

  9. 3D 服务器端以向量计算为主的角色位置的算法

    把我以前学习过的一个东西拿出来分享下~ 3D服务器端玩家行走处理是服务器端根据客户端行走路径.玩家行走时间以及速度(包括变化速度)计算得出玩家的当前位置. 由于客户端行走是一条路径,不使用2D中的格子 ...

  10. C# 中的委托和事件 转张子阳

    引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...