jquery.combogrid.js
7.68 KB
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
/**
* jQuery EasyUI 1.5
*
* Copyright (c) 2009-2016 www.jeasyui.com. All rights reserved.
*
* Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
* To use it on other terms please contact us: info@jeasyui.com
*
*/
(function($){
function _1(_2){
var _3=$.data(_2,"combogrid");
var _4=_3.options;
var _5=_3.grid;
$(_2).addClass("combogrid-f").combo($.extend({},_4,{onShowPanel:function(){
_20(this,$(this).combogrid("getValues"),true);
var p=$(this).combogrid("panel");
var _6=p.outerHeight()-p.height();
var _7=p._size("minHeight");
var _8=p._size("maxHeight");
var dg=$(this).combogrid("grid");
dg.datagrid("resize",{width:"100%",height:(isNaN(parseInt(_4.panelHeight))?"auto":"100%"),minHeight:(_7?_7-_6:""),maxHeight:(_8?_8-_6:"")});
var _9=dg.datagrid("getSelected");
if(_9){
dg.datagrid("scrollTo",dg.datagrid("getRowIndex",_9));
}
_4.onShowPanel.call(this);
}}));
var _a=$(_2).combo("panel");
if(!_5){
_5=$("<table></table>").appendTo(_a);
_3.grid=_5;
}
_5.datagrid($.extend({},_4,{border:false,singleSelect:(!_4.multiple),onLoadSuccess:_b,onClickRow:_c,onSelect:_d("onSelect"),onUnselect:_d("onUnselect"),onSelectAll:_d("onSelectAll"),onUnselectAll:_d("onUnselectAll")}));
function _e(dg){
return $(dg).closest(".combo-panel").panel("options").comboTarget||_2;
};
function _b(_f){
var _10=_e(this);
var _11=$(_10).data("combogrid");
var _12=_11.options;
var _13=$(_10).combo("getValues");
_20(_10,_13,_11.remainText);
_12.onLoadSuccess.call(this,_f);
};
function _c(_14,row){
var _15=_e(this);
var _16=$(_15).data("combogrid");
var _17=_16.options;
_16.remainText=false;
_18.call(this);
if(!_17.multiple){
$(_15).combo("hidePanel");
}
_17.onClickRow.call(this,_14,row);
};
function _d(_19){
return function(_1a,row){
var _1b=_e(this);
var _1c=$(_1b).combogrid("options");
if(_19=="onUnselectAll"){
if(_1c.multiple){
_18.call(this);
}
}else{
_18.call(this);
}
_1c[_19].call(this,_1a,row);
};
};
function _18(){
var dg=$(this);
var _1d=_e(dg);
var _1e=$(_1d).data("combogrid");
var _1f=_1e.options;
var vv=$.map(dg.datagrid("getSelections"),function(row){
return row[_1f.idField];
});
vv=vv.concat(_1f.unselectedValues);
_20(_1d,vv,_1e.remainText);
};
};
function nav(_21,dir){
var _22=$.data(_21,"combogrid");
var _23=_22.options;
var _24=_22.grid;
var _25=_24.datagrid("getRows").length;
if(!_25){
return;
}
var tr=_23.finder.getTr(_24[0],null,"highlight");
if(!tr.length){
tr=_23.finder.getTr(_24[0],null,"selected");
}
var _26;
if(!tr.length){
_26=(dir=="next"?0:_25-1);
}else{
var _26=parseInt(tr.attr("datagrid-row-index"));
_26+=(dir=="next"?1:-1);
if(_26<0){
_26=_25-1;
}
if(_26>=_25){
_26=0;
}
}
_24.datagrid("highlightRow",_26);
if(_23.selectOnNavigation){
_22.remainText=false;
_24.datagrid("selectRow",_26);
}
};
function _20(_27,_28,_29){
var _2a=$.data(_27,"combogrid");
var _2b=_2a.options;
var _2c=_2a.grid;
var _2d=$(_27).combo("getValues");
var _2e=$(_27).combo("options");
var _2f=_2e.onChange;
_2e.onChange=function(){
};
var _30=_2c.datagrid("options");
var _31=_30.onSelect;
var _32=_30.onUnselectAll;
_30.onSelect=_30.onUnselectAll=function(){
};
if(!$.isArray(_28)){
_28=_28.split(_2b.separator);
}
if(!_2b.multiple){
_28=_28.length?[_28[0]]:[""];
}
var vv=$.map(_28,function(_33){
return String(_33);
});
vv=$.grep(vv,function(v,_34){
return _34===$.inArray(v,vv);
});
var _35=$.grep(_2c.datagrid("getSelections"),function(row,_36){
return $.inArray(String(row[_2b.idField]),vv)>=0;
});
_2c.datagrid("clearSelections");
_2c.data("datagrid").selectedRows=_35;
var ss=[];
_2b.unselectedValues=[];
$.map(vv,function(v){
var _37=_2c.datagrid("getRowIndex",v);
if(_37>=0){
_2c.datagrid("selectRow",_37);
}else{
_2b.unselectedValues.push(v);
}
ss.push(_38(v,_2c.datagrid("getRows"))||_38(v,_35)||_38(v,_2b.mappingRows)||v);
});
$(_27).combo("setValues",_2d);
_2e.onChange=_2f;
_30.onSelect=_31;
_30.onUnselectAll=_32;
if(!_29){
var s=ss.join(_2b.separator);
if($(_27).combo("getText")!=s){
$(_27).combo("setText",s);
}
}
$(_27).combo("setValues",_28);
function _38(_39,a){
var _3a=$.easyui.getArrayItem(a,_2b.idField,_39);
return _3a?_3a[_2b.textField]:undefined;
};
};
function _3b(_3c,q){
var _3d=$.data(_3c,"combogrid");
var _3e=_3d.options;
var _3f=_3d.grid;
_3d.remainText=true;
if(_3e.multiple&&!q){
_20(_3c,[],true);
}else{
_20(_3c,[q],true);
}
if(_3e.mode=="remote"){
_3f.datagrid("clearSelections");
_3f.datagrid("load",$.extend({},_3e.queryParams,{q:q}));
}else{
if(!q){
return;
}
_3f.datagrid("clearSelections").datagrid("highlightRow",-1);
var _40=_3f.datagrid("getRows");
var qq=_3e.multiple?q.split(_3e.separator):[q];
$.map(qq,function(q){
q=$.trim(q);
if(q){
$.map(_40,function(row,i){
if(q==row[_3e.textField]){
_3f.datagrid("selectRow",i);
}else{
if(_3e.filter.call(_3c,q,row)){
_3f.datagrid("highlightRow",i);
}
}
});
}
});
}
};
function _41(_42){
var _43=$.data(_42,"combogrid");
var _44=_43.options;
var _45=_43.grid;
var tr=_44.finder.getTr(_45[0],null,"highlight");
_43.remainText=false;
if(tr.length){
var _46=parseInt(tr.attr("datagrid-row-index"));
if(_44.multiple){
if(tr.hasClass("datagrid-row-selected")){
_45.datagrid("unselectRow",_46);
}else{
_45.datagrid("selectRow",_46);
}
}else{
_45.datagrid("selectRow",_46);
}
}
var vv=[];
$.map(_45.datagrid("getSelections"),function(row){
vv.push(row[_44.idField]);
});
$(_42).combogrid("setValues",vv);
if(!_44.multiple){
$(_42).combogrid("hidePanel");
}
};
$.fn.combogrid=function(_47,_48){
if(typeof _47=="string"){
var _49=$.fn.combogrid.methods[_47];
if(_49){
return _49(this,_48);
}else{
return this.combo(_47,_48);
}
}
_47=_47||{};
return this.each(function(){
var _4a=$.data(this,"combogrid");
if(_4a){
$.extend(_4a.options,_47);
}else{
_4a=$.data(this,"combogrid",{options:$.extend({},$.fn.combogrid.defaults,$.fn.combogrid.parseOptions(this),_47)});
}
_1(this);
});
};
$.fn.combogrid.methods={options:function(jq){
var _4b=jq.combo("options");
return $.extend($.data(jq[0],"combogrid").options,{width:_4b.width,height:_4b.height,originalValue:_4b.originalValue,disabled:_4b.disabled,readonly:_4b.readonly});
},cloneFrom:function(jq,_4c){
return jq.each(function(){
$(this).combo("cloneFrom",_4c);
$.data(this,"combogrid",{options:$.extend(true,{cloned:true},$(_4c).combogrid("options")),combo:$(this).next(),panel:$(_4c).combo("panel"),grid:$(_4c).combogrid("grid")});
});
},grid:function(jq){
return $.data(jq[0],"combogrid").grid;
},setValues:function(jq,_4d){
return jq.each(function(){
var _4e=$(this).combogrid("options");
if($.isArray(_4d)){
_4d=$.map(_4d,function(_4f){
if(_4f&&typeof _4f=="object"){
$.easyui.addArrayItem(_4e.mappingRows,_4e.idField,_4f);
return _4f[_4e.idField];
}else{
return _4f;
}
});
}
_20(this,_4d);
});
},setValue:function(jq,_50){
return jq.each(function(){
$(this).combogrid("setValues",$.isArray(_50)?_50:[_50]);
});
},clear:function(jq){
return jq.each(function(){
$(this).combogrid("setValues",[]);
});
},reset:function(jq){
return jq.each(function(){
var _51=$(this).combogrid("options");
if(_51.multiple){
$(this).combogrid("setValues",_51.originalValue);
}else{
$(this).combogrid("setValue",_51.originalValue);
}
});
}};
$.fn.combogrid.parseOptions=function(_52){
var t=$(_52);
return $.extend({},$.fn.combo.parseOptions(_52),$.fn.datagrid.parseOptions(_52),$.parser.parseOptions(_52,["idField","textField","mode"]));
};
$.fn.combogrid.defaults=$.extend({},$.fn.combo.defaults,$.fn.datagrid.defaults,{loadMsg:null,idField:null,textField:null,unselectedValues:[],mappingRows:[],mode:"local",keyHandler:{up:function(e){
nav(this,"prev");
e.preventDefault();
},down:function(e){
nav(this,"next");
e.preventDefault();
},left:function(e){
},right:function(e){
},enter:function(e){
_41(this);
},query:function(q,e){
_3b(this,q);
}},filter:function(q,row){
var _53=$(this).combogrid("options");
return (row[_53.textField]||"").toLowerCase().indexOf(q.toLowerCase())>=0;
}});
})(jQuery);