Home > Article > Web Front-end > Example analysis of ajax cascading menu implementation method
The example in this article describes the implementation method of ajax cascading menu. Share it with everyone for your reference, the details are as follows:
The effect is as follows:
Select the first item, and the content of the second and third items will change accordingly.
Select the second item, and the content of the third item will change accordingly.
The third item does not affect the first and second items.
There are a few points worth mentioning:
1. Is 1.html front-end splicing or background splicing?
I chose front-end splicing, which can save traffic and back-end resources. This is also more in line with program processing. Generally, the background is only responsible for providing data.
Pass it to the front desk through json, and then the front desk will get it for processing.
ajax function
function ajaxgetbigclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetbigclass", data:"typeid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; } function ajaxgetsmallclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetsmallclass", data:"bigclassid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; }
Backend ajax processing code
case 'ajaxgetbigclass': $typeid = trim($this->_getParam('typeid')); $daoNews = new dao_news(); if(isset($typeid)){ $bigClass = $daoNews->getBigClassByType($typeid,true); if($bigClass){ $json = json_encode($bigClass); echo $json; }else{ echo FALSE; } }else{ echo FALSE; } break; case 'ajaxgetsmallclass': $bigclassid = trim($this->_getParam('bigclassid')); $daoNews = new dao_news(); if(isset($bigclassid)){ $smallClass = $daoNews->getSmallClassByBigClass($bigclassid,true); if($smallClass){ $json = json_encode($smallClass); echo $json; }else{ echo FALSE; } }else{ echo FALSE; } break;
Call the ajax function and splice it into html function
function setbigclass(id,flag){ var flag = arguments[1] ? arguments[1] : false;//默认值 var res = ajaxgetbigclass(id); //alert(res); if(res){ myobj = eval(res); for(var i=0;i<myobj.length;i++){ strHtml+="<option value='"+myobj[i].id+"'>"+myobj[i].name+"</option>"; } $("#bigclassid").html(strHtml); }else{ var strHtml = "<option value=''>无子选项</option>"; $("#bigclassid").html(strHtml); } if(flag&&res){ return myobj[0].id; } } function setsmallclass(id){ var res = ajaxgetsmallclass(id); //alert(res); if(res){ myobj = eval(res); var strHtml = "<option value=''>请选择</option>"; for(var i=0;i<myobj.length;i++){ strHtml+="<option value='"+myobj[i].id+"'>"+myobj[i].name+"</option>"; } $("#smallclassid").html(strHtml); }else{ var strHtml = "<option value=''>请选择</option><option value=''>无子选项</option>"; $("#smallclassid").html(strHtml); } }
main function, event action
$(function(){ //ajax级联 $("#typeid").change(function(){ var id = $(this).val(); var res = setbigclass(id,true); if(res){ setsmallclass(res); }else{ setsmallclass(0); } }); $("#bigclassid").change(function(){ var id = $(this).val(); setsmallclass(id); }); });
2. Functionalization of background query.
public function getType($where = false, $order = 'typeid ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_typename,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getTypeName($flag=false){ $where = array(); $aType = $this->getType($where); if($aType){ if($flag){ foreach ($aType as $key => $value) { $type[$key]['id'] = $value['typeid']; $type[$key]['name'] = $value['typename']; } return $type; }else{ foreach ($aType as $key => $value) { $type[$value['typeid']] = $value['typename']; } return $type; } }else{ return false; } } public function getBigClass($where = false, $order = 'BigClassID ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_bigname,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getBigClassByType($typeid = 60,$flag=false){ $where = array(); $where['BigClass.typeid =?'] = array("type"=>1,"val"=>$typeid); //print_r($where);exit; $from = array('BigClassID',"BigClassName","convert(text,BigClassMaster) as BigClassMaster","typeid"); $aBigClass = $this->getBigClass($where, false, false, false, false,$from); if($aBigClass){ if($flag){ foreach ($aBigClass as $key => $value) { $bigClass[$key]['id'] = $value['BigClassID']; $bigClass[$key]['name'] = $value['BigClassName']; } return $bigClass; }else{ foreach ($aBigClass as $key => $value) { $bigClass[$value['BigClassID']] = $value['BigClassName']; } return $bigClass; } }else{ return false; } } public function getSmallClass($where = false, $order = 'SmallClassID ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_smallname,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getSmallClassByBigClass($BigClassID = 221,$flag=false){ $where = array(); $where['SmallClass.BigClassID =?'] = array("type"=>1,"val"=>$BigClassID); //print_r($where);exit; $aSmallClass = $this->getSmallClass($where); if($aSmallClass){ if($flag){ foreach ($aSmallClass as $key => $value) { $smallClass[$key]['id'] = $value['SmallClassID']; $smallClass[$key]['name'] = $value['smallclassname']; } return $smallClass; }else{ foreach ($aSmallClass as $key => $value) { $smallClass[$value['SmallClassID']] = $value['smallclassname']; } return $smallClass; } }else{ return false; } }
This way it can be used in multiple places and at multiple angles.
3. Front-end js, documented, js with the same function are placed in one js file. The content is finally functionalized.
<script type="text/javascript" src="/js/news/cascade.js"></script> <tr> <td width="20%" height="56" align="right" >请选择分类:</td> <td width="80%" style="padding:10px;"> <select id="typeid" name="typeid" class=" ffb-input"> <!--{html_options options=$aType selected=$aData.typeid|default:'0'}--> </select> > <select id="bigclassid" name="bigclassid" class=" ffb-input"> <!--{html_options options=$aBigClass selected=$aData.bigclassid|default:'0'}--> </select> > <select id="smallclassid" name="smallclassid" class=" ffb-input"> <option value="">请选择</option> <!--{html_options class=" ffb-input" options=$aSmallClass selected=$aData.smallclassid|default:'0'}--> </select> </td> </tr>
This will make the document very clear.
Optimized js
$(function(){ //ajax级联 $("#typeid").change(function(){ var id = $(this).val(); setbigclass(id); }); $("#bigclassid").change(function(){ var id = $(this).val(); setsmallclass(id); }); }); function setbigclass(id){ var res = ajaxgetbigclass(id); var strHtml; if(res){ myobj = eval(res); for(var i=0;i"+myobj[i].name+""; } $("#bigclassid").html(strHtml); $("#bigclassid").show().change(); }else{ $("#bigclassid").hide(); $("#smallclassid").hide(); } } function setsmallclass(id){ var res = ajaxgetsmallclass(id); if(res){ myobj = eval(res); var strHtml = ""; for(var i=0;i "+myobj[i].name+""; } $("#smallclassid").html(strHtml); $("#smallclassid").show(); }else{ $("#smallclassid").hide(); } } function ajaxgetbigclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetbigclass", data:"typeid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; } function ajaxgetsmallclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetsmallclass", data:"bigclassid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; }