ThinkPHP2.1模版使用笔记 - 模版嵌套/AJAX级联
最近项目用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){
</script> 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}');
});<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);
}