You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataTables.rowGroup.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*! RowGroup 1.1.2
  2. * ©2017-2020 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * @summary RowGroup
  6. * @description RowGrouping for DataTables
  7. * @version 1.1.2
  8. * @file dataTables.rowGroup.js
  9. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  10. * @contact datatables.net
  11. * @copyright Copyright 2017-2020 SpryMedia Ltd.
  12. *
  13. * This source file is free software, available under the following license:
  14. * MIT license - http://datatables.net/license/mit
  15. *
  16. * This source file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
  19. *
  20. * For details please refer to: http://www.datatables.net
  21. */
  22. (function (factory) {
  23. if (typeof define === 'function' && define.amd) {
  24. // AMD
  25. define(['jquery', 'datatables.net'], function ($) {
  26. return factory($, window, document);
  27. });
  28. } else if (typeof exports === 'object') {
  29. // CommonJS
  30. module.exports = function (root, $) {
  31. if (!root) {
  32. root = window;
  33. }
  34. if (!$ || !$.fn.dataTable) {
  35. $ = require('datatables.net')(root, $).$;
  36. }
  37. return factory($, root, root.document);
  38. };
  39. } else {
  40. // Browser
  41. factory(jQuery, window, document);
  42. }
  43. }(function ($, window, document, undefined) {
  44. 'use strict';
  45. var DataTable = $.fn.dataTable;
  46. var RowGroup = function (dt, opts) {
  47. // Sanity check that we are using DataTables 1.10 or newer
  48. if (!DataTable.versionCheck || !DataTable.versionCheck('1.10.8')) {
  49. throw 'RowGroup requires DataTables 1.10.8 or newer';
  50. }
  51. // User and defaults configuration object
  52. this.c = $.extend(true, {},
  53. DataTable.defaults.rowGroup,
  54. RowGroup.defaults,
  55. opts
  56. );
  57. // Internal settings
  58. this.s = {
  59. dt: new DataTable.Api(dt)
  60. };
  61. // DOM items
  62. this.dom = {};
  63. // Check if row grouping has already been initialised on this table
  64. var settings = this.s.dt.settings()[0];
  65. var existing = settings.rowGroup;
  66. if (existing) {
  67. return existing;
  68. }
  69. settings.rowGroup = this;
  70. this._constructor();
  71. };
  72. $.extend(RowGroup.prototype, {
  73. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  74. * API methods for DataTables API interface
  75. */
  76. /**
  77. * Get/set the grouping data source - need to call draw after this is
  78. * executed as a setter
  79. * @returns string~RowGroup
  80. */
  81. dataSrc: function (val) {
  82. if (val === undefined) {
  83. return this.c.dataSrc;
  84. }
  85. var dt = this.s.dt;
  86. this.c.dataSrc = val;
  87. $(dt.table().node()).triggerHandler('rowgroup-datasrc.dt', [dt, val]);
  88. return this;
  89. },
  90. /**
  91. * Disable - need to call draw after this is executed
  92. * @returns RowGroup
  93. */
  94. disable: function () {
  95. this.c.enable = false;
  96. return this;
  97. },
  98. /**
  99. * Enable - need to call draw after this is executed
  100. * @returns RowGroup
  101. */
  102. enable: function (flag) {
  103. if (flag === false) {
  104. return this.disable();
  105. }
  106. this.c.enable = true;
  107. return this;
  108. },
  109. /**
  110. * Get enabled flag
  111. * @returns boolean
  112. */
  113. enabled: function () {
  114. return this.c.enable;
  115. },
  116. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  117. * Constructor
  118. */
  119. _constructor: function () {
  120. var that = this;
  121. var dt = this.s.dt;
  122. var hostSettings = dt.settings()[0];
  123. dt.on('draw.dtrg', function (e, s) {
  124. if (that.c.enable && hostSettings === s) {
  125. that._draw();
  126. }
  127. });
  128. dt.on('column-visibility.dt.dtrg responsive-resize.dt.dtrg', function () {
  129. that._adjustColspan();
  130. });
  131. dt.on('destroy', function () {
  132. dt.off('.dtrg');
  133. });
  134. },
  135. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  136. * Private methods
  137. */
  138. /**
  139. * Adjust column span when column visibility changes
  140. * @private
  141. */
  142. _adjustColspan: function () {
  143. $('tr.' + this.c.className, this.s.dt.table().body()).find('td:visible')
  144. .attr('colspan', this._colspan());
  145. },
  146. /**
  147. * Get the number of columns that a grouping row should span
  148. * @private
  149. */
  150. _colspan: function () {
  151. return this.s.dt.columns().visible().reduce(function (a, b) {
  152. return a + b;
  153. }, 0);
  154. },
  155. /**
  156. * Update function that is called whenever we need to draw the grouping rows.
  157. * This is basically a bootstrap for the self iterative _group and _groupDisplay
  158. * methods
  159. * @private
  160. */
  161. _draw: function () {
  162. var dt = this.s.dt;
  163. var groupedRows = this._group(0, dt.rows({page: 'current'}).indexes());
  164. this._groupDisplay(0, groupedRows);
  165. },
  166. /**
  167. * Get the grouping information from a data set (index) of rows
  168. * @param {number} level Nesting level
  169. * @param {DataTables.Api} rows API of the rows to consider for this group
  170. * @returns {object[]} Nested grouping information - it is structured like this:
  171. * {
  172. * dataPoint: 'Edinburgh',
  173. * rows: [ 1,2,3,4,5,6,7 ],
  174. * children: [ {
  175. * dataPoint: 'developer'
  176. * rows: [ 1, 2, 3 ]
  177. * },
  178. * {
  179. * dataPoint: 'support',
  180. * rows: [ 4, 5, 6, 7 ]
  181. * } ]
  182. * }
  183. * @private
  184. */
  185. _group: function (level, rows) {
  186. var fns = $.isArray(this.c.dataSrc) ? this.c.dataSrc : [this.c.dataSrc];
  187. var fn = DataTable.ext.oApi._fnGetObjectDataFn(fns[level]);
  188. var dt = this.s.dt;
  189. var group, last;
  190. var data = [];
  191. var that = this;
  192. for (var i = 0, ien = rows.length; i < ien; i++) {
  193. var rowIndex = rows[i];
  194. var rowData = dt.row(rowIndex).data();
  195. var group = fn(rowData);
  196. if (group === null || group === undefined) {
  197. group = that.c.emptyDataGroup;
  198. }
  199. if (last === undefined || group !== last) {
  200. data.push({
  201. dataPoint: group,
  202. rows: []
  203. });
  204. last = group;
  205. }
  206. data[data.length - 1].rows.push(rowIndex);
  207. }
  208. if (fns[level + 1] !== undefined) {
  209. for (var i = 0, ien = data.length; i < ien; i++) {
  210. data[i].children = this._group(level + 1, data[i].rows);
  211. }
  212. }
  213. return data;
  214. },
  215. /**
  216. * Row group display - insert the rows into the document
  217. * @param {number} level Nesting level
  218. * @param {object[]} groups Takes the nested array from `_group`
  219. * @private
  220. */
  221. _groupDisplay: function (level, groups) {
  222. var dt = this.s.dt;
  223. var display;
  224. for (var i = 0, ien = groups.length; i < ien; i++) {
  225. var group = groups[i];
  226. var groupName = group.dataPoint;
  227. var row;
  228. var rows = group.rows;
  229. if (this.c.startRender) {
  230. display = this.c.startRender.call(this, dt.rows(rows), groupName, level);
  231. row = this._rowWrap(display, this.c.startClassName, level);
  232. if (row) {
  233. row.insertBefore(dt.row(rows[0]).node());
  234. }
  235. }
  236. if (this.c.endRender) {
  237. display = this.c.endRender.call(this, dt.rows(rows), groupName, level);
  238. row = this._rowWrap(display, this.c.endClassName, level);
  239. if (row) {
  240. row.insertAfter(dt.row(rows[rows.length - 1]).node());
  241. }
  242. }
  243. if (group.children) {
  244. this._groupDisplay(level + 1, group.children);
  245. }
  246. }
  247. },
  248. /**
  249. * Take a rendered value from an end user and make it suitable for display
  250. * as a row, by wrapping it in a row, or detecting that it is a row.
  251. * @param {node|jQuery|string} display Display value
  252. * @param {string} className Class to add to the row
  253. * @param {array} group
  254. * @param {number} group level
  255. * @private
  256. */
  257. _rowWrap: function (display, className, level) {
  258. var row;
  259. if (display === null || display === '') {
  260. display = this.c.emptyDataGroup;
  261. }
  262. if (display === undefined || display === null) {
  263. return null;
  264. }
  265. if (typeof display === 'object' && display.nodeName && display.nodeName.toLowerCase() === 'tr') {
  266. row = $(display);
  267. } else if (display instanceof $ && display.length && display[0].nodeName.toLowerCase() === 'tr') {
  268. row = display;
  269. } else {
  270. row = $('<tr/>')
  271. .append(
  272. $('<td/>')
  273. .attr('colspan', this._colspan())
  274. .append(display)
  275. );
  276. }
  277. return row
  278. .addClass(this.c.className)
  279. .addClass(className)
  280. .addClass('dtrg-level-' + level);
  281. }
  282. });
  283. /**
  284. * RowGroup default settings for initialisation
  285. *
  286. * @namespace
  287. * @name RowGroup.defaults
  288. * @static
  289. */
  290. RowGroup.defaults = {
  291. /**
  292. * Class to apply to grouping rows - applied to both the start and
  293. * end grouping rows.
  294. * @type string
  295. */
  296. className: 'dtrg-group',
  297. /**
  298. * Data property from which to read the grouping information
  299. * @type string|integer|array
  300. */
  301. dataSrc: 0,
  302. /**
  303. * Text to show if no data is found for a group
  304. * @type string
  305. */
  306. emptyDataGroup: 'No group',
  307. /**
  308. * Initial enablement state
  309. * @boolean
  310. */
  311. enable: true,
  312. /**
  313. * Class name to give to the end grouping row
  314. * @type string
  315. */
  316. endClassName: 'dtrg-end',
  317. /**
  318. * End grouping label function
  319. * @function
  320. */
  321. endRender: null,
  322. /**
  323. * Class name to give to the start grouping row
  324. * @type string
  325. */
  326. startClassName: 'dtrg-start',
  327. /**
  328. * Start grouping label function
  329. * @function
  330. */
  331. startRender: function (rows, group) {
  332. return group;
  333. }
  334. };
  335. RowGroup.version = "1.1.2";
  336. $.fn.dataTable.RowGroup = RowGroup;
  337. $.fn.DataTable.RowGroup = RowGroup;
  338. DataTable.Api.register('rowGroup()', function () {
  339. return this;
  340. });
  341. DataTable.Api.register('rowGroup().disable()', function () {
  342. return this.iterator('table', function (ctx) {
  343. if (ctx.rowGroup) {
  344. ctx.rowGroup.enable(false);
  345. }
  346. });
  347. });
  348. DataTable.Api.register('rowGroup().enable()', function (opts) {
  349. return this.iterator('table', function (ctx) {
  350. if (ctx.rowGroup) {
  351. ctx.rowGroup.enable(opts === undefined ? true : opts);
  352. }
  353. });
  354. });
  355. DataTable.Api.register('rowGroup().enabled()', function () {
  356. var ctx = this.context;
  357. return ctx.length && ctx[0].rowGroup ?
  358. ctx[0].rowGroup.enabled() :
  359. false;
  360. });
  361. DataTable.Api.register('rowGroup().dataSrc()', function (val) {
  362. if (val === undefined) {
  363. return this.context[0].rowGroup.dataSrc();
  364. }
  365. return this.iterator('table', function (ctx) {
  366. if (ctx.rowGroup) {
  367. ctx.rowGroup.dataSrc(val);
  368. }
  369. });
  370. });
  371. // Attach a listener to the document which listens for DataTables initialisation
  372. // events so we can automatically initialise
  373. $(document).on('preInit.dt.dtrg', function (e, settings, json) {
  374. if (e.namespace !== 'dt') {
  375. return;
  376. }
  377. var init = settings.oInit.rowGroup;
  378. var defaults = DataTable.defaults.rowGroup;
  379. if (init || defaults) {
  380. var opts = $.extend({}, defaults, init);
  381. if (init !== false) {
  382. new RowGroup(settings, opts);
  383. }
  384. }
  385. });
  386. return RowGroup;
  387. }));