Discuz教程网

asp无限级分类加js收缩伸展功能代码

[复制链接]
authicon dly 发表于 2010-11-6 18:10:03 | 显示全部楼层 |阅读模式
为了方便使用分类,我定义了一个分类表category,里面字段是
id(自动编号)  cat_name(分类名) parent_id(父ID,对应本表ID) cat_order(顺序) is_show(是否显示)  u_id(这个用来区别是新闻分类,还是产品分类,还是其他分类),为了方便,我将这些分类全部放在这张表中。
在给客户添加分类的时候,结果有太多的分类,本来前台显示的时候,将它们全部显示出来了,好长。客户提出修改意见,要求将它们改成点击大分类,才可以将其子分类显示出来,并且每个分类下面还有一条虚线,并且大分类前面有个图片加号,展开后要变成减号。
  1. <script>
  2. function fd(id,num)
  3. {
  4. t=$("c"+id+"_1").style.display;

  5. if(t=="none")
  6. {
  7.    t1="block";
  8.    t2="images/fll_34.gif";
  9. }
  10. else
  11. {
  12.   t1="none";
  13.   t2="images/fll_34.gif";
  14. }
  15. for(i=1;i<=num;i++)
  16. {
  17.   $("c"+id+"_"+i).style.display=t1;
  18.   $("d_"+id).src=t2;  
  19. }
  20. }
  21. function $(id)
  22. {
  23. return document.getElementById(id);
  24. }
  25. </script>
复制代码


这是asp无限级显示分类代码,并给这些分类加上id
  1. <%
  2. '功能:asp无限级显示分类+js显示与隐藏
  3. '作者:wangsdong
  4. '开发:www.aspprogram.cn
  5. '参数:parent_id为父ID,stype为新闻,产品,文章大分类
  6. '原创文章,转载请保留些信息,谢谢
  7. function cat111(parent_id,stype)
  8. set rs1 =server.createobject("adodb.recordset")
  9. sql="select cat_name,cat_id,parent_id from category where parent_id="&parent_id&" and u_id="&stype&" and is_show=1 order by cat_order asc"
  10. set rs1=conn.execute(sql)
  11. If rs1.eof Then
  12. Else
  13. if(depath>2) then
  14. display2="none"     
  15. else
  16. display2="block"
  17. end if  
  18. dim j
  19. j=1
  20. do while not rs1.eof
  21. cat_name1 = rs1("cat_name")
  22. cat_id1 = rs1("cat_id")  
  23. parent_id1=rs1("parent_id")
  24. '******************下面是你要显示的******************'
  25. m9=0
  26. sql2="select count(cat_id) as t from category where parent_id="&cat_id1&" and u_id="&stype&""   
  27. set rs2=server.createobject("adodb.recordset")
  28. set rs2=conn.execute(sql2)
  29. if not rs2.eof then
  30.     m9=rs2("t")
  31. else
  32.   m9=0  
  33. end if
  34. rs2.close
  35.   
  36. if(depath<=2) then  
  37.   mgif="images/-.gif"
  38.   a="block"
  39. else
  40.   if(m9>0) then
  41.   mgif="images/+.gif"
  42.   else
  43.   mgif="images/-.gif"
  44.   end if
  45.   if(depath=4) then
  46.    a="block"
  47.   else  
  48.    a="none"
  49.   end if
  50. end if  


  51. catstr=catstr & "<tr id=""c"&parent_id&"_"&j&""" style=""display:"&a&"""><td width=""25"" align=""center"" valign=""middle"" class=""dotted_class""><img src="""&mgif&""" width=""12"" height=""11"" id=""d_"&cat_id1&"""></td><td class=""dotted_class leftcatcss"">"
  52. if(m9>0) then
  53.   catstr=catstr&"<a href=""javascript:void(0);"" _fcksavedurl="""javascript:void(0);""" _fcksavedurl="""javascript:void(0);""" onclick=""fd("&cat_id1&","&m9&")"">" &vbnewline
  54. else
  55.   catstr=catstr&"<a href=""products.asp?id="&cat_id1&""" target=""_blank"">"&vbnewline
  56. end if
  57. for i=1 to depath
  58.    catstr=catstr&" "
  59. Next
  60. catstr=catstr&cat_name1&"</a></td></tr>"&vbnewline
  61. m9=0
  62.      
  63. sql2="select cat_name,cat_id from category where parent_id="&parent_id1&" and u_id="&stype&" order by cat_order asc"   
  64. set rs2=server.createobject("adodb.recordset")
  65. set rs2=conn.execute(sql2)
  66. if not rs2.eof then
  67.     depath=depath+4  
  68.     call cat111(cat_id1,stype)
  69. end if
  70. rs2.close
  71. set rs2=nothing
  72. depath=depath-4
  73. '******************上面是你要显示的******************'
  74. j=j+1
  75. rs1.movenext
  76. loop
  77. End If
  78. rs1.close
  79. set rs1=nothing
  80. end Function
  81. %>
复制代码
在使用这个函数之前加上
catstr=""
然后再call,下面的虚线加在css中的dotted_class里面,这样就搞定了。
注意:这里得到的是一个表格的行,要和其他行放在一起,不要加到td里面去了。



上一篇:动态程序防采集的新方法
下一篇:每个ASP程序员必备的知识
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 05:43

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表