├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md └── dataTables.treeTable.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Roy Ling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jquery.dataTables.TreeTable 2 | =========================== 3 | 4 | This is a plugin of [jquery.dataTables](http://datatables.net) to integrate [jquery.treeTable v2.x](https://github.com/ludo/jquery-treetable/tree/2.3.1). **Not working with the latest jquery-treetable plugin** 5 | 6 | If you are still interested, I did create a live example [here](https://jsfiddle.net/royling/zg9q6sjd/), hope this makes things clear. 7 | 8 | To use this plugin, check out the first 2 points and make use of the APIs in point 3: 9 | * Add the feature char `'T'` into `"sDom"` property in the initial settings of jquery.dataTables. 10 | * Specify initial settings for jquery.treeTable integrated, which is `"oTreeTable"` property in the initial settings of jquery.dataTables. 11 | For example: 12 | 13 | ```javascript 14 | $('#example').dataTable({ 15 | "sDom": "Ttfr", 16 | "oTreeTable": { 17 | "fnPreInit": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { 18 | // This function will be registerd as "aoRowCallback" of jquery.dataTables, 19 | // thus it has the same signature as "fnRowCallback". 20 | // Specify "id" & "class" attributes for each row (TR) required by jquery.treeTable: 21 | // for parent rows, add class 'parent'; 22 | // for children rows, add a class with name of prefix - 'child-of-' and parent id 23 | }, 24 | "showExpander": true, 25 | // The other settings to override the default options of jquery.treeTable, e.g. childPrefix, etc. 26 | }, 27 | // The other settings of jquery.dataTables here... 28 | }); 29 | ``` 30 | 31 | * Few APIs (extending jquery.dataTables) are also added to handle treeTable specifically, those are: 32 | 1. `isTreeTable()` 33 | 2. `fnReloadAjaxTreeTable(sNewSource, fnCallback, bStandingDraw)` 34 | 35 | -------------------------------------------------------------------------------- /dataTables.treeTable.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | "use strict"; 3 | 4 | // Private APIs 5 | var expandedNodes = {}, 6 | getInitOpts = function (oDTSettings) { 7 | return oDTSettings.oInit.oTreeTable || {}; 8 | }, 9 | initTreeTable = function (dataTable, options) { 10 | dataTable.treeTable(options).addClass('initialized-treeTable'); 11 | // remove expander 12 | if (options !== null && options.showExpander === false) { 13 | dataTable.$('td a.expander').remove(); 14 | } 15 | }; 16 | 17 | // Public APIs as extension of jquery.dataTables 18 | /** 19 | * Returns whether the current dataTable is also a treeTable or not. 20 | */ 21 | $.fn.dataTableExt.oApi.isTreeTable = function (oSettings) { 22 | return $('#' + oSettings.sTableId).hasClass('treeTable'); 23 | }; 24 | 25 | function isTreeTableInitialized(oSettings) { 26 | return $('#' + oSettings.sTableId).hasClass('treeTable initialized-treeTable'); 27 | } 28 | 29 | /** 30 | * Explicitly trigger the initialisation of treeTable. 31 | */ 32 | $.fn.dataTableExt.oApi.fnInitTreeTable = function (oSettings, options) { 33 | initTreeTable(oSettings.oInstance, options || {}); 34 | }; 35 | 36 | /** 37 | * Save expanded nodes (TRs) before reloading data table. 38 | * And after reloading, call restoreExpanded to restore the expand state. 39 | */ 40 | function saveExpanded (oSettings) { 41 | var oTable = oSettings.oInstance, expandedRows; 42 | if (!oTable.isTreeTable()) { 43 | return; 44 | } 45 | // save expanded row ids 46 | expandedRows = []; 47 | oTable.$('tr.parent.expanded').each(function () { 48 | expandedRows.push($(this).attr('id')); 49 | }); 50 | expandedNodes[oSettings.sTableId] = expandedRows; 51 | } 52 | 53 | /** 54 | * Expand nodes which are saved before reloading data table to restore the expand state. 55 | */ 56 | function restoreExpanded(oSettings) { 57 | if (!isTreeTableInitialized(oSettings)) { 58 | setTimeout(function () { 59 | restoreExpanded(oSettings); 60 | }, 200); 61 | return; 62 | } 63 | var i, iLen, oTable = oSettings.oInstance, tableId = oSettings.sTableId, 64 | expandedRows = expandedNodes[tableId] || []; 65 | for (i = 0, iLen = expandedRows.length; i < iLen; i++) { 66 | oTable.$('tr#' + expandedRows[i]).expand(); 67 | } 68 | expandedNodes[tableId] = null; 69 | } 70 | 71 | $.fn.dataTableExt.oApi.fnReloadAjaxTreeTable = function (oSettings, sNewSource, fnCallback, bStandingRedraw) { 72 | var oTable = oSettings.oInstance; 73 | saveExpanded(oSettings); 74 | oTable.removeClass('initialized-treeTable'); 75 | oTable.fnReloadAjax(sNewSource, fnCallback, bStandingRedraw); 76 | restoreExpanded(oSettings); 77 | }; 78 | 79 | // Register a plugin to integrate jquery.treeTable 80 | $.fn.dataTableExt.aoFeatures.push({ 81 | "fnInit": function (oDTSettings) { 82 | var oTable = oDTSettings.oInstance, 83 | opts = getInitOpts(oDTSettings); 84 | if (typeof opts.fnPreInit === 'function') { 85 | oTable.oApi._fnCallbackReg(oDTSettings, "aoRowCreatedCallback", opts.fnPreInit, "preInitTreeTable"); 86 | } 87 | oTable.oApi._fnCallbackReg(oDTSettings, "aoDrawCallback", function () { 88 | oTable.fnInitTreeTable(opts); 89 | }, "initTreeTable"); 90 | 91 | return null; /* No node to insert */ 92 | }, 93 | "cFeature": "T", 94 | "sFeature": "Integrate jquery.treeTable" 95 | }); 96 | }(jQuery)); 97 | --------------------------------------------------------------------------------