Implementation of the infinite classification algorithm JS

Source: Internet
Author: User
Tags array include key modify query split
js| algorithm

Through ASP dynamic generation of JS array, and then JS in the client to arrange, the largest reduction of server-side pressure. (Server-side only query once, no other action)

Database design:

ID: Index, AutoNumber
Mc:varchar 100 bytes, record category name
FL: number, records belong to which category, 0 is the root category
CEng: Number, records in the number of layers of the root classification of 0 layer, then increase by 1

The generated HTML code is as follows:

Run Code Box

<ptml><pead><title>fenlei</title><meta http-equiv= "Content-Type" content= "text/html; charset=gb2312 "></pead><body bgcolor=" #FFFFFF "text=" #000000 "><script language=" JavaScript "> <!--var DataArray = {};var Temparray = {};var xx= ""; temparray[0]= "0@_!_#_$_|_@1@_!_#_$_|_@test@_!_#_$_|_@0"; Temparray[1]= "1@_!_#_$_|_@2@_!_#_$_|_@test1@_!_#_$_|_@1"; temparray[2]= "2@_!_#_$_|_@3@_!_#_$_|_@test1-test2@_!_ #_ $_|_@2 "; temparray[3]=" 0@_!_#_$_|_@4@_!_#_$_|_@test@_!_#_$_|_@0 "; temparray[4]=" 4@_!_#_$_|_@5@_!_#_$_|_@test5@ _!_#_$_|_@1 "; temparray[5]=" 2@_!_#_$_|_@6@_!_#_$_|_@aaa@_!_#_$_|_@2 "; temparray[6]=" 2@_!_#_$_|_@7@_!_#_$_|_@sss@ _!_#_$_|_@2 "; var y=0;function loadnexttype (Upid) {for (var key in Temparray) {var temps=temparray[key].split (" @_!_#_$_| _@ "); var z=temps[1];if (temps[0]==upid) {y+=1;xx=" "; for (K=0;k<parseint (temps[3)); k++) {if (K!=parseint (temps[3)) -1) {xx+= " ";} else{xx+= " ◇";} dataarray[y]=temps[0]+ "@_!_#_$_|_@" +temps[1]+ "@_!_#_$_|_@" +xx+teMps[2];loadnexttype (z);}} for (var key in Temparray) {var tmp=temparray[key].split ("@_!_#_$_|_@"); if (tmp[0]==0) {dataarray[y]=tmp[0]+ "@_!_#_$_|" _@ "+tmp[1]+" @_!_#_$_|_@ "+" "+tmp[2];loadnexttype (Tmp[1]); y+=1;} --></script><span style= "border:1px solid #000000; Position:absolute; Overflow:hidden; "> <select name=" fenlei "style=" MARGIN:-2PX; " > <script language= "JavaScript" ><!--for (var key in DataArray) {var tp=dataarray[key].split ("@_!_#_$_|_@") ;d ocument.write (' <option value= ' +tp[1]+ ' > ' +tp[2]+ ' </option> ');} --></script></select></span> </body></ptml>

[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

The server-side ASP code is as follows:

Run Code Box

<!--#include file= ". /conn.asp "--><!--#include file=" ck.asp "--><ptml><pead><title>fenlei</title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "></pead><body bgcolor=" #FFFFFF "text=" #000000 "><script language=" JavaScript "> <!--var DataArray = {};var Temparray = {};var xx= ""; <%dim sqlset rs=server.createobject ("Adodb.recordset") sql= " SELECT * from Fenlei "Rs.Open Sql,conn,1,1dim xx=0do while not Rs.eofresponse.write" temparray["& X &"]= "" "& RS ("FL") & "@_!_#_$_|_@" &rs ("id") & "@_!_#_$_|_@" & RS ("MC") & "@_!_#_$_|_@" & RS ("CEng") & "";" X=x+1rs.movenextloop%>var y=0;function Loadnexttype (upid) {for (var key in Temparray) {var temps=temparray[key]. Split ("@_!_#_$_|_@"); var z=temps[1];if (temps[0]==upid) {y+=1;xx= ""; for (K=0;k<parseint (temps[3)); k++) {if (k!= parseint (Temps[3])-1 {xx+= " ";} else{xx+= " ◇";} dataarray[y]=temps[0]+ "@_!_#_$_|_@" +temps[1]+ "@_!_#_$_|_@ "+xx+temps[2];loadnexttype (z);}}} for (var key in Temparray) {var tmp=temparray[key].split ("@_!_#_$_|_@"); if (tmp[0]==0) {dataarray[y]=tmp[0]+ "@_!_#_$_|" _@ "+tmp[1]+" @_!_#_$_|_@ "+" "+tmp[2];loadnexttype (Tmp[1]); y+=1;} --></script><span style= "border:1px solid #000000; Position:absolute; Overflow:hidden; "> <select name=" fenlei "style=" MARGIN:-2PX; " > <script language= "JavaScript" ><!--for (var key in DataArray) {var tp=dataarray[key].split ("@_!_#_$_|_@") ;d ocument.write (' <option value= ' +tp[1]+ ' > ' +tp[2]+ ' </option> ');} --></script></select></span> </body></ptml>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

Explain:

ASP query database, generate JS data object (not array, similar)
Database of different rows, corresponding to different temparray[0] temparray[1] ...
Different fields are segmented by "@_!_#_$_|_@" in each piece of data to avoid duplication with the text in the data.

Using JS to rearrange temparray by recursion algorithm to get dataarray and to cycle Print to option in select



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.