下拉列表多级联动dropDownList示例代码

前端之家收集整理的这篇文章主要介绍了下拉列表多级联动dropDownList示例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

视图:
cdnauto/views/config/index.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="75962" class="copybut" id="copybut75962" onclick="doCopy('code75962')"> 代码如下:

<div class="codebody" id="code75962">
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
<div class="codetitle"><a style="CURSOR: pointer" data="67676" class="copybut" id="copybut67676" onclick="doCopy('code67676')"> 代码如下:
<div class="codebody" id="code67676">
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
<div class="codetitle"><a style="CURSOR: pointer" data="45650" class="copybut" id="copybut45650" onclick="doCopy('code45650')"> 代码如下:
<div class="codebody" id="code45650">
/*
通过节点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
<div class="codetitle"><a style="CURSOR: pointer" data="85171" class="copybut" id="copybut85171" onclick="doCopy('code85171')"> 代码如下:
<div class="codebody" id="code85171">
/*
通过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'];
}

其它:
数据表结构
效果
没有选择节点前:

<IMG src="https://files.jb51.cc/file_images/article/201306/201306271718331.gif?201352717193"&gt;

细节有空再补上了。

下拉列表多级联动

猜你在找的PHP相关文章