多条件查询及可设置条件

<html>
<body>
<tableborder='0'height='400'cellspacing='0'cellpadding='0'>
<trheight=15align=center>
<tdbgcolor="#C0C0E0">
查询条件设置<astyle="cursor:hand;color:blue;"title="清空所有条件"onclick="SearchContent.queryer.deleteallfield()">□</a><astyle="cursor:hand;color:blue;"title="清空所有条件"onclick="SearchContent.innerHTML='';SearchContent.queryer.addallfield()">■</a><astyle="cursor:hand;color:blue;"title="字段值"onclick="SearchContent.queryer.setlevel(1)">1</a><astyle="cursor:hand;color:blue;"title="字段条件值"onclick="SearchContent.queryer.setlevel(2)">2</a><astyle="cursor:hand;color:blue;"title="与或字段条件值"onclick="SearchContent.queryer.setlevel(3)">3</a><astyle="cursor:hand;color:blue;"title="与或左括号字段条件值右括号"onclick="SearchContent.queryer.setlevel(4)">4</a>
</td>
</tr>
<tr>
<tdwidth=300>
<divid="SearchContent"style="overflow-y:scroll;width:100%;height:100%;white-space:nowrap;z-index:1;float:left;border-style:solid;border-width:0px"></div>
</td>
</tr>
</table>

<inputtype="button"value="测试"name="B3"onclick="alert('★'+SearchContent.queryer.getvalue()+'★');">

</body>

</html>

