DX1.5 地区四级分类代码解读,以及如何修改成五级列表的方法

dx1.5支持居住地5级分类,但是系统中默认的分类只有4级。如果把目前的4级地区分类改成5级分类呢,这里我们先要读懂dx的源代码后才能进行修改。

主要关系到以下几个文件:

1、source/function/function_profile.php 这个基类主要是为spacecp文件提供方法封装,这样的写法,能够使所有的代码结构更清晰,调用的时候也更加容易记忆。

方法showdistrict,定义了四级的下拉框无刷新地区分类。通过我的修改和调整,这个文件修改成支持5级地区分类的选择。

其中居住地细分到第五级也就是小区/乡村级别。出生地细分到第三级就够了,知道用这个能够找到老乡就可以。但是在居住地就不一样,越细越能让用户找到自己身边的人。

 1 function showdistrict($values, $elems=array(), $container='districtbox', $showlevel=null, $containertype = 'birth') {
 2     $html = '';
 3     if(!preg_match("/^[A-Za-z0-9_]+$/", $container)) {
 4         return $html;
 5     }
 6     $showlevel = !empty($showlevel) ? intval($showlevel) : count($values);
 7     $showlevel = $showlevel <= 5 ? $showlevel : 5;
 8     $upids = array(0);
 9     for($i=0;$i<$showlevel;$i++) {
10         if(!empty($values[$i])) {
11             $upids[] = intval($values[$i]);
12         } else {
13             for($j=$i; $j<$showlevel; $j++) {
14                 $values[$j] = '';
15             }
16             break;
17         }
18     }
19     $options = array(1=>array(), 2=>array(), 3=>array(), 4=>array(), 5=>array());
20     if($upids && is_array($upids)) {
21         $query = DB::query('SELECT * FROM '.DB::table('common_district')." WHERE upid IN (".dimplode($upids).')');
22         while($value = DB::fetch($query)) {
23             $options[$value['level']][] = array($value['id'], $value['name']);
24         }
25     }
26     $names = array('province', 'city', 'district', 'community', 'suite');
27     for($i=0; $i<5;$i++) {
28         $elems[$i] = !empty($elems[$i]) ? dhtmlspecialchars($elems[$i]) : ($containertype == 'birth' ? 'birth' : 'reside').$names[$i];
29     }
30 
31     for($i=0;$i<$showlevel;$i++) {
32         $level = $i+1;
33         $jscall = "showdistrict('$container', ['$elems[0]', '$elems[1]', '$elems[2]', '$elems[3]', '$elems[4]'], $showlevel, $level, '$containertype')";
34         $html .= '<select name="'.$elems[$i].'" id="'.$elems[$i].'" onchange="'.$jscall.'" tabindex="1">';
35         $html .= '<option value="">'.lang('spacecp', 'district_level_'.$level).'</option>';
36         foreach($options[$level] as $option) {
37             $selected = $option[0] == $values[$i] ? ' selected="selected"' : '';
38             $html .= '<option did="'.$option[0].'" value="'.$option[1].'"'.$selected.'>'.$option[1].'</option>';
39         }
40         $html .= '</select>';
41         $html .= '&nbsp;&nbsp;';
42     }
43     return $html;
44 }

最终返回组成地区分类色所有html代码,展现给用户。

2、souce/include/misc/misc_ajax.php 这个基类为提交后实现的ajax方法。这个文件封装的所有方法都是暂时性接受ajax方法post过来的数据接收和处理,最后return回主页面。这样整个ajax的无刷新效果就实现了。

