-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTableReload.js
286 lines (279 loc) · 6.09 KB
/
DataTableReload.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;(function (){
'use strict';
/**
* dtReload is used for setting new param
* to send to the server with out having
* to reload the table
*/
var dtReload = {};
/**
* This is the object containing
* the param to be sent to the server
*/
dtReload.param = {};
/**
* Adds the param to the object used by datatables
* for the request
* @param param object datatables object
* @param extra object extra params to send to the server
* @return object
*/
dtReload.processParam = (param, extra) => {
$.each(extra, function(i,e){
param[i] = e;
})
return param;
}
/**
* @name DataTableReload
* @summary Allows for reloading the datatable with new param
* @author Kyle Coots
* @date 4-10-2022
* @namespace DataTableReload
* @copyright Kyle Coots
* @version 1.0
*/
class DataTableReload {
/**
* Url for the ajax request
* @type string
*/
#url = '';
/**
* Request Method for the ajax request.
* @type string
*/
#method = 'POST';
/**
* Options used for the DataTable
* @type object
*/
#dtOptions = {};
/**
* Responseive Table
* @type boolean
*/
#responsive = true;
/**
* Table Page Length
* @type interger
*/
#pageLength = 10;
/**
* Table Page Length Menu
* @type array
*/
#lengthMenu = [5, 10, 20];
/**
* Table Processing
* @type boolean
*/
#processing = true;
/**
* Table Use Server Side
* @type boolean
*/
#serverSide = true;
/**
* Table Language Options
* @type object
*/
#language = {};
/**
* Table Initalization Complete
* A function that allows for processing after
* the table has loaded
* @type function
*/
#initComplete;
/**
* Table Columns
* @type array
*/
#columns = [];
/**
* Table Column Definitions
* @type array
*/
#columnDefs = [];
/**
* Table Ajax Param
* @type object
*/
ajaxParam;
/**
* Table ID String
*/
tableEl;
/**
* DataTable Object
* Will hold the DataTable object of the returned
* created table
*/
tableDt;
/**
* Set the url to be used in the request
* @param url string
*/
setURL(url){
this.#url = url
}
/**
* Set the pagelength
* @param pageLengthInt interger
*/
setPageLength(pageLengthInt){
if(pageLengthInt == 'undeinfed'){
this.#pageLength = 10;
}else if(typeof pageLengthInt == "number"){
this.#pageLength = pageLengthInt;
}else{
console.log('Colums should be of type Array');
}
}
/**
* Set the columns to be used in the datatable
* @param columnsArray array
*/
setColumns(columnsArray){
if(columnsArray == 'undefined'){
this.#columns = [];
}else if(typeof columnsArray == "object" && true == Array.isArray(columnsArray)){
this.#columns = columnsArray;
}else{
console.log('Colums should be of type Array');
}
}
/**
* Set the columns definitions to be used in the datatable
* @param columnDefsArray array
*/
setColumnDefs(columnDefsArray){
if(language == 'undefined'){
this.#columnDefs = [
{ targets: 'no-sort', orderable: false }
]
}else if(typeof columnDefsArray == "object" && true == Array.isArray(columnDefsArray)){
this.#columnDefs = columnDefsArray;
}else{
console.log('Coulmn Definitions should be of type Array');
}
}
/**
* Set the language options for the datatables
* @param language object
*/
setLanguage(language){
if(language == 'undefined'){
this.#language = {
infoEmpty: "No Data To Display",
emptyTable: "No Data To Display",
zeroRecords: "No Data To Display"
}
}else if(typeof language == "object" && false == Array.isArray(language)){
this.#language = language;
}else{
console.log('Language should be of type object');
}
}
/**
* Set the function for the initalization complete
* @param func function
*/
setInitComplete(func){
this.#initComplete = function(settings, json){
if(typeof func == "function"){
func(settings,json);
}
}
}
/**
* Set the ajax request params to be used with the request
* @param param object
*/
setParam(param){
this.ajaxParam = param;
}
/**
* Returns the ajax request params to be used with the request
* @return object
*/
getParam(){
return this.ajaxParam;
}
/**
* Builds the ajax request object to be used with the request
* @param param object
* @param dataSource string set the server returned object to use for the table
*/
makeAjax(param, dataSource='data.result'){
dtReload.param = param;
return {
url:this.#url,
dataSrc:dataSource,
data:function(d){
dtReload.processParam(d, dtReload.param)
},
method:this.#method
}
}
/**
* Sets the options to be used for building the DataTable
* @see #dtOptions
*/
#setOptions(){
this.#dtOptions = {
responsive:this.#responsive,
pageLength:this.#pageLength,
lengthMenu:this.#lengthMenu,
processing:this.#processing,
serverSide:this.#serverSide,
language:this.#language,
ajax:this.makeAjax(this.getParam()),
initComplete:this.#initComplete,
columns:this.#columns,
columnDef:this.#columnDefs
}
}
/**
* Sets and then returns the options to be used for building the DataTable
* @see #dtOptions
* @see #setOptions()
*/
getOptions(){
this.#setOptions();
return this.#dtOptions;
}
/**
* Set the param to be used with the request
* This is used for reloads of the same table.
* @param param object
* @see dtReload.param
*/
setReloadParam(param){
dtReload.param = param;
}
/**
* Get the param to be used with the request
* This is used for reloads of the same table.
* @see dtReload.param
* @returns dtReload.param
*/
getReloadParam(){
return dtReload.param;
}
/**
* Creates the DataTable using the DataTable object.
* You only need to pass in the name id of the table element.
* This will return the table object that DataTables.js creates
* @param el string
* @returns tableDt object
*/
create(el){
this.tableEl = el;
this.tableDt = $(this.tableEl).DataTable(this.getOptions());
return this.tableDt;
}
}
window.DataTableReload = DataTableReload;
})();