Yii中的二級分類

HTML部分
javascript

<span style="font-family:Microsoft YaHei;font-size:12px;"><select name="id" id="column_id">
	<?php foreach( (array) $columnListOne as $key => $val ):?>
		<option value="<?php echo $key;?>"><?php echo $val;?></option>
	<?php endforeach;?>
</select>
<select class="ml_5" name="column" id="columnlist">
	<option value="">請選擇分類</option>
	<?php foreach( (array) $columnListTwo as $key => $val ):?>
		<option value="<?php echo $key;?>"><?php echo $val;?></option>
	<?php endforeach;?>
</select></span>

jQuery部分

<span style="font-family:Microsoft YaHei;font-size:12px;"><script type="text/javascript">
$(document).ready(function(){
   /**
	* 二級聯動
	*/
	$("#column_id").change(function(){
		var id=$(this).val();
		$.post("/consult/list",{"id":id},function(list){
			var data = eval( '(' + list + ')');
			var html = '<option value="">請選擇分類</option>';
			if( data.status == 1 )
			{
				for ( var i=0; i<data.list.length; i++){
					html += '<option value="'+ data.list[i].id +'">' + data.list[i].name + '</option>';
				}
				$("#columnlist").html(html);
			}
			else
			{
				$("#columnlist").html(html);
			}
		});
	});
});
</script></span>

控制器部分

<span style="font-family:Microsoft YaHei;font-size:12px;">/**
 * ajax獲取二級分類
 */
public function actionList()
{
		$id = Yii::app() -> request -> getParam('id');
	if ( !empty( $id ) )
	{
		$columnList = ConsultColumn::model() -> findAll("parent_id = {$id} AND is_hidden = 0");
		foreach ( (array) $columnList as $val )
		{
			$columnListTwo[] = array(
				'id' => $val -> id,
				'name' => $val -> column_name,
			);
		}
		Util::json(array('status' => 1, 'msg' => '成功', 'list' => $columnListTwo), 1);
	}
	else
	{
		Util::json(array('status' => 0, 'msg' => '失敗'), 1);
	}
}</span>
相關文章
相關標籤/搜索