这里根据url进行判断,如果接受过来的url字段中包含op=district,那就执行如下代码,主要的作用是结合js代码进行参数的筛选和方法的执行。以实现ajax的无刷新地区选择效果。

 1 elseif($op == 'district') {
 2     $container = $_GET['container'];
 3     $showlevel = intval($_GET['level']);
 4     $showlevel = $showlevel >= 1 && $showlevel <= 5 ? $showlevel : 5;
 5     $values = array(intval($_GET['pid']), intval($_GET['cid']), intval($_GET['did']), intval($_GET['coid']), intval($_GET['sid']));
 6     $containertype = in_array($_GET['containertype'], array('birth', 'reside'), true) ? $_GET['containertype'] : 'birth';
 7     $level = 1;
 8     if($values[0]) {
 9         $level++;
10     } else if($_G['uid']) {
11         space_merge($_G['member'], 'profile');
12 
13         $district = array();
14         if($containertype == 'birth') {
15             if(!empty($_G['member']['birthprovince'])) {
16                 $district[] = $_G['member']['birthprovince'];
17                 if(!empty($_G['member']['birthcity'])) {
18                     $district[] = $_G['member']['birthcity'];
19                 }
20                 if(!empty($_G['member']['birthdist'])) {
21                     $district[] = $_G['member']['birthdist'];
22                 }
23             }
24         } else {
25             if(!empty($_G['member']['resideprovince'])) {
26                 $district[] = $_G['member']['resideprovince'];
27                 if(!empty($_G['member']['residecity'])) {
28                     $district[] = $_G['member']['residecity'];
29                 }
30                 if(!empty($_G['member']['residedist'])) {
31                     $district[] = $_G['member']['residedist'];
32                 }
33                 if(!empty($_G['member']['residecommunity'])) {
34                     $district[] = $_G['member']['residecommunity'];
35                 }
36                 if(!empty($_G['member']['residesuite'])) {
37                     $district[] = $_G['member']['residesuite'];
38                 }
39             }
40         }
41         if(!empty($district)) {
42             $query = DB::query('SELECT * FROM '.DB::table('common_district')." WHERE name IN (".dimplode(daddslashes($district)).')');
43             while($value = DB::fetch($query)) {
44                 $key = $value['level'] - 1;
45                 $values[$key] = $value['id'];
46             }
47             $level++;
48         }
49     }
50     if($values[1]) {
51         $level++;
52     }
53     if($values[2]) {
54         $level++;
55     }
56     if($values[3]) {
57         $level++;
58     }
59     if($values[4]) {
60         $level++;
61     }
62     if($containertype == 'birth' && $level > 2) {
63         $level = 3;
64     }
65     $showlevel = $level;
66     $elems = array();
67     if($_GET['province']) {
68         $elems = array($_GET['province'], $_GET['city'], $_GET['district'], $_GET['community'], $_GET['suite']);
69     }
70 
71     include_once libfile('function/profile');
72     $html = showdistrict($values, $elems, $container, $showlevel, $containertype);
73 }

 

3、static/js/common.js 这个js文件包含了在对地区每个下拉框出发的click事件,然后进行的ajax无刷新处理。

这个showdistrict的js方法,是作为中间的一个跳板,衔接前台的html页面获取参数和后台的php代码处理参数,最终通过ajax方式实现地区选择的无刷新效果。

 1 function showdistrict(container, elems, totallevel, changelevel, containertype) {
 2     var getdid = function(elem) {
 3         var op = elem.options[elem.selectedIndex];
 4         return op['did'] || op.getAttribute('did') || '0';
 5     };
 6     var pid = changelevel >= 1 && elems[0] && $(elems[0]) ? getdid($(elems[0])) : 0;
 7     var cid = changelevel >= 2 && elems[1] && $(elems[1]) ? getdid($(elems[1])) : 0;
 8     var did = changelevel >= 3 && elems[2] && $(elems[2]) ? getdid($(elems[2])) : 0;
 9     var coid = changelevel >= 4 && elems[3] && $(elems[3]) ? getdid($(elems[3])) : 0;
10     var sid = changelevel >= 5 && elems[4] && $(elems[4]) ? getdid($(elems[4])) : 0;
11     var url = "home.php?mod=misc&ac=ajax&op=district&container="+container+"&containertype="+containertype
12         +"&province="+elems[0]+"&city="+elems[1]+"&district="+elems[2]+"&community="+elems[3]+"&suite="+elems[4]
13         +"&pid="+pid + "&cid="+cid+"&did="+did+"&coid="+coid+"&sid="+sid+'&level='+totallevel+'&handlekey='+container+'&inajax=1';
14     ajaxget(url, container, '');
15 }

 

 

posted @ 2012-07-26 15:25  楼品玮  阅读(608)  评论(0编辑  收藏  举报