1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/************************************************************************
* MASTER/CHILD tables extension for jTable *
*************************************************************************/
(function ($) {
//Reference to base object members
var base = {
_removeRowsFromTable: $.hik.jtable.prototype._removeRowsFromTable
};
//extension members
$.extend(true, $.hik.jtable.prototype, {
/************************************************************************
* DEFAULT OPTIONS / EVENTS *
*************************************************************************/
options: {
openChildAsAccordion: false
},
/************************************************************************
* PUBLIC METHODS *
*************************************************************************/
/* Creates and opens a new child table for given row.
*************************************************************************/
openChildTable: function ($row, tableOptions, opened) {
var self = this;
//Apply theming as same as parent table unless explicitily set
if (tableOptions.jqueryuiTheme == undefined) {
tableOptions.jqueryuiTheme = self.options.jqueryuiTheme;
}
//Show close button as default
tableOptions.showCloseButton = (tableOptions.showCloseButton != false);
//Close child table when close button is clicked (default behavior)
if (tableOptions.showCloseButton && !tableOptions.closeRequested) {
tableOptions.closeRequested = function () {
self.closeChildTable($row);
};
}
//If accordion style, close open child table (if it does exists)
if (self.options.openChildAsAccordion) {
$row.siblings('.jtable-data-row').each(function () {
self.closeChildTable($(this));
});
}
//Close child table for this row and open new one for child table
self.closeChildTable($row, function () {
var $childRowColumn = self.getChildRow($row).children('td').empty();
var $childTableContainer = $('<div />')
.addClass('jtable-child-table-container')
.appendTo($childRowColumn);
$childRowColumn.data('childTable', $childTableContainer);
$childTableContainer.jtable(tableOptions);
self.openChildRow($row);
$childTableContainer.hide().slideDown('fast', function () {
if (opened) {
opened({
childTable: $childTableContainer
});
}
});
});
},
/* Closes child table for given row.
*************************************************************************/
closeChildTable: function ($row, closed) {
var self = this;
var $childRowColumn = this.getChildRow($row).children('td');
var $childTable = $childRowColumn.data('childTable');
if (!$childTable) {
if (closed) {
closed();
}
return;
}
$childRowColumn.data('childTable', null);
$childTable.slideUp('fast', function () {
$childTable.jtable('destroy');
$childTable.remove();
self.closeChildRow($row);
if (closed) {
closed();
}
});
},
/* Returns a boolean value indicates that if a child row is open for given row.
*************************************************************************/
isChildRowOpen: function ($row) {
return (this.getChildRow($row).is(':visible'));
},
/* Gets child row for given row, opens it if it's closed (Creates if needed).
*************************************************************************/
getChildRow: function ($row) {
return $row.data('childRow') || this._createChildRow($row);
},
/* Creates and opens child row for given row.
*************************************************************************/
openChildRow: function ($row) {
var $childRow = this.getChildRow($row);
if (!$childRow.is(':visible')) {
$childRow.show();
}
return $childRow;
},
/* Closes child row if it's open.
*************************************************************************/
closeChildRow: function ($row) {
var $childRow = this.getChildRow($row);
if ($childRow.is(':visible')) {
$childRow.hide();
}
},
/************************************************************************
* OVERRIDED METHODS *
*************************************************************************/
/* Overrides _removeRowsFromTable method to remove child rows of deleted rows.
*************************************************************************/
_removeRowsFromTable: function ($rows, reason) {
//var self = this;
if (reason == 'deleted') {
$rows.each(function () {
var $row = $(this);
var $childRow = $row.data('childRow');
if ($childRow) {
//self.closeChildTable($row); //Removed since it causes "Uncaught Error: cannot call methods on jtable prior to initialization; attempted to call method 'destroy'"
$childRow.remove();
}
});
}
base._removeRowsFromTable.apply(this, arguments);
},
/************************************************************************
* PRIVATE METHODS *
*************************************************************************/
/* Creates a child row for a row, hides and returns it.
*************************************************************************/
_createChildRow: function ($row) {
var totalColumnCount = this._$table.find('thead th').length;
var $childRow = $('<tr></tr>')
.addClass('jtable-child-row')
.append('<td colspan="' + totalColumnCount + '"></td>');
$row.after($childRow);
$row.data('childRow', $childRow);
$childRow.hide();
return $childRow;
}
});
})(jQuery);
|