Posted in PHP onJune 27, 2013
视图:
cdnauto/views/config/index.php
echo CHtml::dropDownList('node', '', CHtml::listData(Node::model()->findAll(),'name','name'),array('empty'=>'--请选择节点--', 'id' => 'node', 'ajax'=>array( 'type'=>'POST', 'url'=>Yii::app()->createUrl('cdnauto/config/getNodeServersByNodeName'), 'update'=>'#servers', 'data'=>array('node_name'=>'js:$("#node").val()'), ) ) ); echo " "; echo CHtml::dropDownList('servers', '', array('--请选择服务器--'));
控制器:
cdnauto/controllers/ConfigController.php
public function actionGetNodeServersByNodeName(){ // if(!Yii::app()->request->isAjaxRequest) // throw new CHttpException(404); $node_name = $_POST['node_name']; $nodeid = Node::model()->getNodeId($_POST['node_name']); //通过节点名称获取该节点ID $server = GossServer::model()->getServerByNodeid($nodeid); //通过节点ID获取服务器信息 //$server 为array类型,形如 $server = array(array('name'=>'name1'),array('name'=>'name2'));所以需要两次foreach if(isset($server)){ foreach ($server as $k=>$v){ foreach($v as $kk => $vv){ echo CHtml::tag('option', array('value'=>$kk), CHtml::encode($vv), true); } } }else{ echo CHtml::tag('option', array('value'=>''), 'servers', true); } }
模型:
GossServer.php
/** * 通过节点ID获取该节点下所有的服务器名称 * @author ysdaniel */ public static function getServerByNodeid($nodeid) { $sql = "SELECT name FROM OSS_Server WHERE nodeid = '{$nodeid}' "; ///$sql = "SELECT name,nodeid FROM OSS_Server WHERE nodeid = '{$nodeid}' "; //both ok $cmd = Yii::app()->db->createCommand($sql); $ret = $cmd->queryAll(); if (!$ret){ throw new Exception("找不到这个节点对应的服务器"); } return $ret; }
Node.php
/** * 通过nodename获取nodeid名 * @author */ public static function getNodeId($name) { $sql = "SELECT id FROM OSS_Node WHERE name = '{$name}'"; $cmd = Yii::app()->db->createCommand($sql); $ret = $cmd->queryAll(); if (!$ret){ return null; //throw new Exception("找不到Node{$name}"); } return $ret[0]['id']; }
其它:
数据表结构
效果:
没有选择节点前:
细节有空再补上了。
下拉列表多级联动dropDownList示例代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@