Line 0
Link Here
|
|
|
1 |
/* |
2 |
* File: jquery.dataTables.grouping.js |
3 |
* Version: 1.2.9. |
4 |
* Author: Jovan Popovic |
5 |
* |
6 |
* Copyright 2013 Jovan Popovic, all rights reserved. |
7 |
* |
8 |
* This source file is free software, under either the GPL v2 license or a |
9 |
* BSD style license, as supplied with this software. |
10 |
* |
11 |
* This source file is distributed in the hope that it will be useful, but |
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 |
* or FITNESS FOR A PARTICULAR PURPOSE. |
14 |
* Parameters: |
15 |
* @iGroupingColumnIndex Integer Index of the column that will be used for grouping - default 0 |
16 |
* @sGroupingColumnSortDirection Enumeration Sort direction of the group |
17 |
* @iGroupingOrderByColumnIndex Integer Index of the column that will be used for ordering groups |
18 |
* @sGroupingClass String Class that will be associated to the group row. Default - "group" |
19 |
* @sGroupItemClass String Class that will be associated to the group row of group items. Default - "group-item" |
20 |
* @bSetGroupingClassOnTR Boolean If set class will be set to the TR instead of the TD withing the grouping TR |
21 |
* @bHideGroupingColumn Boolean Hide column used for grouping once results are grouped. Default - true |
22 |
* @bHideGroupingOrderByColumn Boolean Hide column used for ordering groups once results are grouped. Default - true |
23 |
* @sGroupBy Enumeration Type of grouping that should be applied. Values "name"(default), "letter", "year" |
24 |
* @sGroupLabelPrefix String Prefix that will be added to each group cell |
25 |
* @bExpandableGrouping Boolean Attach expand/collapse handlers to the grouping rows |
26 |
* @bExpandSingleGroup Boolean Use accordon grouping |
27 |
* @iExpandGroupOffset Integer Number of pixels to set scroll position above the currently selected group. If -1 scroll will be alligned to the table |
28 |
* General settings |
29 |
* @sDateFormat: "dd/MM/yyyy" String Date format used for grouping |
30 |
* @sEmptyGroupLabel String Lable that will be placed as group if grouping cells are empty. Default "-" |
31 |
|
32 |
* Parameters used in the second level grouping |
33 |
* @iGroupingColumnIndex2 Integer Index of the secondary column that will be used for grouping - default 0 |
34 |
* @sGroupingColumnSortDirection2 Enumeration Sort direction of the secondary group |
35 |
* @iGroupingOrderByColumnIndex2 Integer Index of the column that will be used for ordering secondary groups |
36 |
* @sGroupingClass2 String Class that will be associated to the secondary group row. Default "subgroup" |
37 |
* @sGroupItemClass2 String Class that will be associated to the secondary group row of group items. Default "subgroup-item" |
38 |
* @bHideGroupingColumn2 Boolean Hide column used for secondary grouping once results are grouped. Default - true, |
39 |
* @bHideGroupingOrderByColumn2 Boolean Hide column used for ordering secondary groups once results are grouped. Default - true, |
40 |
* @sGroupBy2 Enumeration Type of grouping that should be applied to secondary column. Values "name"(default), "letter", "year", |
41 |
* @sGroupLabelPrefix2 String Prefix that will be added to each secondary group cell |
42 |
* @fnOnGrouped Function Function that is called when grouping is finished. Function has no parameters. |
43 |
*/ |
44 |
(function ($) { |
45 |
|
46 |
"use strict"; |
47 |
|
48 |
$.fn.rowGrouping = function (options) { |
49 |
|
50 |
function _fnOnGrouped() { |
51 |
|
52 |
} |
53 |
|
54 |
function _fnOnGroupCreated(oGroup, sGroup, iLevel) { |
55 |
///<summary> |
56 |
///Function called when a new grouping row is created(it should be overriden in properties) |
57 |
///</summary> |
58 |
} |
59 |
|
60 |
function _fnOnGroupCompleted(oGroup, sGroup, iLevel) { |
61 |
///<summary> |
62 |
///Function called when a new grouping row is created(it should be overriden in properties) |
63 |
///</summary> |
64 |
} |
65 |
|
66 |
function _getMonthName(iMonth) { |
67 |
var asMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; |
68 |
return asMonths[iMonth - 1]; |
69 |
} |
70 |
|
71 |
var defaults = { |
72 |
|
73 |
iGroupingColumnIndex: 0, |
74 |
sGroupingColumnSortDirection: "", |
75 |
iGroupingOrderByColumnIndex: -1, |
76 |
sGroupingClass: "group", |
77 |
sGroupItemClass: "group-item", |
78 |
bHideGroupingColumn: true, |
79 |
bHideGroupingOrderByColumn: true, |
80 |
sGroupBy: "name", |
81 |
sGroupLabelPrefix: "", |
82 |
fnGroupLabelFormat: function (label) { return label; }, |
83 |
bExpandableGrouping: false, |
84 |
bExpandSingleGroup: false, |
85 |
iExpandGroupOffset: 100, |
86 |
asExpandedGroups: null, |
87 |
|
88 |
sDateFormat: "dd/MM/yyyy", |
89 |
sEmptyGroupLabel: "-", |
90 |
bSetGroupingClassOnTR: false, |
91 |
|
92 |
iGroupingColumnIndex2: -1, |
93 |
sGroupingColumnSortDirection2: "", |
94 |
iGroupingOrderByColumnIndex2: -1, |
95 |
sGroupingClass2: "subgroup", |
96 |
sGroupItemClass2: "subgroup-item", |
97 |
bHideGroupingColumn2: true, |
98 |
bHideGroupingOrderByColumn2: true, |
99 |
sGroupBy2: "name", |
100 |
sGroupLabelPrefix2: "", |
101 |
fnGroupLabelFormat2: function (label) { return label; }, |
102 |
bExpandableGrouping2: false, |
103 |
|
104 |
fnOnGrouped: _fnOnGrouped, |
105 |
|
106 |
fnOnGroupCreated: _fnOnGroupCreated, |
107 |
fnOnGroupCompleted: _fnOnGroupCompleted, |
108 |
|
109 |
oHideEffect: null, // { method: "hide", duration: "fast", easing: "linear" }, |
110 |
oShowEffect: null,//{ method: "show", duration: "slow", easing: "linear" } |
111 |
|
112 |
bUseFilteringForGrouping: false // This is still work in progress option |
113 |
}; |
114 |
return this.each(function (index, elem) { |
115 |
|
116 |
var oTable = $(elem).dataTable(); |
117 |
|
118 |
var aoGroups = new Array(); |
119 |
$(this).dataTableExt.aoGroups = aoGroups; |
120 |
|
121 |
function fnCreateGroupRow(sGroupCleaned, sGroup, iColspan) { |
122 |
var nGroup = document.createElement('tr'); |
123 |
var nCell = document.createElement('td'); |
124 |
nGroup.id = "group-id-" + oTable.attr("id") + "_" + sGroupCleaned; |
125 |
|
126 |
var oGroup = { id: nGroup.id, key: sGroupCleaned, text: sGroup, level: 0, groupItemClass: ".group-item-" + sGroupCleaned, dataGroup: sGroupCleaned, aoSubgroups: new Array() }; |
127 |
|
128 |
|
129 |
|
130 |
if (properties.bSetGroupingClassOnTR) { |
131 |
nGroup.className = properties.sGroupingClass + " " + sGroupCleaned; |
132 |
} else { |
133 |
nCell.className = properties.sGroupingClass + " " + sGroupCleaned; |
134 |
} |
135 |
|
136 |
nCell.colSpan = iColspan; |
137 |
nCell.innerHTML = properties.sGroupLabelPrefix + properties.fnGroupLabelFormat(sGroup == "" ? properties.sEmptyGroupLabel : sGroup, oGroup ); |
138 |
if (properties.bExpandableGrouping) { |
139 |
|
140 |
if (!_fnIsGroupCollapsed(sGroupCleaned)) { |
141 |
nCell.className += " expanded-group"; |
142 |
oGroup.state = "expanded"; |
143 |
} else { |
144 |
nCell.className += " collapsed-group"; |
145 |
oGroup.state = "collapsed"; |
146 |
} |
147 |
nCell.className += " group-item-expander"; |
148 |
$(nCell).attr('data-group', oGroup.dataGroup); //Fix provided by mssskhalsa (Issue 5) |
149 |
$(nCell).attr("data-group-level", oGroup.level); |
150 |
$(nCell).click(_fnOnGroupClick); |
151 |
} |
152 |
nGroup.appendChild(nCell); |
153 |
aoGroups[sGroupCleaned] = oGroup; |
154 |
oGroup.nGroup = nGroup; |
155 |
properties.fnOnGroupCreated(oGroup, sGroupCleaned, 1); |
156 |
return oGroup; |
157 |
} |
158 |
|
159 |
function _fnCreateGroup2Row(sGroup2, sGroupLabel, iColspan, oParentGroup) { |
160 |
|
161 |
var nGroup2 = document.createElement('tr'); |
162 |
nGroup2.id = oParentGroup.id + "_" + sGroup2; |
163 |
var nCell2 = document.createElement('td'); |
164 |
var dataGroup = oParentGroup.dataGroup + '_' + sGroup2; |
165 |
|
166 |
var oGroup = { id: nGroup2.id, key: sGroup2, text: sGroupLabel, level: oParentGroup.level + 1, groupItemClass: ".group-item-" + dataGroup, |
167 |
dataGroup: dataGroup, aoSubgroups: new Array() |
168 |
}; |
169 |
|
170 |
if (properties.bSetGroupingClassOnTR) { |
171 |
nGroup2.className = properties.sGroupingClass2 + " " + sGroup2; |
172 |
} else { |
173 |
nCell2.className = properties.sGroupingClass2 + " " + sGroup2; |
174 |
} |
175 |
|
176 |
nCell2.colSpan = iColspan; |
177 |
nCell2.innerHTML = properties.sGroupLabelPrefix2 + properties.fnGroupLabelFormat2(sGroupLabel == "" ? properties.sEmptyGroupLabel : sGroupLabel, oGroup); |
178 |
|
179 |
if (properties.bExpandableGrouping) { |
180 |
|
181 |
nGroup2.className += " group-item-" + oParentGroup.dataGroup; |
182 |
} |
183 |
|
184 |
|
185 |
if (properties.bExpandableGrouping && properties.bExpandableGrouping2) { |
186 |
|
187 |
if (!_fnIsGroupCollapsed(oGroup.dataGroup)) { |
188 |
nCell2.className += " expanded-group"; |
189 |
oGroup.state = "expanded"; |
190 |
} else { |
191 |
nCell2.className += " collapsed-group"; |
192 |
oGroup.state = "collapsed"; |
193 |
} |
194 |
nCell2.className += " group-item-expander"; |
195 |
$(nCell2).attr('data-group', oGroup.dataGroup); |
196 |
$(nCell2).attr("data-group-level", oGroup.level); |
197 |
$(nCell2).click(_fnOnGroupClick); |
198 |
} |
199 |
|
200 |
nGroup2.appendChild(nCell2); |
201 |
|
202 |
oParentGroup.aoSubgroups[oGroup.dataGroup] = oGroup; |
203 |
aoGroups[oGroup.dataGroup] = oGroup; |
204 |
oGroup.nGroup = nGroup2; |
205 |
properties.fnOnGroupCreated(oGroup, sGroup2, 2); |
206 |
return oGroup; |
207 |
} |
208 |
|
209 |
function _fnIsGroupCollapsed(sGroup) { |
210 |
if (aoGroups[sGroup] != null) |
211 |
return (aoGroups[sGroup].state == "collapsed"); |
212 |
else |
213 |
if (sGroup.indexOf("_") > -1) |
214 |
true; |
215 |
else |
216 |
if(bInitialGrouping && (asExpandedGroups==null || asExpandedGroups.length == 0)) |
217 |
return false;// initially if asExpandedGroups is empty - no one is collapsed |
218 |
else |
219 |
return ($.inArray(sGroup, asExpandedGroups) == -1); //the last chance check asExpandedGroups |
220 |
} |
221 |
|
222 |
function _fnGetYear(x) { |
223 |
if(x.length< (iYearIndex+iYearLength) ) |
224 |
return x; |
225 |
else |
226 |
return x.substr(iYearIndex, iYearLength); |
227 |
} |
228 |
function _fnGetGroupByName(x) { |
229 |
return x; |
230 |
} |
231 |
|
232 |
function _fnGetGroupByLetter(x) { |
233 |
return x.substr(0, 1); |
234 |
} |
235 |
|
236 |
function _fnGetGroupByYear(x) { |
237 |
return _fnGetYear(x); |
238 |
//return Date.parseExact(x, properties.sDateFormat).getFullYear();//slooooow |
239 |
} |
240 |
|
241 |
function _fnGetGroupByYearMonth(x) { |
242 |
//var date = Date.parseExact(x, "dd/MM/yyyy"); |
243 |
//return date.getFullYear() + " / " + date.getMonthName(); |
244 |
//return x.substr(iYearIndex, iYearLength) + '/' + x.substr(iMonthIndex, iMonthLength); |
245 |
return x.substr(iYearIndex, iYearLength) + ' ' + _getMonthName(x.substr(iMonthIndex, iMonthLength)); |
246 |
} |
247 |
|
248 |
function _fnGetCleanedGroup(sGroup) { |
249 |
|
250 |
if (sGroup === "") return "-"; |
251 |
return sGroup.toLowerCase().replace(/[^a-zA-Z0-9\u0080-\uFFFF]+/g, "-"); //fix for unicode characters (Issue 23) |
252 |
//return sGroup.toLowerCase().replace(/\W+/g, "-"); //Fix provided by bmathews (Issue 7) |
253 |
} |
254 |
|
255 |
function _rowGroupingRowFilter(oSettings, aData, iDataIndex) { |
256 |
///<summary>Used to expand/collapse groups with DataTables filtering</summary> |
257 |
if (oSettings.nTable.id !== oTable[0].id) return true; |
258 |
var sColData = aData[properties.iGroupingColumnIndex]; |
259 |
if (typeof sColData === "undefined") |
260 |
sColData = aData[oSettings.aoColumns[properties.iGroupingColumnIndex].mDataProp]; |
261 |
if (_fnIsGroupCollapsed(_fnGetCleanedGroup(sColData))) { |
262 |
if (oTable.fnIsOpen(oTable.fnGetNodes(iDataIndex))) |
263 |
{ |
264 |
if (properties.fnOnRowClosed != null) { |
265 |
properties.fnOnRowClosed(this); // $(this.cells[0].children[0]).attr('src', '../../Images/details.png'); |
266 |
} |
267 |
oTable.fnClose(oTable.fnGetNodes(iDataIndex)); |
268 |
} |
269 |
return false; |
270 |
}; |
271 |
return true; |
272 |
} //end of function _rowGroupingRowFilter |
273 |
|
274 |
|
275 |
function fnExpandGroup(sGroup) { |
276 |
///<summary>Expand group if expanadable grouping is used</summary> |
277 |
|
278 |
aoGroups[sGroup].state = "expanded"; |
279 |
|
280 |
$("td[data-group^='" + sGroup + "']").removeClass("collapsed-group"); |
281 |
$("td[data-group^='" + sGroup + "']").addClass("expanded-group"); |
282 |
|
283 |
|
284 |
if(properties.bUseFilteringForGrouping) |
285 |
{ |
286 |
oTable.fnDraw(); |
287 |
return;//Because rows are expanded with _rowGroupingRowFilter function |
288 |
} |
289 |
|
290 |
if (jQuery.inArray(sGroup, asExpandedGroups)==-1) |
291 |
asExpandedGroups.push(sGroup); |
292 |
|
293 |
if (properties.oHideEffect != null) |
294 |
$(".group-item-" + sGroup, oTable) |
295 |
[properties.oShowEffect.method](properties.oShowEffect.duration, |
296 |
properties.oShowEffect.easing, |
297 |
function () { }); |
298 |
else |
299 |
$(".group-item-" + sGroup, oTable).show(); |
300 |
|
301 |
|
302 |
} //end of function fnExpandGroup |
303 |
|
304 |
function fnCollapseGroup(sGroup) { |
305 |
///<summary>Collapse group if expanadable grouping is used</summary> |
306 |
|
307 |
aoGroups[sGroup].state = "collapsed"; |
308 |
$("td[data-group^='" + sGroup + "']").removeClass("expanded-group"); |
309 |
$("td[data-group^='" + sGroup + "']").addClass("collapsed-group"); |
310 |
|
311 |
if(properties.bUseFilteringForGrouping) |
312 |
{ |
313 |
oTable.fnDraw(); |
314 |
return;//Because rows are expanded with _rowGroupingRowFilter function |
315 |
} |
316 |
//var index = $.inArray(sGroup, asExpandedGroups); |
317 |
//asExpandedGroups.splice(index, 1); |
318 |
|
319 |
$('.group-item-' + sGroup).each(function () { |
320 |
//Issue 24 - Patch provided by Bob Graham |
321 |
if (oTable.fnIsOpen(this)) { |
322 |
if (properties.fnOnRowClosed != null) { |
323 |
properties.fnOnRowClosed(this); // $(this.cells[0].children[0]).attr('src', '../../Images/details.png'); |
324 |
} |
325 |
oTable.fnClose(this); |
326 |
} |
327 |
}); |
328 |
|
329 |
if (properties.oHideEffect != null) |
330 |
$(".group-item-" + sGroup, oTable) |
331 |
[properties.oHideEffect.method](properties.oHideEffect.duration, |
332 |
properties.oHideEffect.easing, |
333 |
function () { }); |
334 |
else |
335 |
$(".group-item-" + sGroup, oTable).hide(); |
336 |
|
337 |
} //end of function fnCollapseGroup |
338 |
|
339 |
function _fnOnGroupClick(e) { |
340 |
///<summary> |
341 |
///Function that is called when user click on the group cell in order to |
342 |
///expand of collapse group |
343 |
///</summary> |
344 |
|
345 |
//var sGroup = $(this).attr("rel"); |
346 |
var sGroup = $(this).attr("data-group"); |
347 |
var iGroupLevel = $(this).attr("data-group-level"); |
348 |
|
349 |
var bIsExpanded = !_fnIsGroupCollapsed(sGroup); |
350 |
if (properties.bExpandSingleGroup) { |
351 |
if (!bIsExpanded) { |
352 |
var sCurrentGroup = $("td.expanded-group").attr("data-group"); |
353 |
fnCollapseGroup(sCurrentGroup); |
354 |
fnExpandGroup(sGroup); |
355 |
|
356 |
if (properties.iExpandGroupOffset != -1) { |
357 |
var position = $("#group-id-" + oTable.attr("id") + "_" + sGroup).offset().top - properties.iExpandGroupOffset; |
358 |
window.scroll(0, position); |
359 |
} else { |
360 |
var position = oTable.offset().top; |
361 |
window.scroll(0, position); |
362 |
} |
363 |
} |
364 |
} else { |
365 |
if (bIsExpanded) { |
366 |
fnCollapseGroup(sGroup); |
367 |
} else { |
368 |
fnExpandGroup(sGroup); |
369 |
} |
370 |
} |
371 |
e.preventDefault(); |
372 |
|
373 |
}; //end function _fnOnGroupClick |
374 |
|
375 |
|
376 |
function _fnDrawCallBackWithGrouping (oSettings) { |
377 |
|
378 |
if (oTable.fnSettings().oFeatures.bServerSide) |
379 |
bInitialGrouping = true; |
380 |
var bUseSecondaryGrouping = false; |
381 |
|
382 |
if (properties.iGroupingColumnIndex2 != -1) |
383 |
bUseSecondaryGrouping = true; |
384 |
|
385 |
//-----Start grouping |
386 |
|
387 |
if (oSettings.aiDisplayMaster.length == 0) { //aiDisplay |
388 |
return; |
389 |
} |
390 |
|
391 |
var nTrs = $('tbody tr', oTable); |
392 |
var iColspan = 0; //nTrs[0].getElementsByTagName('td').length; |
393 |
for (var iColIndex = 0; iColIndex < oSettings.aoColumns.length; iColIndex++) { |
394 |
if (oSettings.aoColumns[iColIndex].bVisible) |
395 |
iColspan += 1; |
396 |
} |
397 |
var sLastGroup = null; |
398 |
var sLastGroup2 = null; |
399 |
if (oSettings.aiDisplay.length > 0) { |
400 |
for (var i = 0; i < nTrs.length; i++) { |
401 |
|
402 |
|
403 |
var iDisplayIndex = oSettings._iDisplayStart + i; |
404 |
if (oTable.fnSettings().oFeatures.bServerSide) |
405 |
iDisplayIndex = i; |
406 |
var sGroupData = ""; |
407 |
var sGroup = null; |
408 |
var sGroupData2 = ""; |
409 |
var sGroup2 = null; |
410 |
|
411 |
//Issue 31 - Start fix provided by Fabien Taysse |
412 |
// sGroupData = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[properties.iGroupingColumnIndex]; |
413 |
// if (sGroupData == undefined) |
414 |
// sGroupData = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[oSettings.aoColumns[properties.iGroupingColumnIndex].mDataProp]; |
415 |
sGroupData = this.fnGetData(nTrs[i], properties.iGroupingColumnIndex); |
416 |
//Issue 31 - End fix provided by Fabien Taysse |
417 |
|
418 |
var sGroup = sGroupData; |
419 |
if (properties.sGroupBy != "year") |
420 |
sGroup = fnGetGroup(sGroupData); |
421 |
|
422 |
if (bUseSecondaryGrouping) { |
423 |
sGroupData2 = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[properties.iGroupingColumnIndex2]; |
424 |
if (sGroupData2 == undefined) |
425 |
sGroupData2 = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[oSettings.aoColumns[properties.iGroupingColumnIndex2].mDataProp]; |
426 |
if (properties.sGroupBy2 != "year") |
427 |
sGroup2 = fnGetGroup(sGroupData2); |
428 |
} |
429 |
|
430 |
|
431 |
if (sLastGroup == null || _fnGetCleanedGroup(sGroup) != _fnGetCleanedGroup(sLastGroup)) { // new group encountered (or first of group) |
432 |
var sGroupCleaned = _fnGetCleanedGroup(sGroup); |
433 |
|
434 |
if(sLastGroup != null) |
435 |
{ |
436 |
properties.fnOnGroupCompleted(aoGroups[_fnGetCleanedGroup(sLastGroup)]); |
437 |
} |
438 |
/* |
439 |
if (properties.bExpandableGrouping && bInitialGrouping) { |
440 |
if (properties.bExpandSingleGroup) { |
441 |
if (asExpandedGroups.length == 0) |
442 |
asExpandedGroups.push(sGroupCleaned); |
443 |
} else { |
444 |
asExpandedGroups.push(sGroupCleaned); |
445 |
} |
446 |
} |
447 |
*/ |
448 |
if(properties.bAddAllGroupsAsExpanded && jQuery.inArray(sGroupCleaned,asExpandedGroups) == -1) |
449 |
asExpandedGroups.push(sGroupCleaned); |
450 |
|
451 |
var oGroup = fnCreateGroupRow(sGroupCleaned, sGroup, iColspan); |
452 |
var nGroup = oGroup.nGroup; |
453 |
|
454 |
if(nTrs[i].parentNode!=null) |
455 |
nTrs[i].parentNode.insertBefore(nGroup, nTrs[i]); |
456 |
else |
457 |
$(nTrs[i]).before(nGroup); |
458 |
|
459 |
sLastGroup = sGroup; |
460 |
sLastGroup2 = null; //to reset second level grouping |
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
} // end if (sLastGroup == null || sGroup != sLastGroup) |
467 |
|
468 |
$(nTrs[i]).attr("data-group", aoGroups[sGroupCleaned].dataGroup); |
469 |
|
470 |
$(nTrs[i]).addClass(properties.sGroupItemClass); |
471 |
$(nTrs[i]).addClass("group-item-" + sGroupCleaned); |
472 |
if (properties.bExpandableGrouping) { |
473 |
if (_fnIsGroupCollapsed(sGroupCleaned) && !properties.bUseFilteringForGrouping) { |
474 |
$(nTrs[i]).hide(); |
475 |
} |
476 |
} |
477 |
|
478 |
|
479 |
if (bUseSecondaryGrouping) { |
480 |
|
481 |
if (sLastGroup2 == null || _fnGetCleanedGroup(sGroup2) != _fnGetCleanedGroup(sLastGroup2)) { |
482 |
var sGroup2Id = _fnGetCleanedGroup(sGroup) + '-' + _fnGetCleanedGroup(sGroup2); |
483 |
var oGroup2 = _fnCreateGroup2Row(sGroup2Id, sGroup2, iColspan, aoGroups[sGroupCleaned]) |
484 |
var nGroup2 = oGroup2.nGroup; |
485 |
nTrs[i].parentNode.insertBefore(nGroup2, nTrs[i]); |
486 |
|
487 |
sLastGroup2 = sGroup2; |
488 |
} |
489 |
|
490 |
$(nTrs[i]).attr("data-group", oGroup2.dataGroup) |
491 |
.addClass(properties.sGroupItemClass2) |
492 |
.addClass("group-item-" + oGroup2.dataGroup); |
493 |
} //end if (bUseSecondaryGrouping) |
494 |
|
495 |
|
496 |
|
497 |
} // end for (var i = 0; i < nTrs.length; i++) |
498 |
}; // if (oSettings.aiDisplay.length > 0) |
499 |
|
500 |
if(sLastGroup != null) |
501 |
{ |
502 |
properties.fnOnGroupCompleted(aoGroups[_fnGetCleanedGroup(sLastGroup)]); |
503 |
} |
504 |
|
505 |
|
506 |
//-----End grouping |
507 |
properties.fnOnGrouped(aoGroups); |
508 |
|
509 |
bInitialGrouping = false; |
510 |
|
511 |
}; // end of _fnDrawCallBackWithGrouping = function (oSettings) |
512 |
|
513 |
|
514 |
//var oTable = this; |
515 |
var iYearIndex = 6; |
516 |
var iYearLength = 4; |
517 |
var asExpandedGroups = new Array(); |
518 |
var bInitialGrouping = true; |
519 |
|
520 |
var properties = $.extend(defaults, options); |
521 |
|
522 |
if (properties.iGroupingOrderByColumnIndex == -1) { |
523 |
properties.bCustomColumnOrdering = false; |
524 |
properties.iGroupingOrderByColumnIndex = properties.iGroupingColumnIndex; |
525 |
} else { |
526 |
properties.bCustomColumnOrdering = true; |
527 |
} |
528 |
|
529 |
if (properties.sGroupingColumnSortDirection == "") { |
530 |
if (properties.sGroupBy == "year") |
531 |
properties.sGroupingColumnSortDirection = "desc"; |
532 |
else |
533 |
properties.sGroupingColumnSortDirection = "asc"; |
534 |
} |
535 |
|
536 |
|
537 |
if (properties.iGroupingOrderByColumnIndex2 == -1) { |
538 |
properties.bCustomColumnOrdering2 = false; |
539 |
properties.iGroupingOrderByColumnIndex2 = properties.iGroupingColumnIndex2; |
540 |
} else { |
541 |
properties.bCustomColumnOrdering2 = true; |
542 |
} |
543 |
|
544 |
if (properties.sGroupingColumnSortDirection2 == "") { |
545 |
if (properties.sGroupBy2 == "year") |
546 |
properties.sGroupingColumnSortDirection2 = "desc"; |
547 |
else |
548 |
properties.sGroupingColumnSortDirection2 = "asc"; |
549 |
} |
550 |
|
551 |
|
552 |
|
553 |
iYearIndex = properties.sDateFormat.toLowerCase().indexOf('yy'); |
554 |
iYearLength = properties.sDateFormat.toLowerCase().lastIndexOf('y') - properties.sDateFormat.toLowerCase().indexOf('y') + 1; |
555 |
|
556 |
var iMonthIndex = properties.sDateFormat.toLowerCase().indexOf('mm'); |
557 |
var iMonthLength = properties.sDateFormat.toLowerCase().lastIndexOf('m') - properties.sDateFormat.toLowerCase().indexOf('m') + 1; |
558 |
|
559 |
var fnGetGroup = _fnGetGroupByName; |
560 |
switch (properties.sGroupBy) { |
561 |
case "letter": fnGetGroup = _fnGetGroupByLetter; |
562 |
break; |
563 |
case "year": fnGetGroup = _fnGetGroupByYear; |
564 |
break; |
565 |
case "month": fnGetGroup = _fnGetGroupByYearMonth; |
566 |
break; |
567 |
default: fnGetGroup = _fnGetGroupByName; |
568 |
break; |
569 |
} |
570 |
|
571 |
|
572 |
if (properties.asExpandedGroups != null) { |
573 |
if (properties.asExpandedGroups == "NONE") { |
574 |
properties.asExpandedGroups = []; |
575 |
asExpandedGroups = properties.asExpandedGroups; |
576 |
bInitialGrouping = false; |
577 |
} else if (properties.asExpandedGroups == "ALL") { |
578 |
properties.bAddAllGroupsAsExpanded = true; |
579 |
} else if (properties.asExpandedGroups.constructor == String) { |
580 |
var currentGroup = properties.asExpandedGroups; |
581 |
properties.asExpandedGroups = new Array(); |
582 |
properties.asExpandedGroups.push(_fnGetCleanedGroup(currentGroup)); |
583 |
asExpandedGroups = properties.asExpandedGroups; |
584 |
bInitialGrouping = false; |
585 |
} else if (properties.asExpandedGroups.constructor == Array) { |
586 |
for (var i = 0; i < properties.asExpandedGroups.length; i++) { |
587 |
asExpandedGroups.push(_fnGetCleanedGroup(properties.asExpandedGroups[i])); |
588 |
if (properties.bExpandSingleGroup) |
589 |
break; |
590 |
} |
591 |
bInitialGrouping = false; |
592 |
} |
593 |
}else{ |
594 |
properties.asExpandedGroups = new Array(); |
595 |
properties.bAddAllGroupsAsExpanded = true; |
596 |
} |
597 |
if(properties.bExpandSingleGroup){ |
598 |
var nTrs = $('tbody tr', oTable); |
599 |
var sGroupData = oTable.fnGetData(nTrs[0], properties.iGroupingColumnIndex); |
600 |
|
601 |
var sGroup = sGroupData; |
602 |
if (properties.sGroupBy != "year") |
603 |
sGroup = fnGetGroup(sGroupData); |
604 |
|
605 |
var sGroupCleaned = _fnGetCleanedGroup(sGroup); |
606 |
properties.asExpandedGroups = new Array(); |
607 |
properties.asExpandedGroups.push(sGroupCleaned); |
608 |
|
609 |
} |
610 |
|
611 |
oTable.fnSetColumnVis(properties.iGroupingColumnIndex, !properties.bHideGroupingColumn); |
612 |
if (properties.bCustomColumnOrdering) { |
613 |
oTable.fnSetColumnVis(properties.iGroupingOrderByColumnIndex, !properties.bHideGroupingOrderByColumn); |
614 |
} |
615 |
if (properties.iGroupingColumnIndex2 != -1) { |
616 |
oTable.fnSetColumnVis(properties.iGroupingColumnIndex2, !properties.bHideGroupingColumn2); |
617 |
} |
618 |
if (properties.bCustomColumnOrdering2) { |
619 |
oTable.fnSetColumnVis(properties.iGroupingOrderByColumnIndex2, !properties.bHideGroupingOrderByColumn2); |
620 |
} |
621 |
oTable.fnSettings().aoDrawCallback.push({ |
622 |
"fn": _fnDrawCallBackWithGrouping, |
623 |
"sName": "fnRowGrouping" |
624 |
}); |
625 |
|
626 |
var aaSortingFixed = new Array(); |
627 |
aaSortingFixed.push([properties.iGroupingOrderByColumnIndex, properties.sGroupingColumnSortDirection]); |
628 |
if (properties.iGroupingColumnIndex2 != -1) { |
629 |
aaSortingFixed.push([properties.iGroupingOrderByColumnIndex2, properties.sGroupingColumnSortDirection2]); |
630 |
} // end of if (properties.iGroupingColumnIndex2 != -1) |
631 |
|
632 |
oTable.fnSettings().aaSortingFixed = aaSortingFixed; |
633 |
//Old way |
634 |
//oTable.fnSettings().aaSortingFixed = [[properties.iGroupingOrderByColumnIndex, properties.sGroupingColumnSortDirection]]; |
635 |
|
636 |
switch (properties.sGroupBy) { |
637 |
case "name": |
638 |
break; |
639 |
|
640 |
|
641 |
case "letter": |
642 |
|
643 |
/* Create an array with the values of all the input boxes in a column */ |
644 |
oTable.fnSettings().aoColumns[properties.iGroupingOrderByColumnIndex].sSortDataType = "rg-letter"; |
645 |
$.fn.dataTableExt.afnSortData['rg-letter'] = function (oSettings, iColumn) { |
646 |
var aData = []; |
647 |
$('td:eq(' + iColumn + ')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () { |
648 |
aData.push(_fnGetGroupByLetter(this.innerHTML)); |
649 |
}); |
650 |
return aData; |
651 |
} |
652 |
|
653 |
|
654 |
break; |
655 |
|
656 |
|
657 |
case "year": |
658 |
/* Create an array with the values of all the input boxes in a column */ |
659 |
oTable.fnSettings().aoColumns[properties.iGroupingOrderByColumnIndex].sSortDataType = "rg-date"; |
660 |
$.fn.dataTableExt.afnSortData['rg-date'] = function (oSettings, iColumn) { |
661 |
var aData = []; |
662 |
var nTrs = oSettings.oApi._fnGetTrNodes(oSettings); |
663 |
for(i = 0; i< nTrs.length; i++) |
664 |
{ |
665 |
aData.push(_fnGetYear( oTable.fnGetData( nTrs[i], iColumn) )); |
666 |
} |
667 |
|
668 |
/* |
669 |
$('td:eq(' + iColumn + ')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () { |
670 |
aData.push(_fnGetYear(this.innerHTML)); |
671 |
}); |
672 |
*/ |
673 |
return aData; |
674 |
} |
675 |
break; |
676 |
default: |
677 |
break; |
678 |
|
679 |
} // end of switch (properties.sGroupBy) |
680 |
|
681 |
if(properties.bUseFilteringForGrouping) |
682 |
$.fn.dataTableExt.afnFiltering.push(_rowGroupingRowFilter); |
683 |
|
684 |
oTable.fnDraw(); |
685 |
|
686 |
|
687 |
|
688 |
}); |
689 |
}; |
690 |
})(jQuery); |