|
首先本人引用的是http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=5236这个贴子中的代码,但是这个贴子中的代码直接是运行不起来的,经过两天查找问题后,终于搞定.
首先region_change.php中select_children函数返回的json_encode是不支持中文的,所以这个函数有必要改造一下
那么我现在把所有代码贴出来,如下
1、控制器中region_change.php
<?php
class Region_change extends CI_Controller {//CI中类名首字母大写
function __construct() {
parent::__construct();
}
function select_children()
{
function ch_json_encode($data) {
function ch_urlencode($data) {
if (is_array($data) || is_object($data)) {
foreach ($data as $k => $v) {
if (is_scalar($v)) {
if (is_array($data)) {
$data[$k] = urlencode($v);
} else if (is_object($data)) {
$data->$k = urlencode($v);
}
} else if (is_array($data)) {
$data[$k] = ch_urlencode($v); //递归调用该函数
} else if (is_object($data)) {
$data->$k = ch_urlencode($v);
}
}
}
return $data;
}
$ret = ch_urlencode($data);
$ret = json_encode($ret);
return urldecode($ret);
}
$segments = $this->uri->uri_to_assoc();
$this->load->model('region_model');
$data['children'] = $this->region_model->children_of($segments['parent_id']);
echo ch_json_encode($data['children']); //返回json格式的数据
}
}//原文中直接用的json_encode返回的不支持中文。
2、模型中region_model.php
<?php
class Region_Model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function provinces()
{
//调用children_of(1)函数,并将parent_id=1;这样的话,就取出了所有parent_id为1的值,即所有的省份
return $this->children_of(1);
}
function children_of($parent_id, $select="*")
{
$parent_id = (int)$parent_id;
//echo $parent_id;
$regions = array();
$this->db->select($select);
$this->db->where('parent_id', $parent_id);
//从region表中由parent_id选取所有的子集
if ($query = $this->db->get('ps_region')){
$res=$query->result_array();
// print_r($res);
// var_dump($res);
// exit;
return $query->result_array(); //返回一个关联数组
}
return array();
}
}
?>
3、视图中district_select.php
<?php
$CI = get_instance();
$CI->load->model('region_model', 'region');
$provinces = $CI->region->provinces();
$citys = $CI->region->children_of($province_selected);
?>
<script language="JavaScript">
<!--
<?php if(isset($province_selected)):?>
var province_selected = <?php echo (int)$province_selected?>;
<?php else:?>
var province_selected = 0;
<?php endif?>
<?php if(isset($city_selected)):?>
var city_selected = <?php echo (int)$city_selected?>;
<?php else:?>
var city_selected = 0;
<?php endif?>
<?php if(isset($district_selected)):?>
var district_selected = <?php echo (int)$district_selected?>;
<?php else:?>
var district_selected = 0;
<?php endif?>
$(document).ready(function() {
var change_city = function(){
$.ajax({
url: '<?php echo site_url('region_change/select_children/parent_id')?>'+'/'+$('#province_id').val(),
type: 'GET',
dataType: 'html',
success: function(data){
city_json = eval('('+data+')');
var city = document.getElementById('city_id');
city.options.length = 0;
city.options[0] = new Option('请选择', -1);
for(var i=1; i<city_json.length+1; i++){
var len = city.length;
city.options = new Option(city_json[i-1].region_name, city_json[i-1].region_id);
//if (city.options.value == city_selected){
// city.options.selected = true;
//}//如需要默认选择的时候就需要加上这段
}
change_district();//重置地区
}
});
}
change_city();//初始化城市
$('#province_id').change(function(){
change_city();
});
var change_district = function(){
$.ajax({
url: '<?php echo site_url('region_change/select_children/parent_id')?>'+'/'+$('#city_id').val(),
type: 'GET',
dataType: 'html',
success: function(data){
district_json = eval('('+data+')');
var district = document.getElementById('district_id');
district.options.length = 0;
district.options[0] = new Option('请选择', -1);
for(var i=1; i<district_json.length+1; i++){
// var len = district.length;
district.options = new Option(district_json[i-1].region_name, district_json[i-1].region_id);
// if (district.options[len].value == district_selected){
// district.options[len].selected = true;
//}//如需要默认选择的时候就需要加上这段
}
}
});
}
change_district();//初始化县区
$('#city_id').change(function(){
change_district();
});
});
//-->
</script>
<select name="province_id" id="province_id" style="width:100px;">
<?php foreach($provinces as $key => $province): ?>
<option value="<?php echo $province['region_id']; ?>" ><?php echo $province['region_name']; ?></option>
<?php endforeach; ?>
</select>
<select name="city_id" id="city_id" style="width:100px;">
<?php foreach($citys as $key => $city): ?>
<option value="<?php echo $city['region_id']; ?>" ><?php echo $city['region_name']; ?></option>
<?php endforeach; ?>
</select>
<select name="district_id" id="district_id" style="width:100px;">
<option value=""></option>
</select>
4、控制器中引用页我就随便用welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
}
public function index()
{
header("Content-type:text/html;charset=Utf-8");
$data['province_selected']=6;
$data['city_selectd'] =76;
$data['district_selected'] =693;
$this->load->view('welcome_message',$data);
}
}
5、视图中的引用页我就用的是welcome_message.php
<html>
<head>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<?php
$data['province_selected']=$province_selected;
$data['city_selectd'] =$city_selectd;
$data['district_selected'] =$district_selected;
$this->load->view('widget/district_select',$data);
?>
</body>
</html>
好了,至此代码已全部贴完了,如果还有什么问题QQ:38829979我
|
|