`

jsp动态显示数据库级联信息

    博客分类:
  • js
阅读更多
单页面动态生成数据的处理方法


<%@ page language="java" import="java.util.*,com.xaccp.entity.*,com.xaccp.dao.*" pageEncoding="GBK"%>
<html>
   <head>
   <%
   //通过服务器动态产生javascript代码(客户端脚本代码)
   StringBuffer sb = new StringBuffer(); //存储大类型的数组
   StringBuffer sbm = new StringBuffer(); //存储子类型的数组
  
   sb.append("<SCRIPT LANGUAGE = 'JavaScript'>");
   sb.append("\n var big = new Array();");
   sb.append("\n var small = new Array();");
  
   TypeListDao typeDao=new TypeListDao();
Map mapType=typeDao.selectMap();

List typeList=(List)mapType.get(new Integer(0)); //获得存储父类别的集合

for (int i = 0; i < typeList.size(); i++) {
TypeList mainType=(TypeList) typeList.get(i);
    sb.append(" \n big["+mainType.getTypeID()+"]=['"+mainType.getTypeName()+"'];");
//out.println(mainType.getTypeID()+":"+ mainType.getTypeName());

//根据父类别的类别细信息来获得相应父类别下的子类别集合
List sonListType=(List)mapType.get(new Integer(mainType.getTypeID()));
if (sonListType!=null) {//判断:如果相应的父类型下有子类型再去循环遍历。以免出现空指定异常
sb.append("\n small["+mainType.getTypeID()+"]=[");
for (int j = 0; j < sonListType.size(); j++) {
TypeList sonType=(TypeList)sonListType.get(j);
    sb.append("'"+sonType.getTypeID()+"-"+sonType.getTypeName()+"'");
    if(j<sonListType.size()-1)
    sb.append(",");
}
sb.append("];\n");
}
}
sb.append("</SCRIPT>\n");
    %>
    <%=sb.toString() %>
   <script type="text/javascript">
     //为类别下拉框添加选项
     function initBig()
     {
         var selBig = document.getElementById("selBig");
         selBig.options.add(new Option("==请选择大类=="),0);
         for(i in big)
         {
            var op = new Option(big[i],i);
            selBig.options.add(op); 
         }
     }
    
     //为栏目下拉框添加选项
     function fillSmall(bId)
     {
document.getElementById("selSmall").options.length=0;//清空原下拉列表中的项
        if(small.length>bId)
        {   
        var selSmall = document.getElementById("selSmall");
        for(var i =0;i<small[bId].length;i++)
        {
             var name =small[bId][i].substring(small[bId][i].indexOf('-')+1);
             var id =small[bId][i].substring(0,small[bId][i].indexOf('-'))
             selSmall.options.add(new Option(name,id));
        }
       
        }
        else
        { 
        //提示当前类别中无子栏目,
           if(confirm("请添加当前大类中的小类 !\r\n要添加吗?"))
           {
             //location.href="#";在这里进行连接,链接到添加类别页面
           }
        }
    
     }
  
   </script>
  
   </head>
  
   <body onLoad="initBig()">

<select id="selBig" onChange="fillSmall(this.value)">
     
</select>  

<select id="selSmall">
     
</select>
   </body>

</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics