Drop-down List Multilevel linkage DropDownList sample code

Source: Internet
Author: User
Tags foreach config yii

View:
cdnauto/views/config/index.php

Copy Code code as follows:


echo CHtml::d ropdownlist (' node ', ', ', Chtml::listdata (Node::model ()->findall (), ' name ', ' name '), Array (' Empty ' => '--Please 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::d ropdownlist (' Servers ', ', ', Array ('--Please select Server-'));


Controller:
cdnauto/controllers/configcontroller.php

Copy Code code 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 ')); Get the node ID through the node name


$server = Gossserver::model ()->getserverbynodeid ($nodeid); Get server information through Node ID


//$server is an array type, such as $server = Array (' name ' => ' name1 '), Array (' name ' => ' name2 '); so it takes two times to 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);


}


}


Model:
gossserver.php

Copy Code code as follows:


/**


* Get all the server names under this node through 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 ("Cannot find the server for this node");


}


return $ret;


}


node.php

Copy Code code as follows:


/**


* Get 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 ("Cannot Find node{$name}");


}


return $ret [0][' id '];


}


Other:
Data table structure
Effect:
Before a node is selected:

The details are free to fill up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.