<script>
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//queryerstart
/*
作者:山西太原的邢志云(耗了我2天时间呀)
引用时请保留此注释
*/
functionqueryer()
{
this.fields;//字段串
this.fieldstype;//字段类型串
this.container;//创建容器
this.level=2;//复杂度设置1:字段+值2:条件3:与或4:括号

this.maxrowno=-1;//表明当前条件设置最大的编号,只起个序号的作用

this.optionsfield;//只读,存储字段下拉框的内容

this.create=function(fields,fieldstype,container)
{
varcols;
varcolstype;
varhtmls=newArray();
vars;
vari;

if(container==null&&this.container==null)throw("queryer.create:必须设置在哪个容器上创建");
if(container==null)container=this.container;
if(this.container==null)this.container=container;

if(fieldstype==null&&this.fieldstype==null)throw("queryer.create:必须设置字段类型串");
if(fieldstype==null)fieldstype=this.fieldstype;
if(this.fieldstype==null)this.fieldstype=fieldstype;

if(fields==null&&this.fields==null)throw("queryer.create:必须设置字段串");
if(fields==null)fields=this.fields;
if(this.fields==null)this.fields=fields;

cols=fields.split(",");
colstype=fieldstype.split(",");
htmls.push("<optionvalue=''></option>");
for(i=0;i<cols.length;i++)
{
htmls.push("<optionvalue='"+cols[i]+"'ftype='"+colstype[i]+"'>"+cols[i]+"</option>");
}

this.optionsfield=htmls.join("");

htmls.splice(0,100000000);
htmls=null;
//把已有的字段增加上
//this.addallfield();
//把自己做为对象附加到容器上,将来可以通过容器访问查询对象
container.queryer=this;
}
//追加所有的字段
//可以加参数,表示不加载这些字段的设置框,但下拉框中仍然有
this.addallfield=function()
{
varcols=this.fields.split(",");
vari;
vara;
varhave;
for(i=0;i<cols.length;i++)
{
have=true;
for(a=0;a<arguments.length;a++)
{
if(cols[i]==arguments[a])
{
have=false;
break;
}
}
if(have)this.addsetter(cols[i]);
}
//最后再加一个空的
this.addsetter();
}
//删除所有的字段
this.deleteallfield=function()
{
vari;
this.container.innerHTML="";
this.maxrowno=-1;
//只增加一个空的
this.addsetter();
}
//增加一个条件设置框
//fieldname如果!=null则字段中显示这个名称
this.addsetter=function(fieldname,condition,setvalue)
{
varhtmls=newArray();
varno=++this.maxrowno;
varnodediv;
vardisplay;
nodediv=document.createElement("<divid=fieldno_"+no+"rownum="+no+"name=queryitem>");
//与或
if(this.level>=3)
display="inline";
else
display="none";
htmls.push("<selectsize='1'id=andorstyle='display:"+display+";'><optionvalue=''></option><optionvalue='and'>并且</option><optionvalue='or'>或</option></select>");
//左括号
if(this.level>=4)
display="inline";
else
display="none";
htmls.push("<selectsize='1'id=lbracketstyle='display:"+display+";'><optionvalue=''></option><optionvalue='('>(</option></select>");
//字段
htmls.push("<selectsize='1'id=fieldnameonchange=\""+this.container.id+".queryer.addjudge("+no+")\">"+this.optionsfield+"</select>");
//条件
if(this.level>=2)
display="inline";
else
display="none";
htmls.push("<selectsize='1'id=conditionstyle='display:"+display+";'><optionvalue=''></option><optionvalue='='>=</option><optionvalue='&gt;'>></option><optionvalue='&lt;'><</option><optionvalue='&gt;='>>=</option><optionvalue='&lt;='><=</option><optionvalue='&lt;&gt;'><></option><optionvalue='like'>包含</option></select>");
//值
htmls.push("<inputtype='text'id=setvaluesize='15'>");
//值选择
//htmls.push("<imgsrc='images/button/dropdown.jpg'style='cursor:hand;border-right:1pxsolid#7F9DB9;border-bottom:1pxsolid#7F9DB9'>");
//右括号
if(this.level>=4)
display="inline";
else
display="none";
htmls.push("<selectsize='1'id=rbracketstyle='display:"+display+";'><optionvalue=''></option><optionvalue=')'>)</option></select>");
//删除
htmls.push("<inputtype='button'value='×'id='closequery'onclick=\""+this.container.id+".queryer.deletesetter("+no+")\"style='cursor:hand;width=18;height=18;'>");

nodediv.innerHTML=htmls.join("");
this.container.appendChild(nodediv);
if(fieldname!=null)
{
obj=eval(this.container.id+".all.fieldno_"+no);
obj.all.fieldname.value=fieldname;
}
if(condition!=null)
{
obj=eval(this.container.id+".all.fieldno_"+no);
obj.all.condition.value=condition;
}
if(setvalue!=null)
{
obj=eval(this.container.id+".all.fieldno_"+no);
obj.all.setvalue.value=setvalue;
}
htmls.splice(0,100000000);
htmls=null;
}
//获取设置值
this.getvalue=function(excludfields)
{
varobjfield=this.container.children;
vari;
varhtmls=newArray();
varandor,lbracket,fieldname,condition,setvalue,rbracket;
varfieldtype;
vars;
varlrquotation;
for(i=0;i<objfield.length;i++)
{
fieldname=objfield[i].all.fieldname.value;
if(fieldname=="")continue;
condition=objfield[i].all.condition.value;
setvalue=objfield[i].all.setvalue.value;
if(condition==""&&setvalue=="")continue;

fieldtype=objfield[i].all.fieldname.options[objfield[i].all.fieldname.options.selectedIndex].ftype;
andor=objfield[i].all.andor.value;
lbracket=objfield[i].all.lbracket.value;
rbracket=objfield[i].all.rbracket.value;

//htmls.push(fieldname+"("+fieldtype+")"+condition+setvalue);
if(fieldtype=="N")
{
lrquotation="";
if(condition=="")condition="=";
if(condition=="like")
{
s="只有字符才能使用'包含',而["+fieldname+"]是数字,所以不能使用包含条件,请调整查询设置";
alert(s);
throw(s);
}
}
else
{
if(condition=="")condition="like";
lrquotation="'";
setvalue=setvalue.replace(/'/g,"''");
if(condition=="like")
{
setvalue=setvalue.replace(/\[/g,"\[\[]");
if(setvalue.indexOf("%")==-1)setvalue="%"+setvalue+"%";
}
}
if(condition=="like"&&setvalue=="%%")
{
condition="=";
setvalue="";
}
if(andor=="")andor="and";
if(andor=="and")andor="and";
if(andor=="or")andor="or";//长度和and时是一样的,这样有利于截取
if(condition=="like")condition="like";
//对null的处理
if(fieldtype=="N")
{

if(setvalue=="")
if(condition=="<>")
s=fieldname+"isnotnull";
elseif(condition=="=")
s=fieldname+"isnull";
else
s=fieldname+condition+"null";
else
s=fieldname+condition+lrquotation+setvalue+lrquotation;
}
else
{
if(setvalue=="")
{
if(condition=="<>")
s="("+fieldname+"isnotnulland"+fieldname+"<>'')";
elseif(condition=="=")
s="("+fieldname+"isnullor"+fieldname+"='')";
else
s=fieldname+condition+"''";
}
else
s=fieldname+condition+lrquotation+setvalue+lrquotation;
}

htmls.push(andor+lbracket+s+rbracket);
}
s=htmls.join("");
htmls.splice(0,100000000);
htmls=null;
if(s.length>0)s=s.substring(5);
//alert("*"+s+"*");
returns;
}
//设置复杂度
this.setlevel=function(level)
{
this.level=level;
vari;
varobjfield=this.container.children;
for(i=0;i<objfield.length;i++)
{
if(level>=4)
{
objfield[i].all.lbracket.style.display="inline";
objfield[i].all.rbracket.style.display="inline";;
}
else
{
objfield[i].all.lbracket.style.display="none";
objfield[i].all.rbracket.style.display="none";
objfield[i].all.lbracket.value="";
objfield[i].all.rbracket.value="";
}

if(level>=3)
objfield[i].all.andor.style.display="inline";
else
{
objfield[i].all.andor.style.display="none";
objfield[i].all.andor.value="";
}
if(level>=2)
objfield[i].all.condition.style.display="inline";
else
{
objfield[i].all.condition.style.display="none";
objfield[i].all.condition.value="";
}
}

}
//删除某个条件
this.deletesetter=function(i)
{
if(this.maxrowno==i)return;//最后一个不能删除
vars=this.container.id+".all.fieldno_"+i;
varobj=eval(s);
this.container.removeChild(obj);
}
//点击最后一条时自动再追加一个空的
this.addjudge=function(i)
{
if(this.maxrowno==i)this.addsetter();
}
}

//queryerend
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

varqe=newqueryer();
qe.level=3;
qe.create("栏目,类别,头条,顺序号,标题,创建日期,创建人,最后修改日期,最后修改人,点击率,部门,公司","C,C,C,N,C,C,C,C,C,N,C,C",SearchContent);
qe.addsetter("公司","=","泰森");
qe.addsetter("点击率",">","100");
qe.addsetter("点击率","<=","200");
qe.addallfield("公司","点击率");
</script>

你可能感兴趣的:(C++,c,C#)