下拉列表多级联动dropDownList示例代码
程序员文章站
2022-06-20 13:13:49
视图: cdnauto/views/config/index.php 复制代码 代码如下: echo chtml::dropdownlist('node', '', cht...
视图:
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'];
}
其它:
数据表结构
效果:
没有选择节点前:
细节有空再补上了。
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'];
}
其它:
数据表结构
效果:
没有选择节点前:
细节有空再补上了。