最近項目用PHP,不太熟,用架構開發,熟悉過程中,記錄下來,備查
標籤庫select標籤使用
<tagLib name="html" /><html:select options="productCategory"
selected="prodCateId" first="選擇分類" name="prod_cate_id" id="prod_cate_id"
/> $Cate
= D('ProductCategory');$list
= $Cate->field('prod_cate_id,prod_cate_name')->findAll();foreach ($list as $vo){ $groupList[$vo['prod_cate_id']] = $vo['prod_cate_name'];}$this->assign('productCategory',$groupList);$this->assign('prodCateId','');$this->display('edit');
<volist>標籤的嵌套使用
<volist name="list" id="vo">
{$vo.prod_cate_name} <volist name="vo['voo']"
id="sub"> {$sub.prod_cate_name} </volist>
</volist> $Dao
= M('ProductCategory');$list
= $Dao->where('ifnull(prod_cate_pid,0)=0')->order('prod_cate_order asc')->findAll();$Sub
= M('ProductCategory');foreach ($list as $n=>$val){ $list[$n]['voo']=$Sub->where('prod_cate_pid='.$val['prod_cate_id'])->select();}$this->assign('list',$list);
$this->display(); AJAX二級聯動的實現 <script type="text/javascript"
src="__ROOT__/public/scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">function ajaxSelect(url,objone,objtwo,defval){
var $s1=$(objone); var $s2=$(objtwo); $s1.change(function(){ var curval = objone.options[objone.selectedIndex].value; $s2.html(""); $("<option>").text("二級分類").val("").appendTo($s2); $.ajax({ type: "POST", url: url, data: "catepid="+curval, dataType: 'json', success: function(obj){ if(obj.data) { $(eval(obj.data)).each(function(i,elm){ appendOptionTo($s2,elm.prod_cate_name,elm.prod_cate_id,defval); }); } } }); }).change(); function appendOptionTo(o,k,v,d){ var opt=$("<option>").text(k).val(v); if(v==d){opt.attr("selected", "selected")} opt.appendTo(o); }}$(function(){ ajaxSelect('__URL__/changeCategory',$('#cate_one').get(0),$('#cate_two').get(0),'{$vo.cate_two}');
});
</script> <tagLib name="html" />
<html:select options="productCategory"
selected="prodCateId" first="選擇分類" name="cate_one" id="cate_one" />
<select name="cate_two" id="cate_two"><option value="">二級分類</option></select>
public function
changeCategory(){ $id= $_POST['catepid']; header("Content-type:text/html;
charset=utf-8"); $Cate
= D('ProductCategory'); $list
= $Cate->field('prod_cate_id,prod_cate_name')->where('prod_cate_pid='.$id)->select(); $this->ajaxReturn(json_encode($list),'','',0);
}