田源
2024-04-07 2ac55ce0edf4870a29691b56bfad59f4830a11a2
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
layui.define(['layer'],function(exports){
    /**
     * 参照的列表框
     * weidy@2018-03-08
     */
    var ReferGrid = function(){
        this.defaultConfig = {
            gridUrl:'/webReferController/referDataGrid',//表格的参照路径
            tableDefinedUrl:'/webReferController/getTableDefine',
            valueField:'oid',//值的字段
            textField:'name'//显示的字段
        };
        this.backPath= configData.compatibility ? path : configData.objectServicePath
    };
    ReferGrid.prototype.init = function(){
 
    };
    ReferGrid.prototype.showReferDialog = function (referConfig){
        var filter = referConfig.filter,
            name = referConfig.name,
            title = referConfig.title,
            valueInfo = ($webUtil.isNotNull(referConfig.text)?("已设置的值为[" +referConfig.text +"]"):'未设置值'),
            isMuti = referConfig.isMuti,
            target = referConfig.target,
            value = referConfig.value,
            text = referConfig.text;
        if(filter.indexOf('.')>-1){
            filter=filter.split('.')[0]+'-'+filter.split('.')[1];
        }
        if(name.indexOf('.')>-1){
            name=name.split('.')[0]+'-'+name.split('.')[1];
        }
        var options = referConfig.options;
        var me = this;
        var referFilter = filter+"_" + name;
        var referIndex = $webUtil.dialog({
            title:title,
            btn:['确定','取消'],
            content:'<div layui-filter="referGrid_toolbar_' + referFilter + '" class="layui-table-toolbar "  style="margin: 5px 0 5px 0"></div><div style="margin: 0 5px"><table class="layui-table layui-referGrid" lay-size="sm" lay-even layui-filter="referGrid_' + referFilter + '" id="referGrid_' + referFilter + '" ></table></div>',
            area:[(($webUtil.isNotNull(options.width) && options.width*1>1) ?options.width*1 : 1000) +'px',
                (($webUtil.isNotNull(options.height) && options.height*1>1) ?options.height*1 : 625) + 'px'],
            btnBefore:valueInfo,
            resizing:function(layero){
                me.doResize(filter,name);
            },
            yes:function(index,layero){
                var selectValue = me.getSelectValue(filter,name,isMuti);
                if(selectValue){
                    target.setValue(filter,name,selectValue.value,selectValue.rawValue,true,selectValue.allValues);
                    layer.close(referIndex);
                }
            },
            success:function(layero){
                me.showGrid(filter,name,options,value,text);
            }
        });
    };
    //显示
    ReferGrid.prototype.showGrid=function(filter,name,options,value,rawValue){
        var that = this;
        that.setConfig(filter,name,options);
 
        if($('#referGrid_' + filter + '_' + name).children().length>0){
            $('#referGrid_' + filter + '_' + name).show();
        }else{
            var tableId = 'referGrid_' + filter + '_' + name;
            layui.use('table',function(){//因为table里引用了form ,form里引用了这里
                var table = layui.table;
                var isMuti = $webUtil.getBoolean($webUtil.getValueFromObj(options,['isMuti'],false));
                var requestData = {};
                if(options.extraParams ){
                    for(var key in options.extraParams){
                        requestData[key] = options.extraParams[key];
                    }
                }
                if(options.useFormKey && options.formValues){
                    //使用表单上的字段来过滤
                    requestData['conditionMap["' + (options.paramForFormKey?options.paramForFormKey:options.useFormKey)  + '"]'] = options.formValues[options.useFormKey];
                }
                if(options.where ){
                    for(var key in options.where){
                        requestData['conditionMap["' + key + '"]'] = options.where[key];
                    }
                }
                var copyParam = ['referBo','textField','valueField','displayTable','whereSql','isMuti','queryScheme'];
                for(var i= 0 ; i < copyParam.length ; i ++){
                    requestData[copyParam[i]] = options[copyParam[i]];
                }
                if(!requestData['referBo']){
                    requestData['referBo'] = options.referType;
                }
                if($webUtil.isNotNull(options.displayTable)){
                    //说明是使用平台的表格
                    $webUtil.post(that.defaultConfig.tableDefinedUrl,{btmType:(options.referBo||options.referType),code:options.displayTable,isMuti:isMuti},function(result){
                        if(result.success){
                            var queryScheme = result.querySchema;
                            if($webUtil.isNotNull(queryScheme) && $webUtil.isNull(options.queryScheme)){
                                options.queryScheme = queryScheme;
                            }
                            requestData['queryScheme'] = options.queryScheme;
                            var cols = [];
                            if(result.showIndex){
                                cols.push({field:'LAY_TABLE_INDEX',type:'numbers',title:'序号'});
                            }
                            if(result.showCheckbox){
                                cols.push({field:'LAY_CHECKED',type:'checkbox'});
                            }
                            var referColumn = "";
                            if(result.columns){
                                layui.each(result.columns,function(_index,record){
                                    var col = record;
                                    if(col.field.indexOf(".")>-1){
                                        referColumn += col.field + ",";
                                        col.field = col.field.replace(".","_");
                                    }
                                    if($webUtil.isNotNull(col.enumCode)){
                                        //是枚举的内容
                                        var vciWebComboxStore = layui.vciWebComboxStore;
                                        vciWebComboxStore.newCombox(col.enumCode,{
                                            data:col.enumMap
                                        });
                                        col.templet=function(d,thisItem){
                                            var vciWebComboxStore = layui.vciWebComboxStore;
                                            return vciWebComboxStore.getComboxText(thisItem.enumCode,d[thisItem.field]);
                                        };
                                    }
                                    cols.push(col);
                                });
                            }
                            requestData['referColumn'] = referColumn;
                            var pageObject = false;
                            if(result.limit>0){
                                pageObject = {
                                        limit:result.limit,
                                        page:1
                                };
                            }
                            //可以定义表格了
                            table.render({
                                elem:'#referGrid_' + filter + '_' + name,
                                id:tableId,
                                backPath:options.backPath?options.backPath:that.backPath,
                                url:options.url,
                                page:pageObject,
                                height:options.height?options.height:475,
                                where:requestData,
                                selectMode:isMuti?table.selectMode.muti:table.selectMode.single,
                                cols:[cols],
                                initSort:options.sortKey,
                                remoteSort:options.remoteSort
                            });
                            that.showSearchHtml(tableConfig.queryColumns,tableId,filter,name,requestData);
                        }else{
                            $webUtil.showErrorMsg(result.msg);
                        }
                    });
                }else{
                    //说明是自定义的表格
                    var tableConfig = options.tableConfig;
                    if(!tableConfig){
                        $webUtil.showErrorMsg("没有定义参照的表格配置");
                    }
                    if(!requestData){
                        requestData = {
                            currentUserReferModelKey:options.currentUserReferModelKey,
                            currentUserReferMethodKey:options.currentUserReferMethodKey
                        };
                    }else{
                        requestData['currentUserReferModelKey'] = options.currentUserReferModelKey;
                        requestData['currentUserReferMethodKey'] = options.currentUserReferMethodKey;
                    }
                    if(options.tableConfig && options.tableConfig.cols && options.tableConfig.cols.length>0){
                        //说明传递了的
                        var hasIndex = false;
                        var hasCheck = false;
                        layui.each(options.tableConfig.cols,function (_index,_item){
                            if(_item.field == layui.table.config.indexName){
                                hasIndex = true;
                            }
                            if(_item.field == layui.table.config.checkName){
                                hasCheck = true;
                            }
                        });
                        if(!hasIndex){
                            options.tableConfig.cols.unshift(layui.table.getIndexColumn());
                        }
                        if(!hasCheck){
                            options.tableConfig.cols.unshift(layui.table.getCheckColumn());
                        }
                    }
                    //可以定义表格了
                    table.render({
                        elem:'#referGrid_' + filter + '_' + name,
                        id:tableId,
                        backPath:options.backPath?options.backPath:that.backPath,
                        url:options.url,
                        page:tableConfig.page,
                        height:options.height?options.height:475,
                        where:requestData,
                        method:options.method,
                        selectMode:isMuti?table.selectMode.muti:table.selectMode.single,
                        cols:[tableConfig.cols],
                        initSort:options.initSort,
                        remoteSort:options.remoteSort
                    });
                    that.showSearchHtml(tableConfig.queryColumns,tableId,filter,name,requestData);
                }
            });
        }
    };
    ReferGrid.prototype.showSearchHtml =function(queryColumns,tableId,filter,name,requestData){
        //设置查询区域
        var that = this;
        var table = layui.table;
        var toolbarElem = $('[layui-filter="referGrid_toolbar_' + filter + '_' + name +'"]');
        if(queryColumns){
            var toolbarHtml = '<div class="layui-form-item" style="height:24px;float:left;display:inline-block"> '
                + '<div class="layui-input-inline" style="height:24px;line-height:24px;width:120px;min-width: 120px"><select name="referGrid_search_'+ filter + '_' + name +'" style="height:24px;width:120px;padding-right: 10px;">';
            layui.each(queryColumns,function(_index,queryRecord){
                toolbarHtml += '<option value="' + queryRecord.field + '" >' + queryRecord.title + '</option>';
            });
            toolbarHtml += '</select></div>';
            toolbarHtml += '<div class="layui-input-inline"><input type="text" style="height:24px" name="referGrid_searchvalue_'+ filter + '_' + name +'" placeholder="输入值后进行模糊查询" class="layui-input" ></div>';
            toolbarHtml += '<div class="layui-input-inline" ><button class="layui-btn" style="height:24px;line-height:24px" name="referGrid_searchbtn_'+ filter + '_' + name +'" >查询</button></div>';
            toolbarHtml += '</div>';
            toolbarElem.html(toolbarHtml);
            layui.form.render('select');
            $('[name="referGrid_searchbtn_'+ filter + '_' + name +'"]').click(function(){
                var searchField = $(this).parent().parent().find('select').val();
                var searchValue = $(this).parent().parent().find('[name="referGrid_searchvalue_' + filter + '_' + name + '"]').val();
                that.queryByCondition(tableId,requestData,searchField,searchValue);
            });
            $('[name="referGrid_searchvalue_'+ filter + '_' + name +'"]').keydown(function(e){
                if(e.keyCode == 13) {
                    var searchField = $(this).parent().parent().find('select').val();
                    var searchValue = $(this).val();
                    that.queryByCondition(tableId, requestData, searchField,searchValue);
                }
            });
            $('[name="referGrid_searchvalue_'+ filter + '_' + name +'"]').focus()
        }
    };
    ReferGrid.prototype.queryByCondition = function(tableId,requestData,searchField,searchValue){
        if($webUtil.isNotNull(searchValue)){
            searchValue = "*" + searchValue + "*";
        }
        requestData['conditionMap["' + searchField + '"]'] = searchValue;
        requestData['page']= 1;
        var table = layui.table;
        table.reload(
            tableId, {
                where: requestData
            }
        );
    }
    ReferGrid.prototype.setConfig=function(filter,name,options){
        var that = this;
        if(!that.referConfig){
            that.referConfig = {};
        }
        that.referConfig[filter + "_" + name] = options;
    };
    ReferGrid.prototype.getConfig=function(filter,name){
        var that = this;
        if(!that.referConfig){
            that.referConfig = {};
        }
        if((filter + "_" + name) in that.referConfig){
            return that.referConfig[filter + "_" + name];
        }
    };
    ReferGrid.prototype.getSelectValue = function(filter,name,isMuti){
        var that = this;
        var table = layui.table;
        var config = that.getConfig(filter,name);
        var selectValue = {};
        var checkStatus = table.checkStatus('referGrid_' + filter + "_" + name);
        if(checkStatus.data.length>0){
            if(!isMuti && checkStatus.data.length>1){
                $webUtil.showErrorMsg("每次只能选择一条数据");
                return false;
            }
            var value = "";
            var rawValue = "";
            config.valueField = config.valueField?config.valueField:that.defaultConfig.valueField;
            config.textField = config.textField?config.textField:that.defaultConfig.textField;
            var isMutiValue = (config.valueField.indexOf(",")>-1);
            var isMutiRaw = (config.textField.indexOf(",")>-1);
            layui.each(checkStatus.data,function(_index,item){
                if(isMutiValue){
                    var valueFieldArray = config.valueField.split(",");
                    layui.each(valueFieldArray,function(_indexField,_itemField){
                        value += item[_itemField] + (config.valueSep?config.valueSep:' ');
                    });
                    value+= ",";
                }else {
                    value += item[config.valueField] + ",";
                }
                if(isMutiRaw) {
                    var rawFieldArray = config.textField.split(",");
                    layui.each(rawFieldArray,function(_indexField,_itemField){
                        rawValue += item[_itemField] + (config.textSep?config.textSep:' ');
                    });
                    rawValue+= ",";
                }else{
                    rawValue += item[config.textField] + ",";
                }
            });
            if($webUtil.endWith(value,",")){
                value = value.substring(0,value.length - 1);
            }
            if($webUtil.endWith(rawValue,",")){
                rawValue = rawValue.substring(0,rawValue.length -1);
            }
            selectValue = {
                    value : value,
                    rawValue : rawValue,
                    allValues: checkStatus.data
            };
            return selectValue;
        }else{
            return false;
        }
    };
    ReferGrid.prototype.doResize = function(filter,name){
 
    };
 
    ReferGrid.prototype.destory = function(filter,name){
        //销毁
        var that = this;
        if($('#referGrid_' + filter + '_' + name)){
            $('#referGrid_' + filter + '_' + name).remove();
        }
        if(!that.referConfig){
            that.referConfig = {};
        }
        if((filter + "_" + name) in that.referConfig){
            delete that.referConfig[filter + "_" + name];
        }
    };
    var rg = new ReferGrid();
    exports('vciWebReferGrid',rg);
});