JSP中如何制作树形选择框
这项机关的值在数据库中有个对应的表,怎样以树型形式显示并且选择? 展开
2016-01-11 · 做真实的自己 用良心做教育
JSP中可以引用jquery控件来制作树形选择框。
其实就是联动下拉框,参考实现代码:
<!DOCTaYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="keywords" content=" keywords" />
<meta name="description" content="description" />
</head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:13px}
.clsInit{width:435px;height::35px;line-height:35px;padding-left:10px}
.clsTip{padding-top:5px;background-color:#eee;display:none}
.btn{border:solid 1px #666;padding:2px;width:65px;float:right;margin-top:6px;margin-right:6px;filter:progid:DXImageTransform.Mcrosoft.Gradient(GraientType=0,StartColorStr=#FFFFFF,EndColorStr=#ECE9D8);}
</style>
<body>
<script type="text/javascript">
$(function(){
function objInit(obj){
return $(obj).html('<option>请选择</option>');
}
var arrData={
厂商1:{品牌一:'型号1-1-1,型号1-1-2',
品牌二:'型号1-2-1,型号1-2-2'},
厂商2:{品牌一:'型号2-1-1,型号2-1-2',
品牌二:'型号2-2-1,型号2-2-2'},
厂商3:{品牌一:'型号3-1-1,型号3-1-2',
品牌二:'型号3-2-1,型号3-2-2'}
};
$.each(arrData,function(pF){
$('#selF').append('<option>'+pF+'</option>');
});
$('#selF').change(function(){
objInit('#selT');
objInit('#selC');
$.each(arrData,function(pF,pS){
if($('#selF option:selected').text()==pF){
$.each(pS,function(pT,pC){
$('#selT').append('<option>'+pT+'</option>');
});
$('#selT').change(function(){
objInit('#selC');
$.each(pS,function(pT,pC){
if($('#selT option:selected').text()==pT){
$.each(pC.split(","),function(){
$('#selC').append('<option>'+this+'</option>');
})
}
})
});
}
})
});
});
</script>
<div class="clsInit">
厂商:<select id="selF"><option>请选择</option></select>
品牌:<select id="selT"><option>请选择</option></select>
型号:<select id="selC"><option>请选择</option></select>
<input type="button" value="查询" id="Button1" class="btn" />
</div>
<div class="clsInit" id="divTip"></div>
</body>
</html>
效果:
2013-06-24
/**获取商品分类列表,并调整为select组件的option*/
public String[][] getCategory()
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
IMerchandiseService service=(MerchandiseService)context.getBean("MerchandiseService");
String[][] options = null;
try{
List list = service.getAllCategorys();
Category category = null;
if (list!=null)
{
int i = 1;
options = new String[list.size()+1][2];
options[0][0] =new Long(Category.ROOT_ID).toString();
options[0][1] ="顶级分类";
Iterator it = list.iterator();
while(it.hasNext())
{
category = (Category)it.next();
options[i][0] =category.getCategoryId().toString();
StringBuffer s=new StringBuffer();
for(int l=1;l<=category.getLevel();l++)
{
if(l==category.getLevel())
{
s.append("->");
break;
}
s.append(". ");
}
s.append(category.getCategoryName());
options[i][1] =s.toString();
i++;
}
}
else
{
options = new String[1][2];
options[0][0] =new Long(Category.ROOT_ID).toString();
options[0][1] ="顶级分类";
}
}
catch(Exception ex)
{
logger.info("执行JsonRpcBean类的getCategory()时出错:\n");
ex.printStackTrace();
}
return options;
}
以下是页面里的代码:
<select id="category"></select>
<script type="text/javascript">
//构造商品分类下拉列表
jsonrpc = new JSONRpcClient("JSON-RPC");
var result = jsonrpc.ajax.getCategory();
for (var i=0;i<result.length;i++)
{
option =document.createElement("OPTION");
option.value = result[i][0];
option.text = result[i][1];
document.getElementById("category").options.add(option);
}
</script>
不懂的话,去看看json-rpc的用法
2013-06-24
2013-06-24
2013-06-24