The dropDownList sample code is linked to multiple levels in the drop-down list. View: cdnautoviewsconfigindex. the php copy code is as follows: echoCHtml: dropDownList (node, CHtml: listData (Node: model ()-findAll (), name, name ), array (empty -- select View:
Cdnauto/views/config/index. php
The code is as follows:
Echo CHtml: dropDownList ('node', '', CHtml: listData (node: model ()-> findAll (), 'name', 'name '), array ('empty' => '-- select a node --',
'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 ('-- Select Server --'));
Controller:
Cdnauto/controllers/ConfigController. php
The code is as follows:
Public function actionGetNodeServersByNodeName (){
// If (! Yii: app ()-> request-> isAjaxRequest)
// Throw new CHttpException (404 );
$ Node_name = $ _ POST ['node _ name'];
$ Nodeid = Node: model ()-> getNodeId ($ _ POST ['node _ name']); // Obtain the node ID using the Node name
$ Server = GossServer: model ()-> getServerByNodeid ($ nodeid); // Obtain server information through node ID
// $ Server is an array type, such as $ server = array ('name' => 'name1'), array ('name' => 'name2 ')); so two foreach requests are required.
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 );
}
}
Model:
GossServer. php
The code is as follows:
/**
* Obtain the names of all servers under the node by using the node 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 ("The server corresponding to this node cannot be found ");
}
Return $ ret;
}
Node. php
The code is as follows:
/**
* Get the nodeid name through nodename
* @ 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} Not Found }");
}
Return $ ret [0] ['id'];
}
Others:
Data table structure
Effect:
Before selecting a node:
Make up the details.
Export cdnauto/views/config/index. the php code is as follows: echo CHtml: dropDownList ('node', '', CHtml: listData (node: model ()-findAll (), 'name ', 'name'), array ('empty' = '-- select...