|
11 | 11 | factory(jQuery); |
12 | 12 | } |
13 | 13 | }(function ($) { |
14 | | -function fetchOptions(elem,pid,selectedId,selectedIds,url){ |
| 14 | +function fetchOptions(elem,pid,selectedId,selectedIds,url,idKey,callback){ |
15 | 15 | if(!url) url=BACKEND_URL+'/tool/area/index'; |
16 | | - $.get(url,{pid:pid},function(r){ |
| 16 | + $.get(typeof url === 'function'?url():url,{pid:pid},function(r){ |
17 | 17 | if(r.Code!=1){ |
18 | 18 | return App.message({text:r.Info,type:'error'}); |
19 | 19 | } |
20 | | - if(r.Data.listData.length<1){ |
| 20 | + if(!r.Data || r.Data.listData.length<1){ |
21 | 21 | while($(elem).next('select').length>0) $(elem).next('select').remove(); |
22 | | - $(elem).remove(); |
| 22 | + if(pid!=''&&pid!='0') $(elem).remove(); |
| 23 | + else $(elem).html('<option value=""> - '+(App.i18n.PLEASE_SELECT?App.i18n.PLEASE_SELECT:'请选择')+' - </option>'); |
23 | 24 | return; |
24 | 25 | } |
25 | 26 | $(elem).show(h); |
26 | 27 | var exclude = $(elem).data('exclude'); |
27 | 28 | var h = '<option value=""> - '+(App.i18n.PLEASE_SELECT?App.i18n.PLEASE_SELECT:'请选择')+' - </option>'; |
28 | 29 | for(var i=0;i<r.Data.listData.length;i++){ |
29 | 30 | var v=r.Data.listData[i]; |
30 | | - var s=selectedId==v.id?' selected':''; |
31 | | - if(exclude==v.id) s+=' disabled'; |
32 | | - h+='<option value="'+v.id+'"'+s+'>'+v.name+'</option>'; |
| 31 | + var s=''; |
| 32 | + if(selectedId==v[idKey]) s+=' selected'; |
| 33 | + if(exclude==v[idKey]) s+=' disabled'; |
| 34 | + h+='<option value="'+v[idKey]+'"'+s+'>'+v.name+'</option>'; |
33 | 35 | } |
34 | 36 | $(elem).html(h); |
35 | | - bindEvent(elem,selectedIds,url).trigger('change'); |
| 37 | + callback && callback(pid,r); |
| 38 | + bindEvent(elem,selectedIds,url,idKey,callback).trigger('change'); |
36 | 39 | },'json'); |
37 | 40 | } |
38 | | -function bindEvent(elem,selectedIds,url){ |
| 41 | +function bindEvent(elem,selectedIds,url,idKey,callback){ |
39 | 42 | if($(elem).data('bindEvent')){ |
40 | 43 | return $(elem); |
41 | 44 | } |
@@ -64,19 +67,57 @@ function bindEvent(elem,selectedIds,url){ |
64 | 67 | } |
65 | 68 | var selectedId=''; |
66 | 69 | if($.isArray(selectedIds) && selectedIds.length>index) selectedId=selectedIds[index]; |
67 | | - fetchOptions($(this).next('select')[0],v,selectedId,selectedIds,url); |
| 70 | + fetchOptions($(this).next('select')[0],v,selectedId,selectedIds,url,idKey,callback); |
68 | 71 | }); |
69 | 72 | } |
70 | | -function init(elem,selectedIds,url){ |
| 73 | +function init(elem,selectedIds,url,idKey,callback){ |
71 | 74 | if(selectedIds == null){ |
72 | 75 | selectedIds = $(elem).data('selected') || []; |
73 | 76 | if(selectedIds && !$.isArray(selectedIds)) selectedIds = String(selectedIds).split(","); |
74 | 77 | } |
75 | | - if(url == null){ |
76 | | - url = $(elem).data('url'); |
77 | | - } |
| 78 | + if(!url) url = $(elem).data('url'); |
| 79 | + if(!idKey) idKey = $(elem).data('idKey') || 'id'; |
78 | 80 | var selected = selectedIds && selectedIds.length > 0 ? selectedIds[0] : ''; |
79 | | - fetchOptions(elem,0,selected,selectedIds,url); |
| 81 | + var country = $(elem).data('country'); |
| 82 | + var countryURL = $(elem).data('country-url'); |
| 83 | + if(country || countryURL){ |
| 84 | + if(!countryURL) countryURL = typeof url == 'function' ? url() : url; |
| 85 | + $.get(countryURL,{country:true,id:selected},function(r){ |
| 86 | + if(r.Code!=1){ |
| 87 | + return App.message({text:r.Info,type:'error'}); |
| 88 | + } |
| 89 | + if(!r.Data || r.Data.listData.length<1){ |
| 90 | + return; |
| 91 | + } |
| 92 | + var h = '<option value=""> - '+(App.i18n.PLEASE_SELECT?App.i18n.PLEASE_SELECT:'请选择')+' - </option>'; |
| 93 | + var selectedAbbr = r.Data.countryAbbr||''; |
| 94 | + for(var i=0;i<r.Data.listData.length;i++){ |
| 95 | + var v=r.Data.listData[i]; |
| 96 | + var s=''; |
| 97 | + if(selectedAbbr==v.abbr) s+=' selected'; |
| 98 | + h+='<option value="'+v.abbr+'"'+s+'>'+v.name+'</option>'; |
| 99 | + } |
| 100 | + var wrap=$(elem).prev('span.select-country-wrap'); |
| 101 | + if(wrap.length<1){ |
| 102 | + var $h=$('<span class="select-country-wrap"><select id="select-country">'+h+'</select></span>'); |
| 103 | + $(elem).before($h); |
| 104 | + var $select=$h.children('select'); |
| 105 | + $select.attr('class',$(elem).attr('class')); |
| 106 | + $select.on('change',function(){ |
| 107 | + $(elem).trigger('reload') |
| 108 | + }).trigger('change'); |
| 109 | + } |
| 110 | + },'json'); |
| 111 | + var oldURL = typeof url == 'function' ? url() : url; |
| 112 | + var sep = oldURL.indexOf('?') > 0 ? '&' : '?'; |
| 113 | + url = function(){ |
| 114 | + return oldURL + sep + 'countryAbbr='+($('#select-country').val()||''); |
| 115 | + }; |
| 116 | + } |
| 117 | + fetchOptions(elem,0,selected,selectedIds,url,idKey,callback); |
| 118 | + $(elem).on('reload',function(){ |
| 119 | + fetchOptions(elem,0,selected,selectedIds,url,idKey,callback); |
| 120 | + }); |
80 | 121 | } |
81 | 122 | window.CascadeSelect = { |
82 | 123 | init: init |
|
0 commit comments