1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
*文件名称:JqxGridAPI.js
*API功能描述:封装简单的jqxgrid表格
使用方式:需要提前引用jqxgrid的相关js文件需要引用TJAPP.Common.js文件 JqxGridAPI.FillJqxGrid("grid", TJAPP.WebServiceUrl.ElectTable_test_Server, "TestJson", { a: "a", b: "b" }, 5, function (obj) {});
*作者:张恒
*编写时间:20180429
*/
 
var JqxGridAPI = {
    /*
    *每页显示数量
    */
    pagesize: 10,
    pagesizeoptions: [10, 20, 30],
    /*
    *参数
    *作者:张恒
    *编写时间:20180429
    */
    args: {},
    /*
    *API功能描述:封装简单的jqxgrid表格
    *container:容器id url 请求的服务地址 functionName请求的方法名称 arg 请求参数{ a: "a", b: "b" } pagesie 每页显示数量 callabck 回调函数
    *作者:张恒
    *编写时间:20180429
    */
    FillJqxGrid: function (container, url, functionName, gridHeight, callback) {
        ///前端显示列的集合
        var columns = [];
        var dataItem = [];
        var gjzd="";
        ///数据集合
        var rows = null;
        ///配置数据源
        var source =
           {
               datatype: "json",
               url: url + "/" + functionName,
               pagenum: 0,
               pagesize: JqxGridAPI.pagesize,
               pager: function (pagenum, pagesize, oldpagenum) {
               },
               root: "rows",
               //sort: function () {
               //    //$("#" + container).jqxGrid('updatebounddata', 'sort');
               //},
               sortcolumn: "",
               sortdirection: "asc",
               datatype: "json",//服务返回的数据类型  
               type: "POST",//默认为GET  
               data: JqxGridAPI.args,//定义发送到服务器的默认参数  
               processdata: function (data) {//对data属性的扩展  
                   //data.param = "abcde";
               },
               beforeprocessing: function (data) { //请求后的回调函数,data是查询方法返回的数据  
                   if (data.total != -1)
                       source.totalrecords = data.total;
                   if (data.columns && data.columns.length >= 12) {
                       $.each(data.columns, function (key, value) {
                           value.width = 150;
                       })
                   }
                   if (JqxGridAPI.args.width) {
                       $.each(data.columns, function (key, value) {
                           value.width = JqxGridAPI.args.width;
                       })
                   }
                   if (data.rows)
                   {
                       $.each(data.rows, function (key, value) {
                           //var 
                           $.each(value, function (k, val) {
 
 
                               if (typeof val === 'number' && !isNaN(val) && parseFloat(val).toString().indexOf(".") >= 0) {
                                   data.rows[key][k] = parseFloat(val).toFixed(2);
                               }
                           });                      
                       });
                   }
 
 
                   columns = data.columns;
                   dataItem = data.rows;
                   gjzd = data.gjzd;
               }
           };
        ///配置数据源
        var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                $.extend(data, JqxGridAPI.args);
                return data;
            }
        });
        $("#" + container).jqxGrid(
        {
            //width: TJAPP.GetGridWidth.testGridWidth,
            //autoheight: true,
            height: gridHeight,
            width: "100%",
            source: dataAdapter,
            columnsresize: true,//设置列间距可调整
            pageable: true,
            sortable: true,
            pagermode: 'default',
            pagesizeoptions: JqxGridAPI.pagesizeoptions,//可选择每页显示条数 
            virtualmode: true,
            rendergridrows: function (obj) {
                return obj.data;
            },
            columns: columns,
            cellhover:function(element,x,y){
                // $()
            },
            altrows:true
        });
 
        ///数据完成绑定事件
        $("#" + container).on("bindingcomplete", function (event) {
            try {
                columns= _.remove(columns,function(item){
                    return item.datafield !='datadescription'
                });
 
                if ($('#' + container).jqxGrid("columns").length() == 0)
                    $('#' + container).jqxGrid({
                        columns: columns
                    });
                //$("#" + container).jqxGrid('autoresizecolumns');
                $("#" + container).jqxGrid("endupdate");
                if (typeof callback == 'function') {
                    callback({ item: $('#' + container).jqxGrid(), columns: columns, dataItem: dataItem, source: source,gjzd:gjzd });
                }
 
            } catch (e) { }
        });
        JqxGridAPI.Columnclick(container, source);
    },
    FillJqxGrid_XC: function (container, url, functionName, gridHeight, callback) {
        this.gridHeight = gridHeight;
        ///前端显示列的集合
        var columns = [];
        var dataItem = [];
        var gjzd = "";
        ///数据集合
        var rows = null;
        ///配置数据源
        var source =
           {
               datatype: "json",
               url: url + "/" + functionName,
               pagenum: 0,
               pagesize: JqxGridAPI.pagesize,
               pager: function (pagenum, pagesize, oldpagenum) {
               },
               root: "rows",
               //sort: function () {
               //    $("#" + container).jqxGrid('updatebounddata', 'sort');
               //},
               sortcolumn: "",
               sortdirection: "asc",
               datatype: "json",//服务返回的数据类型  
               type: "GET",//默认为GET  
               data: JqxGridAPI.args,//定义发送到服务器的默认参数  
               processdata: function (data) {//对data属性的扩展  
                   //data.param = "abcde";
               },
               beforeprocessing: function (data) { //请求后的回调函数,data是查询方法返回的数据  
                   if (data.total != -1)
                       source.totalrecords = data.total;
                   if (data.columns && data.columns.length >= 12) {
                       $.each(data.columns, function (key, value) {
                           value.width = 150;
                       })
                   }
                   if (JqxGridAPI.args.width) {
                       $.each(data.columns, function (key, value) {
                           value.width = JqxGridAPI.args.width;
                       })
                   }
                   if (data) {
                       $.each(data.rows, function (key, value) {
                           //var 
                           $.each(value, function (k, val) {
 
 
                               if (typeof val === 'number' && !isNaN(val) && parseFloat(val).toString().indexOf(".") >= 0) {
                                   data.rows[key][k] = parseFloat(val).toFixed(2);
                               }
                           });
                       });
                   }
                   columns = data.columns;
                   dataItem = data.rows;
                   gjzd = data.gjzd;
               }
           };
        ///配置数据源
        var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                $.extend(data, JqxGridAPI.args);
                return data;
            }
        });
        $("#" + container).jqxGrid(
        {
            //width: TJAPP.GetGridWidth.testGridWidth,
            //autoheight: true,
            height: this.gridHeight,
            width: "100%",
            source: dataAdapter,
            columnsresize: true,//设置列间距可调整
            pageable: true,
            sortable: true,
            pagermode: 'default',
            pagesizeoptions: JqxGridAPI.pagesizeoptions,//可选择每页显示条数 
            virtualmode: true,
            rendergridrows: function (obj) {
                return obj.data;
            },
            columns: columns
        });
 
        ///数据完成绑定事件
        $("#" + container).on("bindingcomplete", function (event) {
            try {
                if ($('#' + container).jqxGrid("columns").length() == 0)
                    $('#' + container).jqxGrid({
                        columns: columns
                    });
                //$("#" + container).jqxGrid('autoresizecolumns');
                $("#" + container).jqxGrid("endupdate");
                if (typeof callback == 'function') {
                    callback({ item: $('#' + container).jqxGrid(), columns: columns, dataItem: dataItem, source: source, gjzd: gjzd });
                }
            } catch (e) {
                console.log(e.message);
            }
        });
        JqxGridAPI.Columnclick(container, source);
    },
    /*
    *API功能描述:清空表格
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    ClearnJqxGrid: function (container) {
        $('#' + container).jqxGrid('clear');
    },
    /*
    *API功能描述:销毁表格
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    DestoryJqxGrid: function (container) {
        $('#' + container).jqxGrid('destroy');
    },
 
    /*
    *API功能描述:销毁表格
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    ApplyfiltersJqxGrid: function (container) {
        $('#' + container).jqxGrid('applyfilters');
    },
    Columnclick: function (container, source) {
        $("#" + container).on("columnclick", function (event) {
            var sortinformation = $('#' + container).jqxGrid('getsortinformation');
 
            var sortdirection = sortinformation.sortdirection.ascending ? "asc" : "desc";
            // column data field.
            var sortColumnDataField = sortinformation.sortcolumn;
            //JqxGridAPI.SortJqxGrid(container, sortColumnDataField, sortdirection);
            source.sortdirection = sortdirection;
            source.sortcolumn = sortColumnDataField;
            JqxGridAPI.UpdateBoundDataCells(container);
        });
    },
    /*
    *API功能描述:刷新表格
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    RefreshJqxGrid: function (container) {
        $('#' + container).jqxGrid('refresh');
    },
    /*
    *API功能描述:刷新表格数据
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    RefreshJqxGridData: function (container) {
        $('#' + container).jqxGrid('refreshdata');
    },
    /*
    *API功能描述:表格排序
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    SortJqxGrid: function (container, sortfield, sort) {
        $('#' + container).jqxGrid('sortby', sortfield, sort);
    },
 
    UpdateBoundDataCells: function (container) {
        $("#" + container).jqxGrid("updatebounddata", "cells");
    },
    /*
    *API功能描述:刷新表格数据
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    GoToPage: function (container, pageindex) {
        $('#' + container).jqxGrid('gotopage', pageindex);
    },
 
    /*
    *API功能描述:当前页改变事件
    *container:容器id 
    *作者:张恒
    *编写时间:20180429
    */
    PageChange: function (container, callback) {
        $('#' + container).on('pagechanged', function (event) {
            callback(event);
        });
    },
 
    /*
    *API功能描述:封装简单的jqxgrid表格的行点击事件 此事件用在filljqxgrid之后
    *jqxgrid:FillJqxGrid的回调参数 callabck 回调函数 返回点击行的数据
    *作者:张恒
    *编写时间:20180429
    */
    RowDoublecClick: function (jqxgrid, callback) {
        jqxgrid.on('rowdoubleclick', function (event) {
            var args = event.args;
            var boundIndex = args.rowindex;
            var rowdata = jqxgrid.jqxGrid('getrowdata', boundIndex);
            if (typeof callback == 'function') {
                callback({ data: rowdata });
            }
        });
    },
 
    /*
    *API功能描述:封装简单的jqxgrid表格的行点击事件 此事件用在filljqxgrid之后
    *jqxgrid:FillJqxGrid的回调参数 callabck 回调函数 返回点击行的数据
    *作者:张恒
    *编写时间:20180429
    *dataFields: [
                { name: 'label', type: 'string' },
                { name: 'value', type: 'string' }
            ],
    *array:[
                { value: "AF", label: "Afghanistan" },
                { value: "AL", label: "Albania" },
                { value: "DZ", label: "Algeria" },
                { value: "AR", label: "Argentina" },
                { value: "AM", label: "Armenia" },
                { value: "AU", label: "Australia" },
                { value: "AT", label: "Austria" }
        ];
    */
    FillJqxGridLocalData: function (container, datafields, array, columns, callback) {
        //var array1 = 
        var countriesSource =
        {
            datatype: "array",
            datafields: datafields,
            localdata: array
        };
 
        var cAdapter = new $.jqx.dataAdapter(countriesSource);
        ///配置数据源
        $("#" + container).jqxGrid(
        {
            //width: TJAPP.GetGridWidth.testGridWidth,
            //autoheight: true,
            height: "250",
            width: "100%",
            source: cAdapter,
            columnsresize: true,
            selectionmode: 'checkbox',
            sortable: false,
            pageable: false,
            pagermode: 'none',
            pagesizeoptions: JqxGridAPI.pagesizeoptions,//可选择每页显示条数 
            virtualmode: false,
            rendergridrows: function (obj) {
                return obj.data;
            },
            columns: columns
        });
        ///数据完成绑定事件
        //$("#" + container).on("bindingcomplete", function (event) {
        //    if ($('#' + container).jqxGrid("columns").length() == 0)
        //        $('#' + container).jqxGrid({
        //            columns: columns
        //        });
        //    $("#" + container).jqxGrid('autoresizecolumns');
        //    if (typeof callback == 'function') {
        callback({ item: $('#' + container).jqxGrid() });
        // }
        //});
    },
    /*
    *API功能描述:封装简单的jqxgrid表格的行点击事件 此事件用在filljqxgrid之后
    *jqxgrid:FillJqxGrid的回调参数 callabck 回调函数 返回点击行的数据
    *作者:张恒
    *编写时间:20180429
    */
    RowSelect: function (container, callback) {
        $('#' + container).on('rowselect', function (event) {
            // event arguments.
            var args = event.args;
            // row's bound index.
            var rowBoundIndex = args.rowindex;
            var rowData = args.row;
            callback(rowData);
        });
    },
    /*
    *API功能描述:封装简单的jqxgrid表格的行点击事件 此事件用在filljqxgrid之后
    *jqxgrid:FillJqxGrid的回调参数 callabck 回调函数 返回点击行的数据
    *作者:张恒
    *编写时间:20180429
    */
    RowClick: function (jqxgrid, callback) {
        jqxgrid.on('rowclick', function (event) {
            var args = event.args;
            var boundIndex = args.rowindex;
            var rowdata = jqxgrid.jqxGrid('getrowdata', boundIndex);
            if (typeof callback == 'function') {
                callback({ data: rowdata });
            }
        });
    },
    /*
    *API功能描述:封装简单的jqxgrid表格的单元格点击事件 此事件用在filljqxgrid之后
    *jqxgrid:FillJqxGrid的回调参数 callabck 回调函数 返回点击行的数据
    *作者:张恒
    *编写时间:20180429
    */
    CellClick: function (jqxgrid, callback) {
        jqxgrid.on("cellclick", function (event) {
            // event arguments.
            var args = event.args;
            // row's bound index.
            var rowBoundIndex = args.rowindex;
            // column index.
            var columnindex = args.columnindex;
            // column data field.
            var dataField = args.datafield;
            // cell value
            var value = args.value;
            if (typeof callback == 'function') {
                callback({ value: value, dataField: dataField, columnindex: columnindex, rowindex: rowBoundIndex });
            }
        });
    }
};