jqgrid_utils 1.24.0 → 1.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/#jqgrid_utils.js# +1690 -0
- package/dist/jqgrid_utils.js +35 -1
- package/jqgrid_utils.js +35 -1
- package/package.json +1 -1
|
@@ -0,0 +1,1690 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A module for Jqgrid_utils
|
|
3
|
+
* @module Jqgrid_utils
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
module.exports = class Vanilla_website_utils
|
|
9
|
+
{
|
|
10
|
+
|
|
11
|
+
constructor(settings=false)
|
|
12
|
+
{
|
|
13
|
+
if(settings)
|
|
14
|
+
{
|
|
15
|
+
if(settings.hasOwnProperty('page'))
|
|
16
|
+
{
|
|
17
|
+
this.page = settings['page'];
|
|
18
|
+
localStorage.setItem('page', this.page);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Takes the updated columns data and send it to your API post server
|
|
26
|
+
* loadComplete: async function() for the old record needs to be called, see example !
|
|
27
|
+
@alias module:Jqgrid_utils
|
|
28
|
+
@param {object} - Grid Object (required)
|
|
29
|
+
@param {string} - API URL like https://foo.com (required)
|
|
30
|
+
@param {array} - id list, ids from the column header (required)
|
|
31
|
+
@param {object} - settings - extra key:value to send to your server
|
|
32
|
+
|
|
33
|
+
@example
|
|
34
|
+
var jqu = new Jqgrid_utils({page:page});
|
|
35
|
+
|
|
36
|
+
....},
|
|
37
|
+
loadComplete: async function()
|
|
38
|
+
{
|
|
39
|
+
$grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
|
|
40
|
+
},
|
|
41
|
+
afterSetRow: async function(row)
|
|
42
|
+
{
|
|
43
|
+
const _api = await get_api_url('sapir');
|
|
44
|
+
const api = _api + '/column2';
|
|
45
|
+
let info = {"msg":"failed"};
|
|
46
|
+
var jqu = new Jqgrid_utils();
|
|
47
|
+
info = await jqu.update_row_to_api($grid, api,['id'],row,{server : '232',db : 'sl_h',table : 'kpi',});
|
|
48
|
+
for(let i in info)
|
|
49
|
+
{
|
|
50
|
+
log.info(info[i]);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async update_row_to_api(_self, api='',_ids=['id'],row={}, data)
|
|
57
|
+
{
|
|
58
|
+
let self = this;
|
|
59
|
+
let infos = [];
|
|
60
|
+
let ids = {};
|
|
61
|
+
let values = {};
|
|
62
|
+
let changed = {};
|
|
63
|
+
const rd = _self.jqGrid("getGridParam", "record_data");
|
|
64
|
+
|
|
65
|
+
if(api != '' && Object.keys(row).length > 0 && row.inputData.oper == 'edit')
|
|
66
|
+
{
|
|
67
|
+
for(let i in rd)
|
|
68
|
+
{
|
|
69
|
+
if (rd[i]['id'] === row.rowid)
|
|
70
|
+
{
|
|
71
|
+
|
|
72
|
+
for(let ii in _ids)
|
|
73
|
+
{
|
|
74
|
+
if(rd[i].hasOwnProperty(_ids[ii]))
|
|
75
|
+
{
|
|
76
|
+
ids[_ids[ii]] = rd[i][_ids[ii]];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for(let iii in row.inputData)
|
|
81
|
+
{
|
|
82
|
+
if(iii != 'oper')
|
|
83
|
+
{
|
|
84
|
+
if(Object.keys(ids).indexOf(iii) < 0)
|
|
85
|
+
{
|
|
86
|
+
//console.log(iii);
|
|
87
|
+
//console.log(row.inputData[iii]);
|
|
88
|
+
//console.log(rd[i][iii]);
|
|
89
|
+
if(row.inputData[iii] != rd[i][iii])
|
|
90
|
+
{
|
|
91
|
+
changed[iii] = row.inputData[iii];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
for(let i in changed)
|
|
101
|
+
{
|
|
102
|
+
if( Object.keys(ids).indexOf(i) < 0 && i!= 'oper')
|
|
103
|
+
{
|
|
104
|
+
const col_name = i;
|
|
105
|
+
let col_value = {};
|
|
106
|
+
col_value[col_name] = changed[i];
|
|
107
|
+
data['ids'] = ids;
|
|
108
|
+
data['values'] = col_value;
|
|
109
|
+
data['operator'] = 'edit';
|
|
110
|
+
//console.log(data)
|
|
111
|
+
const info = await self.post_json(api ,JSON.stringify(data));
|
|
112
|
+
infos.push(info);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
else if(api != '' && Object.keys(row).length > 0 && row.inputData.oper == 'add')
|
|
121
|
+
{
|
|
122
|
+
console.log("...add");
|
|
123
|
+
for(let i in row.inputData)
|
|
124
|
+
{
|
|
125
|
+
if(row.inputData[i] && i != 'id' && i != 'oper')
|
|
126
|
+
{
|
|
127
|
+
values[i] = row.inputData[i];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
data['ids'] = ids;
|
|
131
|
+
data['values'] = values;
|
|
132
|
+
data['operator'] = 'add';
|
|
133
|
+
//console.log(data);
|
|
134
|
+
let info = await self.post_json(api ,JSON.stringify(data));
|
|
135
|
+
infos.push(info);
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return infos;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* After Delete a Grid Row send to and DELETE REST Request
|
|
145
|
+
* You need to define loadComplete and afterDelRow
|
|
146
|
+
* The Grid data needs to be saved as record within loadComplete
|
|
147
|
+
@alias module:Jqgrid_utils
|
|
148
|
+
@param {object} - Grid Object (required)
|
|
149
|
+
@param {string} - API URL like https://foo.com (required)
|
|
150
|
+
@param {string} - the row id value from afterDelRow (required)
|
|
151
|
+
@param {array} - id list, ids from the column header colmodel (required)
|
|
152
|
+
@param {object} - settings - extra key:value to send to your server
|
|
153
|
+
@example
|
|
154
|
+
|
|
155
|
+
loadComplete: async function()
|
|
156
|
+
{
|
|
157
|
+
$grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
|
|
158
|
+
},
|
|
159
|
+
afterDelRow: async function(rowid)
|
|
160
|
+
{
|
|
161
|
+
const _api = await get_api_url('sapir');
|
|
162
|
+
const api = _api + '/column2';
|
|
163
|
+
let info = {"msg":"failed"};
|
|
164
|
+
var jqu = new Jqgrid_utils();
|
|
165
|
+
|
|
166
|
+
info = await jqu.delete_row_to_api($grid,api,rowid,['id'],{
|
|
167
|
+
server : '232',
|
|
168
|
+
db : 'sl_h',
|
|
169
|
+
table : 'kpi',
|
|
170
|
+
operator: 'delete',
|
|
171
|
+
});
|
|
172
|
+
log.info(JSON.parse(info).msg);
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
async delete_row_to_api(_self, api='', rowid, _ids=[], data={})
|
|
182
|
+
{
|
|
183
|
+
let info = {"msg":"failed"};
|
|
184
|
+
let self = this;
|
|
185
|
+
let ids = [];
|
|
186
|
+
let values = {};
|
|
187
|
+
const rd = _self.jqGrid("getGridParam", "record_data");
|
|
188
|
+
for(let i in rd)
|
|
189
|
+
{
|
|
190
|
+
if (rd[i]['id'] === rowid)
|
|
191
|
+
{
|
|
192
|
+
for(let ii in _ids)
|
|
193
|
+
{
|
|
194
|
+
if(rd[i].hasOwnProperty(_ids[ii]))
|
|
195
|
+
{
|
|
196
|
+
values[_ids[ii]] = rd[i][_ids[ii]];
|
|
197
|
+
ids.push(_ids[ii]);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
if(api != '' && Object.keys(values).length == ids.length )
|
|
207
|
+
{
|
|
208
|
+
data['ids'] = ids;
|
|
209
|
+
data['values'] = values;
|
|
210
|
+
//console.log(data);
|
|
211
|
+
info = await self.adelete_api(api,JSON.stringify(data));
|
|
212
|
+
}
|
|
213
|
+
return info;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
*Append and sperator based link column to the end of a row
|
|
221
|
+
@alias module:Jqgrid_utils
|
|
222
|
+
@param {object} - col_model of the grid
|
|
223
|
+
@param {string} - URL string
|
|
224
|
+
@param {string} - field value - the visible text of the anchor
|
|
225
|
+
@param {object} - base row key value - like {"name":'wiki','label':"Wiki"}
|
|
226
|
+
@param {string} - URL Attributes
|
|
227
|
+
@param {object} - keys and fields value to use
|
|
228
|
+
@example
|
|
229
|
+
var jqu = new Jqgrid_utils();
|
|
230
|
+
col_model = await jqu.append_seperator_link_column(col_model, 'http://wiki.foo.com/index.php' ,'Wiki',{"name":'wiki','label':"Wiki","width":"65px","align":"center"} ,'target="_blank"',{"report_central":"name"});
|
|
231
|
+
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
async append_seperator_link_column(col_model, url, field_value, base, attr = '', keys)
|
|
235
|
+
{
|
|
236
|
+
url = url + '/';
|
|
237
|
+
let self = this;
|
|
238
|
+
base['formatter'] = function(cell_val, obj)
|
|
239
|
+
{
|
|
240
|
+
let _cell_val = field_value;
|
|
241
|
+
if (typeof keys === 'object')
|
|
242
|
+
{
|
|
243
|
+
let pref = '';
|
|
244
|
+
for (let i in keys)
|
|
245
|
+
{
|
|
246
|
+
|
|
247
|
+
let key = i;
|
|
248
|
+
let v = keys[i];
|
|
249
|
+
let key_val = obj.rowData[v];
|
|
250
|
+
if (key_val)
|
|
251
|
+
{
|
|
252
|
+
if (key_val)
|
|
253
|
+
{
|
|
254
|
+
if(key != '')
|
|
255
|
+
{
|
|
256
|
+
pref += key + '' + '/' + encodeURIComponent(key_val) + '/';
|
|
257
|
+
}
|
|
258
|
+
else
|
|
259
|
+
{
|
|
260
|
+
pref += encodeURIComponent(key_val);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (pref)
|
|
266
|
+
{
|
|
267
|
+
if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
|
|
268
|
+
{
|
|
269
|
+
pref = pref.slice(0, -1);
|
|
270
|
+
}
|
|
271
|
+
cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return cell_val;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
col_model.push(base);
|
|
280
|
+
|
|
281
|
+
return col_model;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* add textarea
|
|
287
|
+
@alias module:Jqgrid_utils
|
|
288
|
+
@param {object} - edittype like
|
|
289
|
+
@example
|
|
290
|
+
let col_model = JSON.parse(await aget_api(url + "/model"));
|
|
291
|
+
col_model = await jqu.add_edit(col_model, 'mon',{ edittype:'textarea', editoptions:{rows:6,cols:100} });
|
|
292
|
+
see for other inputfields:
|
|
293
|
+
http://www.trirand.com/blog/phpjqgrid/doc/_2v80w6oam.htm
|
|
294
|
+
*/
|
|
295
|
+
async add_edit(col_model, edit_field, edittype, editoptions)
|
|
296
|
+
{
|
|
297
|
+
for (let i = 0; i < col_model.length; i++)
|
|
298
|
+
{
|
|
299
|
+
if (col_model[i]['name'] === edit_field)
|
|
300
|
+
{
|
|
301
|
+
Object.assign(col_model[i], edittype );
|
|
302
|
+
Object.assign(col_model[i], editoptions );
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return col_model;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* add textarea
|
|
311
|
+
@alias module:Jqgrid_utils
|
|
312
|
+
@param {string} - edit_filed
|
|
313
|
+
@param {string} - style of the textaread
|
|
314
|
+
@example
|
|
315
|
+
let col_model = JSON.parse(await aget_api(url + "/model"));
|
|
316
|
+
col_model = await jqu.add_textarea(col_model, 'worker','style="width:100%;height:100px"');
|
|
317
|
+
*/
|
|
318
|
+
async add_textarea(col_model, edit_field, style = 'style="width:100%;height:100px"')
|
|
319
|
+
{
|
|
320
|
+
for (let i = 0; i < col_model.length; i++)
|
|
321
|
+
{
|
|
322
|
+
if (col_model[i]['name'] === edit_field)
|
|
323
|
+
{
|
|
324
|
+
col_model[i]['formatter'] = function(cell_val)
|
|
325
|
+
{
|
|
326
|
+
const txt = '<textarea '+ style +'>' + cell_val + '</textarea>' ;
|
|
327
|
+
return txt;
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return col_model;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Get basic colModel data from raw data
|
|
338
|
+
@alias module:Jqgrid_utils
|
|
339
|
+
@param {array} - grid object
|
|
340
|
+
@param {array} - raw data object from loadComplete
|
|
341
|
+
@param {array} - list of columns to exclude (optional)
|
|
342
|
+
@param {array} - existing colModel (optional)
|
|
343
|
+
@returns {array} - col_model
|
|
344
|
+
@example
|
|
345
|
+
var jqu = new Jqgrid_utils();
|
|
346
|
+
,loadComplete: async function(data)
|
|
347
|
+
{
|
|
348
|
+
let col_model = jQuery(this).jqGrid('getGridParam',"colModel");
|
|
349
|
+
const new_col_model= await update_col_model(this, data, ['id','cust_qty','waiting_supplier_orders','waiting_assemblies','pending_components','pending_customer_order',col_model);
|
|
350
|
+
jQuery(this).jqGrid('setGridParam',{colModel:new_col_model});
|
|
351
|
+
},
|
|
352
|
+
*/
|
|
353
|
+
async get_col_model_from_data(obj, data, exclude=[],col_model=[])
|
|
354
|
+
{
|
|
355
|
+
let cols = [];
|
|
356
|
+
for(let i in data)
|
|
357
|
+
{
|
|
358
|
+
const keys = Object.keys(data[i]);
|
|
359
|
+
for(let ii in keys)
|
|
360
|
+
{
|
|
361
|
+
const key = keys[ii];
|
|
362
|
+
cols.push(key);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
cols = cols.filter((item, pos) => cols.indexOf(item) === pos);
|
|
366
|
+
let mcols = [];
|
|
367
|
+
for(let i in col_model)
|
|
368
|
+
{
|
|
369
|
+
mcols.push(col_model[i]['name']);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
let diff = cols.filter(x => !mcols.includes(x));
|
|
374
|
+
const _exclude = new Set(exclude);
|
|
375
|
+
diff = diff.filter((name) => {return !_exclude.has(name); });
|
|
376
|
+
diff.sort();
|
|
377
|
+
|
|
378
|
+
for(let x=0; x < diff.length; x++)
|
|
379
|
+
{
|
|
380
|
+
col_model.push({'name': diff[x],'label': diff[x]});
|
|
381
|
+
}
|
|
382
|
+
//console.log(col_model);
|
|
383
|
+
return col_model;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Replace a Binaery 0 or 1 to other given value
|
|
388
|
+
@alias module:Jqgrid_utils
|
|
389
|
+
@param {string} - cell value
|
|
390
|
+
@param {string} - string replacement for 0
|
|
391
|
+
@param {string} - string replacement for 1
|
|
392
|
+
@example
|
|
393
|
+
var jqu = new Jqgrid_utils();
|
|
394
|
+
let _data = jqu.binery_replace(0,'zero','one');
|
|
395
|
+
or for column formatter
|
|
396
|
+
download_formatter:"var jqu = new Jqgrid_utils();jqu.binary_replace({0},'zero','one')"});
|
|
397
|
+
*/
|
|
398
|
+
binary_replace(cell_value, a='zero',b='one')
|
|
399
|
+
{
|
|
400
|
+
let value = a;
|
|
401
|
+
if(cell_value == 1 || cell_value == 0 )
|
|
402
|
+
{
|
|
403
|
+
if(cell_value == 1)
|
|
404
|
+
{
|
|
405
|
+
value = b;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Convert a 112 date string to a DMY format with sepertaor - sync function
|
|
414
|
+
@alias module:Jqgrid_utils
|
|
415
|
+
@param {string} - date string
|
|
416
|
+
@param {string} - seperator used
|
|
417
|
+
@example
|
|
418
|
+
var jqu = new Jqgrid_utils();
|
|
419
|
+
let _data = jqu._date112_to_DMY('20220104','/');
|
|
420
|
+
console.log(_data);
|
|
421
|
+
*/
|
|
422
|
+
_date112_to_DMY(cell_value, seperator='/')
|
|
423
|
+
{
|
|
424
|
+
let value = cell_value;
|
|
425
|
+
if(cell_value.length >= 8 && cell_value.indexOf(seperator) === -1)
|
|
426
|
+
{
|
|
427
|
+
let a = [];
|
|
428
|
+
a.push(cell_value.substr(6, 2));
|
|
429
|
+
a.push(cell_value.substr(4, 2));
|
|
430
|
+
a.push(cell_value.substr(0, 4));
|
|
431
|
+
value = a.join(seperator);
|
|
432
|
+
}
|
|
433
|
+
return value;
|
|
434
|
+
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Convert a 112 date to a DMY format with sepertaor
|
|
439
|
+
@alias module:Jqgrid_utils
|
|
440
|
+
@param {object} - col_model of the grid
|
|
441
|
+
@param {string} - name of the date 112 column what should get converted
|
|
442
|
+
@param {string} - seperator used
|
|
443
|
+
@example
|
|
444
|
+
var jqu = new Jqgrid_utils();
|
|
445
|
+
let _data = await jqu.date112_to_DMY(this,'field','/');
|
|
446
|
+
console.log(_data);
|
|
447
|
+
*/
|
|
448
|
+
async date112_to_DMY(col_model, edit_field , seperator='/')
|
|
449
|
+
{
|
|
450
|
+
for(let i=0;i< col_model.length;i++)
|
|
451
|
+
{
|
|
452
|
+
if(col_model[i]['name'] === edit_field)
|
|
453
|
+
{
|
|
454
|
+
col_model[i]['formatter'] = function (cell_value, o)
|
|
455
|
+
{
|
|
456
|
+
if(cell_value)
|
|
457
|
+
{
|
|
458
|
+
cell_value = cell_value.toString();
|
|
459
|
+
let value = cell_value;
|
|
460
|
+
if(cell_value.length >= 8 && cell_value.indexOf(seperator) === -1)
|
|
461
|
+
{
|
|
462
|
+
let a = [];
|
|
463
|
+
a.push(cell_value.substr(6, 2));
|
|
464
|
+
a.push(cell_value.substr(4, 2));
|
|
465
|
+
a.push(cell_value.substr(0, 4));
|
|
466
|
+
value = a.join(seperator);
|
|
467
|
+
}
|
|
468
|
+
return value;
|
|
469
|
+
}
|
|
470
|
+
else
|
|
471
|
+
{
|
|
472
|
+
return cell_value;
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return col_model;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Add Formatter
|
|
482
|
+
@alias module:Jqgrid_utils
|
|
483
|
+
@param {array} - grid col_model
|
|
484
|
+
@param {string} - string columns names what will be formatted
|
|
485
|
+
@param {object} - formatter object like { formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }}
|
|
486
|
+
@example
|
|
487
|
+
var jqu = new Jqgrid_utils();
|
|
488
|
+
col_model = await jqu.add_formatter(col_model,'select',{ formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }})
|
|
489
|
+
*/
|
|
490
|
+
|
|
491
|
+
async add_formatter(col_model,edit_field, formatter)
|
|
492
|
+
{
|
|
493
|
+
for(let i=0;i< col_model.length;i++)
|
|
494
|
+
{
|
|
495
|
+
if(col_model[i]['name'] === edit_field)
|
|
496
|
+
{
|
|
497
|
+
if(formatter.hasOwnProperty('formatter') && formatter.hasOwnProperty('formatoptions'))
|
|
498
|
+
{
|
|
499
|
+
col_model[i]['formatter'] = formatter['formatter'];
|
|
500
|
+
col_model[i]['formatoptions'] = formatter['formatoptions'];
|
|
501
|
+
col_model[i]['edittype'] = formatter['formatter'];
|
|
502
|
+
col_model[i]['editoptions'] = formatter['formatoptions'];
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return col_model;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Add HTML Formatter
|
|
512
|
+
@alias module:Jqgrid_utils
|
|
513
|
+
@param {array} - grid col_model
|
|
514
|
+
@param {string} - string columns names what will be converted to ok buttons
|
|
515
|
+
@param {string} - html tag code
|
|
516
|
+
@returns {array} - col_model
|
|
517
|
+
@example
|
|
518
|
+
var jqu = new Jqgrid_utils();
|
|
519
|
+
col_model = await jqu.add_html_formatter(col_model,'process',"<button tabindex='0' class='cellbtn' type='button'>Process</button>");
|
|
520
|
+
*/
|
|
521
|
+
|
|
522
|
+
async add_html_formatter(col_model, edit_field, html)
|
|
523
|
+
{
|
|
524
|
+
for(let i=0;i< col_model.length;i++)
|
|
525
|
+
{
|
|
526
|
+
if(col_model[i]['name'] === edit_field)
|
|
527
|
+
{
|
|
528
|
+
col_model[i]['formatter'] = function (cell_val,o)
|
|
529
|
+
{
|
|
530
|
+
return html;
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return col_model;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Add an OK Button
|
|
539
|
+
@alias module:Jqgrid_utils
|
|
540
|
+
@param {array} - grid col_model
|
|
541
|
+
@param {array} - list of columns names what will be converted to ok buttons
|
|
542
|
+
@returns {array} - col_model
|
|
543
|
+
@example
|
|
544
|
+
var jqu = new Jqgrid_utils();
|
|
545
|
+
col_model = await jqu.add_ok_button(col_model, ['checked']);
|
|
546
|
+
*/
|
|
547
|
+
|
|
548
|
+
async add_ok_button(col_model, fields)
|
|
549
|
+
{
|
|
550
|
+
let self = this;
|
|
551
|
+
for (let i = 0; i < col_model.length; i++)
|
|
552
|
+
{
|
|
553
|
+
if (fields.indexOf(col_model[i]['name']) > -1)
|
|
554
|
+
{
|
|
555
|
+
col_model[i]['formatter'] = function(cell_val)
|
|
556
|
+
{
|
|
557
|
+
if (cell_val != undefined)
|
|
558
|
+
{
|
|
559
|
+
return self.__cell_format(cell_val, 'format_ok');
|
|
560
|
+
}
|
|
561
|
+
else
|
|
562
|
+
{
|
|
563
|
+
return '';
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return col_model;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Get the filled cell data
|
|
575
|
+
@alias module:Jqgrid_utils
|
|
576
|
+
@param {object} - the grid object or its name
|
|
577
|
+
@param {array} - list of columns names what will be collected
|
|
578
|
+
@returns {array} - table array
|
|
579
|
+
@example
|
|
580
|
+
var jqu = new Jqgrid_utils();
|
|
581
|
+
col_model = await jqu.set_link(col_model,'av0_code','url_code','target="blank"');
|
|
582
|
+
*/
|
|
583
|
+
async get_filled_cell_table_data(_grid, fields=[])
|
|
584
|
+
{
|
|
585
|
+
let d = jQuery(_grid).jqGrid('getGridParam','data');
|
|
586
|
+
let keys = fields;
|
|
587
|
+
let _data = [];
|
|
588
|
+
for(let i in d)
|
|
589
|
+
{
|
|
590
|
+
if(d[i].hasOwnProperty('id'))
|
|
591
|
+
{
|
|
592
|
+
let row = [d[i]['id']];
|
|
593
|
+
for(let x in keys)
|
|
594
|
+
{
|
|
595
|
+
if(d[i].hasOwnProperty(keys[x]))
|
|
596
|
+
{
|
|
597
|
+
row.push(d[i][keys[x]]);
|
|
598
|
+
}
|
|
599
|
+
else
|
|
600
|
+
{
|
|
601
|
+
row.push("");
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
var f = row.filter(function(value, index, arr){ return value !== "";});
|
|
605
|
+
if(Object.keys(f).length > 1)
|
|
606
|
+
{
|
|
607
|
+
_data.push(row);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return _data;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Get the filled cell data
|
|
617
|
+
@alias module:Jqgrid_utils
|
|
618
|
+
@param {object} - the grid object or its name
|
|
619
|
+
@param {array} - list of columns names what will be collected
|
|
620
|
+
@returns {object} - json object of the colleted fields
|
|
621
|
+
@example
|
|
622
|
+
var jqu = new Jqgrid_utils();
|
|
623
|
+
col_model = await jqu.set_link(col_model,'av0_code','url_code','target="blank"');
|
|
624
|
+
*/
|
|
625
|
+
|
|
626
|
+
async get_filled_cell_data(_grid, fields=[])
|
|
627
|
+
{
|
|
628
|
+
let d = jQuery(_grid).jqGrid('getGridParam','data');
|
|
629
|
+
let keys = fields;
|
|
630
|
+
let _data = [];
|
|
631
|
+
for(let i in d)
|
|
632
|
+
{
|
|
633
|
+
if(d[i].hasOwnProperty('id'))
|
|
634
|
+
{
|
|
635
|
+
let row = {'id':d[i]['id']};
|
|
636
|
+
for(let x in keys)
|
|
637
|
+
{
|
|
638
|
+
if(d[i].hasOwnProperty(keys[x]))
|
|
639
|
+
{
|
|
640
|
+
if(d[i][keys[x]] != "")
|
|
641
|
+
{
|
|
642
|
+
row[keys[x]] = d[i][keys[x]];
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if(Object.keys(row).length > 1)
|
|
647
|
+
{
|
|
648
|
+
_data.push(row);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return _data;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Add an URL from the data to a specific cell/column
|
|
657
|
+
@alias module:Jqgrid_utils
|
|
658
|
+
@param {object} - col_model of the grid
|
|
659
|
+
@param {string} - name of the column what should get convert to the url
|
|
660
|
+
@param {string} - the used url of the data
|
|
661
|
+
@returns {object} https://foo.bar.com/av0_code/bar
|
|
662
|
+
@example
|
|
663
|
+
var jqu = new Jqgrid_utils();
|
|
664
|
+
let _data = await jqu.get_filled_cell_data(this,["P-","bulk","wholesale"]);
|
|
665
|
+
console.log(_data);
|
|
666
|
+
*/
|
|
667
|
+
async set_link(col_model, edit_field, url ,attr='')
|
|
668
|
+
{
|
|
669
|
+
for(let i=0;i< col_model.length;i++)
|
|
670
|
+
{
|
|
671
|
+
if(col_model[i]['name'] === edit_field)
|
|
672
|
+
{
|
|
673
|
+
col_model[i]['formatter'] = function (cell_val,o)
|
|
674
|
+
{
|
|
675
|
+
return '<a class="gl" ' + attr + 'href="' + o.rowData[url] + '">'+ cell_val +'</a>';
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return col_model;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Hide a col_model column before load the grid
|
|
686
|
+
* @alias module:Jqgrid_utils
|
|
687
|
+
* @param {object} - col_model of the grid
|
|
688
|
+
* @param {string} - name of the column to hide
|
|
689
|
+
* @returns {object} col_model
|
|
690
|
+
* @example
|
|
691
|
+
if(filter['_filter'] == 'ch_p_higher_ch_plus_10pc')
|
|
692
|
+
{
|
|
693
|
+
col_model = await jqu.hide_column(col_model,'wholesale');
|
|
694
|
+
col_model = await jqu.hide_column(col_model,'wholesale_formula');
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
*/
|
|
698
|
+
async hide_column(col_model,field)
|
|
699
|
+
{
|
|
700
|
+
for(let i=0;i< col_model.length;i++)
|
|
701
|
+
{
|
|
702
|
+
if(col_model[i]['name'] === field)
|
|
703
|
+
{
|
|
704
|
+
console.log("fffffffF");
|
|
705
|
+
col_model[i]['hidden'] = true;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return col_model;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
@alias module:Jqgrid_utils
|
|
714
|
+
@param {object} - gridobject;
|
|
715
|
+
@param {object} - grid data (optional);
|
|
716
|
+
@example
|
|
717
|
+
var jqu = new Jqgrid_utils();
|
|
718
|
+
loadComplete: function(){
|
|
719
|
+
jqu.grid_set_caption(this);
|
|
720
|
+
},
|
|
721
|
+
*/
|
|
722
|
+
|
|
723
|
+
s_grid_set_caption(_grid, data=[])
|
|
724
|
+
{
|
|
725
|
+
this.grid_set_captionn(_grid, data=[]);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
Adding the row count number to the caption
|
|
730
|
+
@alias module:Jqgrid_utils
|
|
731
|
+
@param {object} - gridobject;
|
|
732
|
+
@param {object} - grid data (optional);
|
|
733
|
+
@example
|
|
734
|
+
var jqu = new Jqgrid_utils();
|
|
735
|
+
loadComplete: function(){
|
|
736
|
+
await jqu.grid_set_caption(this);
|
|
737
|
+
},
|
|
738
|
+
*/
|
|
739
|
+
async grid_set_caption(_grid, data=[])
|
|
740
|
+
{
|
|
741
|
+
if(_grid)
|
|
742
|
+
{
|
|
743
|
+
const grid = jQuery(_grid);
|
|
744
|
+
let count = 0;
|
|
745
|
+
if(data.length === 0)
|
|
746
|
+
{
|
|
747
|
+
count = grid.jqGrid('getGridParam', 'records');
|
|
748
|
+
}
|
|
749
|
+
else
|
|
750
|
+
{
|
|
751
|
+
count = data.length;
|
|
752
|
+
}
|
|
753
|
+
let caption = grid.jqGrid("getGridParam", "caption");
|
|
754
|
+
const reg = /\d.*x/;
|
|
755
|
+
const new_caption = caption.replace(reg, "");
|
|
756
|
+
grid.jqGrid('setCaption', new_caption + " " + count + 'x');
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
@alias module:Jqgrid_utils
|
|
762
|
+
@param {object} - the col_model of the grid
|
|
763
|
+
@param {string} - the name of the page(optional)
|
|
764
|
+
@param {object} - the grid objec(optional)
|
|
765
|
+
@example
|
|
766
|
+
col_model = await jqu.resize_saved_cell_width(col_model);
|
|
767
|
+
*/
|
|
768
|
+
s_resize_saved_cell_width(col_model, page=false, grid=false)
|
|
769
|
+
{
|
|
770
|
+
this.grid_set_caption(col_model, page, grid);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
@alias module:Jqgrid_utils
|
|
776
|
+
@param {object} - the col_model of the grid
|
|
777
|
+
@param {string} - the name of the page(optional)
|
|
778
|
+
@param {object} - the grid objec(optional)
|
|
779
|
+
@example
|
|
780
|
+
col_model = await jqu.resize_saved_cell_width(col_model);
|
|
781
|
+
*/
|
|
782
|
+
async resize_saved_cell_width(col_model,page=false, grid=false)
|
|
783
|
+
{
|
|
784
|
+
let key = page ? page : this.page;
|
|
785
|
+
key += grid ? '-' + grid + '-w-' : '-grid-w-';
|
|
786
|
+
for(let x = 0; x<= col_model.length; x++)
|
|
787
|
+
{
|
|
788
|
+
if(col_model[x])
|
|
789
|
+
{
|
|
790
|
+
if(col_model[x]['name'])
|
|
791
|
+
{
|
|
792
|
+
const name = col_model[x]['name'];
|
|
793
|
+
const width = localStorage.getItem(key + name);
|
|
794
|
+
if(width)
|
|
795
|
+
{
|
|
796
|
+
col_model[x]['width'] = width;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
return col_model;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
@alias module:Jqgrid_utils
|
|
806
|
+
@param {string} the width of the resized column
|
|
807
|
+
@param {string} column number what get resized
|
|
808
|
+
@param {string} not in use yet
|
|
809
|
+
@example
|
|
810
|
+
* var jqu = new Jqgrid_utils({page:'mypage'});
|
|
811
|
+
* resizeStop: jqu.resize_cell,
|
|
812
|
+
*/
|
|
813
|
+
resize_cell(width, index, _page=false)
|
|
814
|
+
{
|
|
815
|
+
const col_model = jQuery(this).jqGrid ('getGridParam', 'colModel');
|
|
816
|
+
if(col_model[index])
|
|
817
|
+
{
|
|
818
|
+
if(col_model[index]['name'])
|
|
819
|
+
{
|
|
820
|
+
const name = col_model[index]['name'];
|
|
821
|
+
const page = _page ? _page : localStorage.getItem('page');
|
|
822
|
+
const grid = this.id;
|
|
823
|
+
let key = page + '-' + grid + '-w-' + name;
|
|
824
|
+
localStorage.setItem(key, width);
|
|
825
|
+
const cat = localStorage.getItem(key);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* Upsert(insert or update) from the grid to an API
|
|
834
|
+
@alias module:Jqgrid_utils
|
|
835
|
+
@param {object} - row object
|
|
836
|
+
@param {string} - url of the API
|
|
837
|
+
@param {string} - data oject
|
|
838
|
+
@returns {object} {update: 'ok'} or {update: 'failed'}
|
|
839
|
+
@example
|
|
840
|
+
var jqu = new Jqgrid_utils();
|
|
841
|
+
afterSetRow: async function(row)
|
|
842
|
+
{
|
|
843
|
+
let r = await jqu.upsert_row(row, 'http://api.com',{'key':'value'});
|
|
844
|
+
console.log(r);
|
|
845
|
+
},
|
|
846
|
+
*/
|
|
847
|
+
async upsert_row(row, url, req = {})
|
|
848
|
+
{
|
|
849
|
+
if (row.rowid.startsWith('jqg'))
|
|
850
|
+
{
|
|
851
|
+
const r0 = await this.insert_row(row, url);
|
|
852
|
+
return r0;
|
|
853
|
+
}
|
|
854
|
+
else
|
|
855
|
+
{
|
|
856
|
+
const r1 = await this.update_row(row, url);
|
|
857
|
+
return r1;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Insert from the grid to an API used by the upsert_row function
|
|
864
|
+
@alias module:Jqgrid_utils
|
|
865
|
+
@param {object} - row object
|
|
866
|
+
@param {string} - URL of the API
|
|
867
|
+
@returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
|
|
868
|
+
@example
|
|
869
|
+
var jqu = new Jqgrid_utils();
|
|
870
|
+
afterSetRow: async function(row)
|
|
871
|
+
{
|
|
872
|
+
let r = await jqu.insert_row(row, 'http://api.com');
|
|
873
|
+
console.log(r);
|
|
874
|
+
},
|
|
875
|
+
*/
|
|
876
|
+
async insert_row(row, url)
|
|
877
|
+
{
|
|
878
|
+
let req = {};
|
|
879
|
+
let ret = '';
|
|
880
|
+
if (row.inputData.hasOwnProperty('id'))
|
|
881
|
+
{
|
|
882
|
+
req['_id'] = 'id';
|
|
883
|
+
req['_id_val'] = row.inputData['id'];
|
|
884
|
+
for (let i in row.inputData)
|
|
885
|
+
{
|
|
886
|
+
req[i] = row.inputData[i];
|
|
887
|
+
}
|
|
888
|
+
delete req['id'];
|
|
889
|
+
delete req['oper'];
|
|
890
|
+
//console.log(req);
|
|
891
|
+
ret = await this.put_json(url, JSON.stringify(req));
|
|
892
|
+
}
|
|
893
|
+
return ret;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Update from the grid to an API used by the upsert_row function
|
|
899
|
+
@alias module:Jqgrid_utils
|
|
900
|
+
@param {object} - row object
|
|
901
|
+
@param {string} - url of the API
|
|
902
|
+
@param {string} - data oject
|
|
903
|
+
@returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
|
|
904
|
+
@example
|
|
905
|
+
var jqu = new Jqgrid_utils();
|
|
906
|
+
afterSetRow: async function(row)
|
|
907
|
+
{
|
|
908
|
+
let r = await jqu.update_row(row, 'http://api.com',{'key':value});
|
|
909
|
+
console.log(r);
|
|
910
|
+
},
|
|
911
|
+
*/
|
|
912
|
+
|
|
913
|
+
async update_row(row, url, req = {})
|
|
914
|
+
{
|
|
915
|
+
let ret = '';
|
|
916
|
+
{
|
|
917
|
+
if(! req['_id'])
|
|
918
|
+
{
|
|
919
|
+
req['_id'] = 'id';
|
|
920
|
+
}
|
|
921
|
+
req['_id_val'] = row.inputData['id'];
|
|
922
|
+
for (let i in row.inputData)
|
|
923
|
+
{
|
|
924
|
+
req[i] = row.inputData[i];
|
|
925
|
+
}
|
|
926
|
+
delete req['id'];
|
|
927
|
+
delete req['oper'];
|
|
928
|
+
ret = await this.post_json(url, JSON.stringify(req));
|
|
929
|
+
}
|
|
930
|
+
return ret;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Delete from the grid to an API
|
|
935
|
+
@alias module:Jqgrid_utils
|
|
936
|
+
@param {string} - row id
|
|
937
|
+
@param {string} - url of the API
|
|
938
|
+
@returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
|
|
939
|
+
@example
|
|
940
|
+
var jqu = new Jqgrid_utils();
|
|
941
|
+
afterDelRow: async function(row)
|
|
942
|
+
{
|
|
943
|
+
const r = await jqu.delete_row('id', 'http://api.com');
|
|
944
|
+
console.log(r + ' : ' + row + ' - from API');
|
|
945
|
+
},
|
|
946
|
+
*/
|
|
947
|
+
|
|
948
|
+
async delete_row(_id, url)
|
|
949
|
+
{
|
|
950
|
+
let ret = '';
|
|
951
|
+
if (url.indexOf('?') > -1)
|
|
952
|
+
{
|
|
953
|
+
url += "&_id=" + encodeURIComponent(unescape(_id));
|
|
954
|
+
}
|
|
955
|
+
else
|
|
956
|
+
{
|
|
957
|
+
url += "?_id=" + encodeURIComponent(unescape(_id));
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
ret = JSON.parse(await this.adelete_api(url));
|
|
961
|
+
return ret['message'];
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Async Delete request used by function delete_row
|
|
967
|
+
@alias module:Jqgrid_utils
|
|
968
|
+
@param {string} - url of the API
|
|
969
|
+
@param {boalan} - header should be json type? default form type
|
|
970
|
+
@returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
|
|
971
|
+
@example
|
|
972
|
+
var jqu = new Jqgrid_utils();
|
|
973
|
+
afterDelRow: async function(row)
|
|
974
|
+
{
|
|
975
|
+
ret = JSON.parse(await jqu.adelete_api(url));
|
|
976
|
+
},
|
|
977
|
+
*/
|
|
978
|
+
|
|
979
|
+
async adelete_api(url, json = false)
|
|
980
|
+
{
|
|
981
|
+
let ctype = "application/x-www-form-urlencoded";
|
|
982
|
+
let body = null;
|
|
983
|
+
if(json)
|
|
984
|
+
{
|
|
985
|
+
ctype = "application/json;charset=UTF-8" ;
|
|
986
|
+
body = json;
|
|
987
|
+
}
|
|
988
|
+
return new Promise((resolve, reject) =>
|
|
989
|
+
{
|
|
990
|
+
let xhr = new XMLHttpRequest();
|
|
991
|
+
xhr.open("DELETE", url);
|
|
992
|
+
xhr.setRequestHeader("Content-type", ctype);
|
|
993
|
+
xhr.onload = () => resolve(xhr.responseText);
|
|
994
|
+
xhr.onerror = () => reject(xhr.statusText);
|
|
995
|
+
xhr.send(body);
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Async Post request used by the update_row function
|
|
1002
|
+
@alias module:Jqgrid_utils
|
|
1003
|
+
@param {string} - url of the API
|
|
1004
|
+
@param {object} - json object
|
|
1005
|
+
@returns {object} @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
|
|
1006
|
+
@example
|
|
1007
|
+
var jqu = new Jqgrid_utils();
|
|
1008
|
+
ret = JSON.parse(await jqu.post_json(url,{'key':value,'key2':'value'}));
|
|
1009
|
+
*/
|
|
1010
|
+
|
|
1011
|
+
async post_json(url, data)
|
|
1012
|
+
{
|
|
1013
|
+
return new Promise((resolve, reject) =>
|
|
1014
|
+
{
|
|
1015
|
+
let xhr = new XMLHttpRequest();
|
|
1016
|
+
xhr.open("POST", url);
|
|
1017
|
+
xhr.setRequestHeader("Content-type", "application/json");
|
|
1018
|
+
xhr.onload = () => resolve(xhr.responseText);
|
|
1019
|
+
xhr.onerror = () => reject(xhr.statusText);
|
|
1020
|
+
xhr.send(data);
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/**
|
|
1025
|
+
* Async Put request used by the insert_row function
|
|
1026
|
+
@alias module:Jqgrid_utils
|
|
1027
|
+
@param {string} - url of the API
|
|
1028
|
+
@param {object} - json object
|
|
1029
|
+
@returns {object} @returns {object} Object from the the API like {insert: 'ok'} or {insert: 'failed'}
|
|
1030
|
+
@example
|
|
1031
|
+
var jqu = new Jqgrid_utils();
|
|
1032
|
+
ret = JSON.parse(await jqu.put_json(url,{'key':value,'key2':'value2'}));
|
|
1033
|
+
*/
|
|
1034
|
+
async put_json(url, data)
|
|
1035
|
+
{
|
|
1036
|
+
return new Promise((resolve, reject) =>
|
|
1037
|
+
{
|
|
1038
|
+
let xhr = new XMLHttpRequest();
|
|
1039
|
+
xhr.open("PUT", url);
|
|
1040
|
+
xhr.setRequestHeader("Content-type", "application/json");
|
|
1041
|
+
xhr.onload = () => resolve(xhr.responseText);
|
|
1042
|
+
xhr.onerror = () => reject(xhr.statusText);
|
|
1043
|
+
xhr.send(data);
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* Hide the del iconf rom the grid
|
|
1049
|
+
@alias module:Jqgrid_utils
|
|
1050
|
+
@example
|
|
1051
|
+
var jqu = new Jqgrid_utils();
|
|
1052
|
+
jqu.hide_del_icon();
|
|
1053
|
+
*/
|
|
1054
|
+
s_hide_del_icon()
|
|
1055
|
+
{
|
|
1056
|
+
hide_del_icon();
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* Hide the del iconf rom the grid
|
|
1061
|
+
@alias module:Jqgrid_utils
|
|
1062
|
+
@example
|
|
1063
|
+
var jqu = new Jqgrid_utils();
|
|
1064
|
+
await jqu.hide_del_icon();
|
|
1065
|
+
*/
|
|
1066
|
+
async hide_del_icon()
|
|
1067
|
+
{
|
|
1068
|
+
jQuery('.ui-inline-del').each(function(index) {jQuery(this).html('');});
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* Convert a cell into a link/url with data from another cell and spit the value by comma - CSV
|
|
1074
|
+
@alias module:Jqgrid_utils
|
|
1075
|
+
@param {object} - col_model of the grid
|
|
1076
|
+
@param {string} - URL string
|
|
1077
|
+
@param {string} - Column/Cell to use
|
|
1078
|
+
@param {string} - URL Attributes
|
|
1079
|
+
@param {object} - keys and fields value to use
|
|
1080
|
+
@param {object} - format info
|
|
1081
|
+
@param {string} - seperator of the cell value to split (default is comma)
|
|
1082
|
+
@example
|
|
1083
|
+
var jqu = new Jqgrid_utils();
|
|
1084
|
+
col_model = await jqu.add_link_details_csv(col_model, host + '/html/report.html' , 'tags','target="_blank"',{"tags":"tags"},',');
|
|
1085
|
+
|
|
1086
|
+
*/
|
|
1087
|
+
async add_link_details_csv(col_model, url, edit_field, attr = '', keys, format, seperator=',')
|
|
1088
|
+
{
|
|
1089
|
+
let self = this;
|
|
1090
|
+
if (url.indexOf('?') > -1)
|
|
1091
|
+
{
|
|
1092
|
+
url = url + '&';
|
|
1093
|
+
}
|
|
1094
|
+
else
|
|
1095
|
+
{
|
|
1096
|
+
url = url + '?';
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
for (let i = 0; i < col_model.length; i++)
|
|
1100
|
+
{
|
|
1101
|
+
if (col_model[i]['name'] === edit_field)
|
|
1102
|
+
{
|
|
1103
|
+
col_model[i]['formatter'] = function(cell_val, obj)
|
|
1104
|
+
{
|
|
1105
|
+
let key_val = cell_val;
|
|
1106
|
+
const _cell_val = self.__cell_format(cell_val, format);
|
|
1107
|
+
const a = _cell_val.split(seperator);
|
|
1108
|
+
let cell_value = '';
|
|
1109
|
+
for(let x in a)
|
|
1110
|
+
{
|
|
1111
|
+
const x_value = a[x].trim();
|
|
1112
|
+
if(x_value)
|
|
1113
|
+
{
|
|
1114
|
+
if (typeof keys === 'object')
|
|
1115
|
+
{
|
|
1116
|
+
let pref = '';
|
|
1117
|
+
for (let ii in keys)
|
|
1118
|
+
{
|
|
1119
|
+
let key = ii;
|
|
1120
|
+
let v = keys[ii];
|
|
1121
|
+
key_val = obj.rowData[v];
|
|
1122
|
+
if (key_val)
|
|
1123
|
+
{
|
|
1124
|
+
if (key.indexOf('=') !== -1)
|
|
1125
|
+
{
|
|
1126
|
+
pref = pref + '' + key + '' + encodeURIComponent(x_value) + '&';
|
|
1127
|
+
}
|
|
1128
|
+
else
|
|
1129
|
+
{
|
|
1130
|
+
pref = pref + '' + key + '=' + encodeURIComponent(x_value) + '&';
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
if (pref.slice(-1) === '&')
|
|
1135
|
+
{
|
|
1136
|
+
pref = pref.slice(0, -1);
|
|
1137
|
+
}
|
|
1138
|
+
cell_value += '<a ' + attr + 'href="' + url + pref + '"> ' + x_value + '</a>' + seperator + ' ';
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
cell_val = cell_value.trim();
|
|
1142
|
+
if(cell_val.slice(-1) === seperator) //remove last seperator
|
|
1143
|
+
{
|
|
1144
|
+
cell_val = cell_val.slice(0, -1);
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
if(cell_val)
|
|
1150
|
+
{
|
|
1151
|
+
return cell_val;
|
|
1152
|
+
}
|
|
1153
|
+
else
|
|
1154
|
+
{
|
|
1155
|
+
return _cell_val;
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
return col_model;
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
/**
|
|
1167
|
+
* Set styles to individual cells, what are defined in a dedicated column
|
|
1168
|
+
@alias module:Jqgrid_utils
|
|
1169
|
+
@param {object} - grid object
|
|
1170
|
+
@param {string} - name of the column what includes the style values what need to be in a strinify json format
|
|
1171
|
+
@example
|
|
1172
|
+
var jqu = new Jqgrid_utils();
|
|
1173
|
+
loadComplete: async function() {
|
|
1174
|
+
await jqu.set_styles(this);
|
|
1175
|
+
},
|
|
1176
|
+
*/
|
|
1177
|
+
async set_styles(obj,style_column='styles')
|
|
1178
|
+
{
|
|
1179
|
+
|
|
1180
|
+
const rows = jQuery(obj).jqGrid('getGridParam','data');
|
|
1181
|
+
for(let i in rows) {
|
|
1182
|
+
if(rows[i][style_column])
|
|
1183
|
+
{
|
|
1184
|
+
const styles = JSON.parse(rows[i][style_column]);
|
|
1185
|
+
for(let ii in styles)
|
|
1186
|
+
{
|
|
1187
|
+
const rowid = rows[i]['id'];
|
|
1188
|
+
const name = ii;
|
|
1189
|
+
if(rows[i].hasOwnProperty(name))
|
|
1190
|
+
{
|
|
1191
|
+
jQuery("#grid").jqGrid('setCell',rowid,name,"",styles[ii]);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
/**
|
|
1202
|
+
* Convert a cell into a link/url with data from another cell
|
|
1203
|
+
@alias module:Jqgrid_utils
|
|
1204
|
+
@param {object} - col_model of the grid
|
|
1205
|
+
@param {string} - URL string
|
|
1206
|
+
@param {string} - Column/Cell to use
|
|
1207
|
+
@param {string} - URL Attributes
|
|
1208
|
+
@param {object} - keys and fields value to use
|
|
1209
|
+
@example
|
|
1210
|
+
var jqu = new Jqgrid_utils();
|
|
1211
|
+
col_model = await jqu.add_link_details(col_model,'http://foo.bar' , 'style','target="_blank"',{'key':'style'});
|
|
1212
|
+
col_model = await jqu.add_link_details(col_model, host + '/html/table_size.html' , 'database','target="_blank"',{"database":"database","server":"server"});
|
|
1213
|
+
*/
|
|
1214
|
+
async add_link_details(col_model, url, edit_field, attr = '', keys, format)
|
|
1215
|
+
{
|
|
1216
|
+
let self = this;
|
|
1217
|
+
if (url.indexOf('?') > -1)
|
|
1218
|
+
{
|
|
1219
|
+
url = url + '&';
|
|
1220
|
+
}
|
|
1221
|
+
else
|
|
1222
|
+
{
|
|
1223
|
+
url = url + '?';
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
for (let i = 0; i < col_model.length; i++)
|
|
1227
|
+
{
|
|
1228
|
+
if (col_model[i]['name'] === edit_field)
|
|
1229
|
+
{
|
|
1230
|
+
col_model[i]['formatter'] = function(cell_val, obj)
|
|
1231
|
+
{
|
|
1232
|
+
let key_val = cell_val;
|
|
1233
|
+
|
|
1234
|
+
if (typeof keys === 'object')
|
|
1235
|
+
{
|
|
1236
|
+
let pref = '';
|
|
1237
|
+
for (let ii in keys)
|
|
1238
|
+
{
|
|
1239
|
+
let key = ii;
|
|
1240
|
+
let v = keys[ii];
|
|
1241
|
+
key_val = obj.rowData[v];
|
|
1242
|
+
if (key_val)
|
|
1243
|
+
{
|
|
1244
|
+
if (key_val)
|
|
1245
|
+
{
|
|
1246
|
+
if (key.indexOf('=') !== -1)
|
|
1247
|
+
{
|
|
1248
|
+
pref = pref + '' + key + '' + encodeURIComponent(key_val) + '&';
|
|
1249
|
+
}
|
|
1250
|
+
else
|
|
1251
|
+
{
|
|
1252
|
+
pref = pref + '' + key + '=' + encodeURIComponent(key_val) + '&';
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
if (pref)
|
|
1258
|
+
{
|
|
1259
|
+
if (pref.slice(-1) === '&')
|
|
1260
|
+
{
|
|
1261
|
+
pref = pref.slice(0, -1);
|
|
1262
|
+
}
|
|
1263
|
+
const _cell_val = self.__cell_format(cell_val, format);
|
|
1264
|
+
if(cell_val)
|
|
1265
|
+
{
|
|
1266
|
+
cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
|
|
1267
|
+
}
|
|
1268
|
+
else
|
|
1269
|
+
{
|
|
1270
|
+
cell_val = '';
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
}
|
|
1275
|
+
if(cell_val)
|
|
1276
|
+
{
|
|
1277
|
+
return cell_val;
|
|
1278
|
+
}
|
|
1279
|
+
else
|
|
1280
|
+
{
|
|
1281
|
+
return '';
|
|
1282
|
+
}
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
return col_model;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* Convert a cell into seperated based link/url like https://foo.bar.com/field/value/field/value
|
|
1295
|
+
@alias module:Jqgrid_utils
|
|
1296
|
+
@param {object} - col_model of the grid
|
|
1297
|
+
@param {string} - URL string
|
|
1298
|
+
@param {string} - Column/Cell to use
|
|
1299
|
+
@param {string} - URL Attributes
|
|
1300
|
+
@param {object} - keys and fields value to use
|
|
1301
|
+
@example
|
|
1302
|
+
var jqu = new Jqgrid_utils();
|
|
1303
|
+
col_model = await jqu.add_link_details_separator(col_model, url1 , 'style','target="_blank"',{"pricelist":"pricelist","style":"style"});
|
|
1304
|
+
col_model = await jqu.add_link_details_separator(col_model, 'https://foo.com' , 'target_column','target="_blank"',{"mykey":"myval"});
|
|
1305
|
+
*/
|
|
1306
|
+
async add_link_details_separator(col_model, url, edit_field, attr = '', keys, format)
|
|
1307
|
+
{
|
|
1308
|
+
url = url + '/';
|
|
1309
|
+
let self = this;
|
|
1310
|
+
for (let i = 0; i < col_model.length; i++)
|
|
1311
|
+
{
|
|
1312
|
+
if (col_model[i]['name'] === edit_field)
|
|
1313
|
+
{
|
|
1314
|
+
col_model[i]['formatter'] = function(cell_val, obj)
|
|
1315
|
+
{
|
|
1316
|
+
let key_val = cell_val;
|
|
1317
|
+
|
|
1318
|
+
if (typeof keys === 'object')
|
|
1319
|
+
{
|
|
1320
|
+
let pref = '';
|
|
1321
|
+
for (let ii in keys)
|
|
1322
|
+
{
|
|
1323
|
+
|
|
1324
|
+
let key = ii;
|
|
1325
|
+
let v = keys[ii];
|
|
1326
|
+
key_val = obj.rowData[v];
|
|
1327
|
+
if (key_val)
|
|
1328
|
+
{
|
|
1329
|
+
if (key_val)
|
|
1330
|
+
{
|
|
1331
|
+
if(key != '')
|
|
1332
|
+
{
|
|
1333
|
+
pref += key + '' + '/' + encodeURIComponent(key_val) + '/';
|
|
1334
|
+
}
|
|
1335
|
+
else
|
|
1336
|
+
{
|
|
1337
|
+
pref += encodeURIComponent(key_val);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
if (pref)
|
|
1343
|
+
{
|
|
1344
|
+
if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
|
|
1345
|
+
{
|
|
1346
|
+
pref = pref.slice(0, -1);
|
|
1347
|
+
}
|
|
1348
|
+
const _cell_val = self.__cell_format(cell_val, format);
|
|
1349
|
+
cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
return cell_val;
|
|
1355
|
+
};
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
return col_model;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
/**
|
|
1366
|
+
* Convert a cell into seperated based link/url include parameter based url like https://foo.bar.com/field.html?k=v
|
|
1367
|
+
@alias module:Jqgrid_utils
|
|
1368
|
+
@param {object} - col_model of the grid
|
|
1369
|
+
@param {string} - URL string
|
|
1370
|
+
@param {array} - array of dict
|
|
1371
|
+
@param {string} - URL Attributes
|
|
1372
|
+
@example
|
|
1373
|
+
var jqu = new Jqgrid_utils();
|
|
1374
|
+
col_model = await jqu.add_link_separator(col_model, host + '/html' , 'style',[
|
|
1375
|
+
{
|
|
1376
|
+
'field':'pricelist',
|
|
1377
|
+
'extension':'.html',
|
|
1378
|
+
'fields':{'style':'style'}
|
|
1379
|
+
}
|
|
1380
|
+
]);
|
|
1381
|
+
|
|
1382
|
+
*/
|
|
1383
|
+
async add_link_separator(col_model, url, edit_field, fields, attr='')
|
|
1384
|
+
{
|
|
1385
|
+
url = url + '/';
|
|
1386
|
+
let self = this;
|
|
1387
|
+
for (let i = 0; i < col_model.length; i++)
|
|
1388
|
+
{
|
|
1389
|
+
if (col_model[i]['name'] === edit_field)
|
|
1390
|
+
{
|
|
1391
|
+
col_model[i]['formatter'] = function(cell_val, obj)
|
|
1392
|
+
{
|
|
1393
|
+
let key_val = cell_val;
|
|
1394
|
+
let pref = '';
|
|
1395
|
+
for(let x in fields)
|
|
1396
|
+
{
|
|
1397
|
+
for(let xx in fields[x])
|
|
1398
|
+
{
|
|
1399
|
+
if(xx === 'field')
|
|
1400
|
+
{
|
|
1401
|
+
let field_value = obj.rowData[fields[x][xx]];
|
|
1402
|
+
pref += field_value;
|
|
1403
|
+
}
|
|
1404
|
+
if(xx === 'extension')
|
|
1405
|
+
{
|
|
1406
|
+
pref += fields[x][xx] ;
|
|
1407
|
+
}
|
|
1408
|
+
if(xx === 'fields')
|
|
1409
|
+
{
|
|
1410
|
+
pref += '?';
|
|
1411
|
+
for(let key in fields[x][xx])
|
|
1412
|
+
{
|
|
1413
|
+
let val = obj.rowData[fields[x][xx][key]];
|
|
1414
|
+
pref = pref + '' + key + '=' + encodeURIComponent(val) + '&';
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
if (pref)
|
|
1421
|
+
{
|
|
1422
|
+
if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
|
|
1423
|
+
{
|
|
1424
|
+
pref = pref.slice(0, -1);
|
|
1425
|
+
}
|
|
1426
|
+
cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + cell_val + '</a>';
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
return cell_val;
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
return col_model;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
/**
|
|
1437
|
+
* Private Function
|
|
1438
|
+
@alias module:Jqgrid_utils
|
|
1439
|
+
*/
|
|
1440
|
+
__cell_format(cell_value, format)
|
|
1441
|
+
{
|
|
1442
|
+
if (format == 'format_ok')
|
|
1443
|
+
{
|
|
1444
|
+
if (cell_value == 0 || cell_value === 'fail')
|
|
1445
|
+
{
|
|
1446
|
+
cell_value = '<i data-check="failed" class="fa fa-times-circle" aria-hidden="true" style="color:#ff0000;"></i>';
|
|
1447
|
+
}
|
|
1448
|
+
else
|
|
1449
|
+
{
|
|
1450
|
+
cell_value = '<i data-check="ok" class="fa fa-check-circle" aria-hidden="true" style="color:#008000;"></i>';
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
return cell_value;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
@alias module:Jqgrid_utils
|
|
1460
|
+
@param {string} - row_id
|
|
1461
|
+
@param {string} - data id
|
|
1462
|
+
@param {string} - url to request
|
|
1463
|
+
@param {object} - col_model for the table
|
|
1464
|
+
@param {string} - Add to the caption of the subgrid
|
|
1465
|
+
@example
|
|
1466
|
+
subGrid: true,
|
|
1467
|
+
,subGridRowExpanded: async function(_id, id) {
|
|
1468
|
+
let data_url2 = api + '/process_locations?f=data&process=';
|
|
1469
|
+
let col_model_url2 = api + '/process_locations?f=col_model';
|
|
1470
|
+
let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
|
|
1471
|
+
await jqu.subgrid(_id, id, data_url2, col_model2,'Locations for Process');
|
|
1472
|
+
|
|
1473
|
+
let data_url = api + '/process_styles?f=data&process=';
|
|
1474
|
+
let col_model_url = api + '/process_styles?f=col_model';
|
|
1475
|
+
let col_model = JSON.parse(await vwu.aget_api(col_model_url));
|
|
1476
|
+
await jqu.subgrid(_id, id, data_url, col_model,'Styles for Process');
|
|
1477
|
+
},
|
|
1478
|
+
|
|
1479
|
+
or
|
|
1480
|
+
|
|
1481
|
+
subGrid: true,
|
|
1482
|
+
subGridRowExpanded: async function(_id, id) {
|
|
1483
|
+
let row_data = jQuery(this).jqGrid ('getRowData', id);
|
|
1484
|
+
let param={f:'data','style':row_data['style'],pricelist:'P-TENENGR1'};
|
|
1485
|
+
let data_url = api + '/order_ln';
|
|
1486
|
+
data_url = await add_parameters(data_url, param);
|
|
1487
|
+
let col_model_url2 = api + '/order_ln?f=col_model';
|
|
1488
|
+
let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
|
|
1489
|
+
await jqu.subgrid(_id, false, data_url, col_model2,'Order Lines for ' + row_data['style']);
|
|
1490
|
+
},
|
|
1491
|
+
|
|
1492
|
+
*/
|
|
1493
|
+
async subgrid(_id, id, url, col_model, caption='' )
|
|
1494
|
+
{
|
|
1495
|
+
caption = caption != '' ? caption + ' ' : '';
|
|
1496
|
+
if(id)
|
|
1497
|
+
{
|
|
1498
|
+
url += id;
|
|
1499
|
+
}
|
|
1500
|
+
else
|
|
1501
|
+
{
|
|
1502
|
+
id = '';
|
|
1503
|
+
}
|
|
1504
|
+
let $s1 = jQuery("<table style='margin: 5px 0' class='" + _id + "_t'></table>");
|
|
1505
|
+
$s1.appendTo("#" + jQuery.jgrid.jqID(_id));
|
|
1506
|
+
$s1.jqGrid({
|
|
1507
|
+
caption: caption + id,
|
|
1508
|
+
colModel: col_model,
|
|
1509
|
+
datatype: "json",
|
|
1510
|
+
url: url,
|
|
1511
|
+
gridview: true,
|
|
1512
|
+
rownumbers: true,
|
|
1513
|
+
autoencode: true,
|
|
1514
|
+
sortname: "c1",
|
|
1515
|
+
sortorder: "desc"
|
|
1516
|
+
});
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
|
|
1520
|
+
/**
|
|
1521
|
+
@alias module:Jqgrid_utils
|
|
1522
|
+
@param {object} - col_model for the grid
|
|
1523
|
+
@param {string} - field what include the image/picture href path like http://mypicture.png
|
|
1524
|
+
@param {int} - size of the picture
|
|
1525
|
+
@param {bolen} - image path should be a link
|
|
1526
|
+
@example
|
|
1527
|
+
col_model = await jqu.add_image(col_model, 'image', 60, false);
|
|
1528
|
+
*/
|
|
1529
|
+
async add_image(col_model, edit_field, size, link=false)
|
|
1530
|
+
{
|
|
1531
|
+
if (size === undefined)
|
|
1532
|
+
{
|
|
1533
|
+
size = 60;
|
|
1534
|
+
}
|
|
1535
|
+
for (let i = 0; i < col_model.length; i++)
|
|
1536
|
+
{
|
|
1537
|
+
if (col_model[i]['name'] === edit_field)
|
|
1538
|
+
{
|
|
1539
|
+
col_model[i]['picture'] = true;
|
|
1540
|
+
col_model[i]['width'] = size;
|
|
1541
|
+
col_model[i]['height'] = size;
|
|
1542
|
+
col_model[i]['formatter'] = function(cell_val)
|
|
1543
|
+
{
|
|
1544
|
+
const cell_val2 = cell_val.toLowerCase();
|
|
1545
|
+
if(cell_val2.startsWith('https://', 0) || cell_val2.startsWith('http://', 0) )
|
|
1546
|
+
{
|
|
1547
|
+
if(cell_val2.endsWith(".png") ||
|
|
1548
|
+
cell_val2.endsWith(".jpg") ||
|
|
1549
|
+
cell_val2.endsWith(".jpeg") ||
|
|
1550
|
+
cell_val2.endsWith(".gif") ||
|
|
1551
|
+
cell_val2.endsWith(".svg") ||
|
|
1552
|
+
cell_val2.endsWith(".svgz") ||
|
|
1553
|
+
cell_val2.endsWith(".webp"))
|
|
1554
|
+
{
|
|
1555
|
+
if(link)
|
|
1556
|
+
{
|
|
1557
|
+
return '<a target="blank" href="' + cell_val + '"><img src="' + cell_val + '" alt="my image" width="' + size + '" /></a>';
|
|
1558
|
+
}
|
|
1559
|
+
else
|
|
1560
|
+
{
|
|
1561
|
+
return '<img src="' + cell_val + '" alt="my image" width="' + size + '" />';
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
return cell_val;
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
return col_model;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
|
|
1576
|
+
|
|
1577
|
+
|
|
1578
|
+
/**
|
|
1579
|
+
* Add a filter to the website beside the grid
|
|
1580
|
+
@alias module:Jqgrid_utils
|
|
1581
|
+
@param {object} - grid object or grid string name
|
|
1582
|
+
@param {object} - the grid data object
|
|
1583
|
+
@param {object} - a dict with a array what should be filterd by the grid
|
|
1584
|
+
@param {string} - id name of the DOM oject where the filter should be appened
|
|
1585
|
+
@example
|
|
1586
|
+
var jqu = new Jqgrid_utils();
|
|
1587
|
+
var run_me_once = true;
|
|
1588
|
+
gridComplete: async function(){
|
|
1589
|
+
if(run_me_once)
|
|
1590
|
+
{
|
|
1591
|
+
await jqu.set_filter(this, data, {material:[],section:[]}, '#filter');
|
|
1592
|
+
run_me_once = false;
|
|
1593
|
+
}
|
|
1594
|
+
},
|
|
1595
|
+
*/
|
|
1596
|
+
async set_filter(grid, data, fx, append_to="#filter")
|
|
1597
|
+
{
|
|
1598
|
+
jQuery(grid).jqGrid('setGridParam', { fdata: data });
|
|
1599
|
+
let f = document.querySelector(append_to);
|
|
1600
|
+
for(const i in data)
|
|
1601
|
+
{
|
|
1602
|
+
for(let x in fx)
|
|
1603
|
+
{
|
|
1604
|
+
fx[x].push(data[i][x]);
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
for(let x in fx)
|
|
1609
|
+
{
|
|
1610
|
+
fx[x]= fx[x].filter((val, ind, arr) => arr.indexOf(val) === ind);
|
|
1611
|
+
fx[x].sort();
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
for(let x in fx)
|
|
1615
|
+
{
|
|
1616
|
+
let ul = document.createElement('ul');
|
|
1617
|
+
let lh = document.createElement('lh');
|
|
1618
|
+
lh.innerHTML = x;
|
|
1619
|
+
ul.appendChild(lh);
|
|
1620
|
+
for(let i in fx[x])
|
|
1621
|
+
{
|
|
1622
|
+
let li = document.createElement('li');
|
|
1623
|
+
let l = document.createElement('label');
|
|
1624
|
+
l.innerHTML = fx[x][i];
|
|
1625
|
+
let c = document.createElement('input');
|
|
1626
|
+
c.setAttribute('type','checkbox');
|
|
1627
|
+
c.setAttribute('class',x);
|
|
1628
|
+
c.setAttribute('id', x + '_' + fx[x][i]);
|
|
1629
|
+
l.setAttribute('for',x + '_' + fx[x][i]);
|
|
1630
|
+
c.value = fx[x][i];
|
|
1631
|
+
c.onchange = async () => { await this._filter(grid,fx);};
|
|
1632
|
+
li.appendChild(l);
|
|
1633
|
+
li.appendChild(c);
|
|
1634
|
+
ul.appendChild(li);
|
|
1635
|
+
}
|
|
1636
|
+
f.appendChild(ul);
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
/**
|
|
1642
|
+
* private function of set_filter
|
|
1643
|
+
@alias module:Jqgrid_utils
|
|
1644
|
+
*/
|
|
1645
|
+
async _filter(grid, fx)
|
|
1646
|
+
{
|
|
1647
|
+
let _data = [];
|
|
1648
|
+
let data = jQuery(grid).jqGrid('getGridParam','fdata');
|
|
1649
|
+
let filter = [];
|
|
1650
|
+
for(let x in fx)
|
|
1651
|
+
{
|
|
1652
|
+
let m = document.querySelectorAll("." + x);
|
|
1653
|
+
filter[x] = [];
|
|
1654
|
+
for(let i in m)
|
|
1655
|
+
{
|
|
1656
|
+
if(m[i].checked)
|
|
1657
|
+
{
|
|
1658
|
+
filter[x].push(m[i].value);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
for(let i in data)
|
|
1663
|
+
{
|
|
1664
|
+
let include = false;
|
|
1665
|
+
for(let x in fx)
|
|
1666
|
+
{
|
|
1667
|
+
if(filter[x].indexOf(data[i][x]) != -1)
|
|
1668
|
+
{
|
|
1669
|
+
include = true;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
if(include)
|
|
1673
|
+
{
|
|
1674
|
+
_data.push(data[i]);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
jQuery(grid).jqGrid('clearGridData');
|
|
1678
|
+
jQuery(grid).jqGrid('setGridParam', {data: _data});
|
|
1679
|
+
jQuery(grid).trigger('reloadGrid');
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
|
|
1685
|
+
|
|
1686
|
+
|
|
1687
|
+
|
|
1688
|
+
};
|
|
1689
|
+
|
|
1690
|
+
|