| 776 |
lars |
1 |
/***
|
|
|
2 |
Wrapper/Helper Class for datagrid based on jQuery Datatable Plugin
|
|
|
3 |
***/
|
|
|
4 |
var Datatable = function() {
|
|
|
5 |
|
|
|
6 |
var tableOptions; // main options
|
|
|
7 |
var dataTable; // datatable object
|
|
|
8 |
var table; // actual table jquery object
|
|
|
9 |
var tableContainer; // actual table container object
|
|
|
10 |
var tableWrapper; // actual table wrapper jquery object
|
|
|
11 |
var tableInitialized = false;
|
|
|
12 |
var ajaxParams = {}; // set filter mode
|
|
|
13 |
var the;
|
|
|
14 |
|
|
|
15 |
var countSelectedRecords = function() {
|
|
|
16 |
var selected = $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
|
|
|
17 |
var text = tableOptions.dataTable.language.metronicGroupActions;
|
|
|
18 |
if (selected > 0) {
|
|
|
19 |
$('.table-group-actions > span', tableWrapper).text(text.replace("_TOTAL_", selected));
|
|
|
20 |
} else {
|
|
|
21 |
$('.table-group-actions > span', tableWrapper).text("");
|
|
|
22 |
}
|
|
|
23 |
};
|
|
|
24 |
|
|
|
25 |
return {
|
|
|
26 |
|
|
|
27 |
//main function to initiate the module
|
|
|
28 |
init: function(options) {
|
|
|
29 |
|
|
|
30 |
if (!$().dataTable) {
|
|
|
31 |
return;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
the = this;
|
|
|
35 |
|
|
|
36 |
// default settings
|
|
|
37 |
options = $.extend(true, {
|
|
|
38 |
src: "", // actual table
|
|
|
39 |
filterApplyAction: "filter",
|
|
|
40 |
filterCancelAction: "filter_cancel",
|
|
|
41 |
resetGroupActionInputOnSuccess: true,
|
|
|
42 |
loadingMessage: 'Loading...',
|
|
|
43 |
dataTable: {
|
|
|
44 |
"dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r><'table-responsive't><'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>", // datatable layout
|
|
|
45 |
"pageLength": 10, // default records per page
|
|
|
46 |
"language": { // language settings
|
|
|
47 |
// metronic spesific
|
|
|
48 |
"metronicGroupActions": "_TOTAL_ records selected: ",
|
|
|
49 |
"metronicAjaxRequestGeneralError": "Could not complete request. Please check your internet connection",
|
|
|
50 |
|
|
|
51 |
// data tables spesific
|
|
|
52 |
"lengthMenu": "<span class='seperator'>|</span>View _MENU_ records",
|
|
|
53 |
"info": "<span class='seperator'>|</span>Found total _TOTAL_ records",
|
|
|
54 |
"infoEmpty": "No records found to show",
|
|
|
55 |
"emptyTable": "No data available in table",
|
|
|
56 |
"zeroRecords": "No matching records found",
|
|
|
57 |
"paginate": {
|
|
|
58 |
"previous": "Prev",
|
|
|
59 |
"next": "Next",
|
|
|
60 |
"last": "Last",
|
|
|
61 |
"first": "First",
|
|
|
62 |
"page": "Page",
|
|
|
63 |
"pageOf": "of"
|
|
|
64 |
}
|
|
|
65 |
},
|
|
|
66 |
|
|
|
67 |
"orderCellsTop": true,
|
|
|
68 |
"columnDefs": [{ // define columns sorting options(by default all columns are sortable extept the first checkbox column)
|
|
|
69 |
'orderable': false,
|
|
|
70 |
'targets': [0]
|
|
|
71 |
}],
|
|
|
72 |
|
|
|
73 |
"pagingType": "bootstrap_extended", // pagination type(bootstrap, bootstrap_full_number or bootstrap_extended)
|
|
|
74 |
"autoWidth": false, // disable fixed width and enable fluid table
|
|
|
75 |
"processing": false, // enable/disable display message box on record load
|
|
|
76 |
"serverSide": true, // enable/disable server side ajax loading
|
|
|
77 |
|
|
|
78 |
"ajax": { // define ajax settings
|
|
|
79 |
"url": "", // ajax URL
|
|
|
80 |
"type": "POST", // request type
|
|
|
81 |
"timeout": 20000,
|
|
|
82 |
"data": function(data) { // add request parameters before submit
|
|
|
83 |
$.each(ajaxParams, function(key, value) {
|
|
|
84 |
data[key] = value;
|
|
|
85 |
});
|
|
|
86 |
App.blockUI({
|
|
|
87 |
message: tableOptions.loadingMessage,
|
|
|
88 |
target: tableContainer,
|
|
|
89 |
overlayColor: 'none',
|
|
|
90 |
cenrerY: true,
|
|
|
91 |
boxed: true
|
|
|
92 |
});
|
|
|
93 |
},
|
|
|
94 |
"dataSrc": function(res) { // Manipulate the data returned from the server
|
|
|
95 |
if (res.customActionMessage) {
|
|
|
96 |
App.alert({
|
|
|
97 |
type: (res.customActionStatus == 'OK' ? 'success' : 'danger'),
|
|
|
98 |
icon: (res.customActionStatus == 'OK' ? 'check' : 'warning'),
|
|
|
99 |
message: res.customActionMessage,
|
|
|
100 |
container: tableWrapper,
|
|
|
101 |
place: 'prepend'
|
|
|
102 |
});
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
if (res.customActionStatus) {
|
|
|
106 |
if (tableOptions.resetGroupActionInputOnSuccess) {
|
|
|
107 |
$('.table-group-action-input', tableWrapper).val("");
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if ($('.group-checkable', table).size() === 1) {
|
|
|
112 |
$('.group-checkable', table).attr("checked", false);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (tableOptions.onSuccess) {
|
|
|
116 |
tableOptions.onSuccess.call(undefined, the, res);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
App.unblockUI(tableContainer);
|
|
|
120 |
|
|
|
121 |
return res.data;
|
|
|
122 |
},
|
|
|
123 |
"error": function() { // handle general connection errors
|
|
|
124 |
if (tableOptions.onError) {
|
|
|
125 |
tableOptions.onError.call(undefined, the);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
App.alert({
|
|
|
129 |
type: 'danger',
|
|
|
130 |
icon: 'warning',
|
|
|
131 |
message: tableOptions.dataTable.language.metronicAjaxRequestGeneralError,
|
|
|
132 |
container: tableWrapper,
|
|
|
133 |
place: 'prepend'
|
|
|
134 |
});
|
|
|
135 |
|
|
|
136 |
App.unblockUI(tableContainer);
|
|
|
137 |
}
|
|
|
138 |
},
|
|
|
139 |
|
|
|
140 |
"drawCallback": function(oSettings) { // run some code on table redraw
|
|
|
141 |
if (tableInitialized === false) { // check if table has been initialized
|
|
|
142 |
tableInitialized = true; // set table initialized
|
|
|
143 |
table.show(); // display table
|
|
|
144 |
}
|
|
|
145 |
countSelectedRecords(); // reset selected records indicator
|
|
|
146 |
|
|
|
147 |
// callback for ajax data load
|
|
|
148 |
if (tableOptions.onDataLoad) {
|
|
|
149 |
tableOptions.onDataLoad.call(undefined, the);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
}, options);
|
|
|
154 |
|
|
|
155 |
tableOptions = options;
|
|
|
156 |
|
|
|
157 |
// create table's jquery object
|
|
|
158 |
table = $(options.src);
|
|
|
159 |
tableContainer = table.parents(".table-container");
|
|
|
160 |
|
|
|
161 |
// apply the special class that used to restyle the default datatable
|
|
|
162 |
var tmp = $.fn.dataTableExt.oStdClasses;
|
|
|
163 |
|
|
|
164 |
$.fn.dataTableExt.oStdClasses.sWrapper = $.fn.dataTableExt.oStdClasses.sWrapper + " dataTables_extended_wrapper";
|
|
|
165 |
$.fn.dataTableExt.oStdClasses.sFilterInput = "form-control input-xs input-sm input-inline";
|
|
|
166 |
$.fn.dataTableExt.oStdClasses.sLengthSelect = "form-control input-xs input-sm input-inline";
|
|
|
167 |
|
|
|
168 |
// initialize a datatable
|
|
|
169 |
dataTable = table.DataTable(options.dataTable);
|
|
|
170 |
|
|
|
171 |
// revert back to default
|
|
|
172 |
$.fn.dataTableExt.oStdClasses.sWrapper = tmp.sWrapper;
|
|
|
173 |
$.fn.dataTableExt.oStdClasses.sFilterInput = tmp.sFilterInput;
|
|
|
174 |
$.fn.dataTableExt.oStdClasses.sLengthSelect = tmp.sLengthSelect;
|
|
|
175 |
|
|
|
176 |
// get table wrapper
|
|
|
177 |
tableWrapper = table.parents('.dataTables_wrapper');
|
|
|
178 |
|
|
|
179 |
// build table group actions panel
|
|
|
180 |
if ($('.table-actions-wrapper', tableContainer).size() === 1) {
|
|
|
181 |
$('.table-group-actions', tableWrapper).html($('.table-actions-wrapper', tableContainer).html()); // place the panel inside the wrapper
|
|
|
182 |
$('.table-actions-wrapper', tableContainer).remove(); // remove the template container
|
|
|
183 |
}
|
|
|
184 |
// handle group checkboxes check/uncheck
|
|
|
185 |
$('.group-checkable', table).change(function() {
|
|
|
186 |
var set = table.find('tbody > tr > td:nth-child(1) input[type="checkbox"]');
|
|
|
187 |
var checked = $(this).prop("checked");
|
|
|
188 |
$(set).each(function() {
|
|
|
189 |
$(this).prop("checked", checked);
|
|
|
190 |
});
|
|
|
191 |
countSelectedRecords();
|
|
|
192 |
});
|
|
|
193 |
|
|
|
194 |
// handle row's checkbox click
|
|
|
195 |
table.on('change', 'tbody > tr > td:nth-child(1) input[type="checkbox"]', function() {
|
|
|
196 |
countSelectedRecords();
|
|
|
197 |
});
|
|
|
198 |
|
|
|
199 |
// handle filter submit button click
|
|
|
200 |
table.on('click', '.filter-submit', function(e) {
|
|
|
201 |
e.preventDefault();
|
|
|
202 |
the.submitFilter();
|
|
|
203 |
});
|
|
|
204 |
|
|
|
205 |
// handle filter cancel button click
|
|
|
206 |
table.on('click', '.filter-cancel', function(e) {
|
|
|
207 |
e.preventDefault();
|
|
|
208 |
the.resetFilter();
|
|
|
209 |
});
|
|
|
210 |
},
|
|
|
211 |
|
|
|
212 |
submitFilter: function() {
|
|
|
213 |
the.setAjaxParam("action", tableOptions.filterApplyAction);
|
|
|
214 |
|
|
|
215 |
// get all typeable inputs
|
|
|
216 |
$('textarea.form-filter, select.form-filter, input.form-filter:not([type="radio"],[type="checkbox"])', table).each(function() {
|
|
|
217 |
the.setAjaxParam($(this).attr("name"), $(this).val());
|
|
|
218 |
});
|
|
|
219 |
|
|
|
220 |
// get all checkboxes
|
|
|
221 |
$('input.form-filter[type="checkbox"]:checked', table).each(function() {
|
|
|
222 |
the.addAjaxParam($(this).attr("name"), $(this).val());
|
|
|
223 |
});
|
|
|
224 |
|
|
|
225 |
// get all radio buttons
|
|
|
226 |
$('input.form-filter[type="radio"]:checked', table).each(function() {
|
|
|
227 |
the.setAjaxParam($(this).attr("name"), $(this).val());
|
|
|
228 |
});
|
|
|
229 |
|
|
|
230 |
dataTable.ajax.reload();
|
|
|
231 |
},
|
|
|
232 |
|
|
|
233 |
resetFilter: function() {
|
|
|
234 |
$('textarea.form-filter, select.form-filter, input.form-filter', table).each(function() {
|
|
|
235 |
$(this).val("");
|
|
|
236 |
});
|
|
|
237 |
$('input.form-filter[type="checkbox"]', table).each(function() {
|
|
|
238 |
$(this).attr("checked", false);
|
|
|
239 |
});
|
|
|
240 |
the.clearAjaxParams();
|
|
|
241 |
the.addAjaxParam("action", tableOptions.filterCancelAction);
|
|
|
242 |
dataTable.ajax.reload();
|
|
|
243 |
},
|
|
|
244 |
|
|
|
245 |
getSelectedRowsCount: function() {
|
|
|
246 |
return $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
|
|
|
247 |
},
|
|
|
248 |
|
|
|
249 |
getSelectedRows: function() {
|
|
|
250 |
var rows = [];
|
|
|
251 |
$('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).each(function() {
|
|
|
252 |
rows.push($(this).val());
|
|
|
253 |
});
|
|
|
254 |
|
|
|
255 |
return rows;
|
|
|
256 |
},
|
|
|
257 |
|
|
|
258 |
setAjaxParam: function(name, value) {
|
|
|
259 |
ajaxParams[name] = value;
|
|
|
260 |
},
|
|
|
261 |
|
|
|
262 |
addAjaxParam: function(name, value) {
|
|
|
263 |
if (!ajaxParams[name]) {
|
|
|
264 |
ajaxParams[name] = [];
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
skip = false;
|
|
|
268 |
for (var i = 0; i < (ajaxParams[name]).length; i++) { // check for duplicates
|
|
|
269 |
if (ajaxParams[name][i] === value) {
|
|
|
270 |
skip = true;
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
if (skip === false) {
|
|
|
275 |
ajaxParams[name].push(value);
|
|
|
276 |
}
|
|
|
277 |
},
|
|
|
278 |
|
|
|
279 |
clearAjaxParams: function(name, value) {
|
|
|
280 |
ajaxParams = {};
|
|
|
281 |
},
|
|
|
282 |
|
|
|
283 |
getDataTable: function() {
|
|
|
284 |
return dataTable;
|
|
|
285 |
},
|
|
|
286 |
|
|
|
287 |
getTableWrapper: function() {
|
|
|
288 |
return tableWrapper;
|
|
|
289 |
},
|
|
|
290 |
|
|
|
291 |
gettableContainer: function() {
|
|
|
292 |
return tableContainer;
|
|
|
293 |
},
|
|
|
294 |
|
|
|
295 |
getTable: function() {
|
|
|
296 |
return table;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
};
|
|
|
300 |
|
|
|
301 |
};
|