---
title: "OpenJweb平台中自定义组合查询条件窗口的实现方式(经典之作)"
author: "cjc"
date: "2008-04-11T20:20:00Z"
tags: []
slug: "openjweb"
draft: false
lastmod: "2008-04-11T20:20:00Z"
id: 446891
url: "https://cjc.iteye.com/blog/446891"
---

首先,在父页面中写一个javascript脚本,用来接收组合条件设置窗口的拼装的SQL语句,查询条件子窗口关闭后,javascript将查询条件提交到后台控制层,父页面的javascript:

function doCustQuery()  
{  
//  
var returnvalue =window.showModalDialog( "&lt;%=request.getContextPath()%&gt;/module/platform/compositeQuery.jsp?entityName=${entityClassName}", '组合条件查询', "dialogWidth =800px;dialogHeight = 400px;help=0 ");  
// alert(returnvalue+"!!!")  
document.listForm.customHql.value=returnvalue;

document.listForm.submit();

}

下面是动态查询条件设置窗口的界面:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/baozhengw/000.jpg)

说明:最右侧选择并且或或者可以增加新行,查询条件列是当前表的查询条件列,当选择了审批状态,由于审批状态

是数据字典,则计划发布对应的那个iframe自动显示审批状态的取值,对于信息标题,由于没有数据字典,则iframe显示的是输入框,另外每行有一对单引号,表明iframe的值是一个字符串,如果条件列为数字,则去掉一对引号,算术比较符下拉框包含了=,&lt;&gt; ,in ,not in ,is null,is not null,like,&gt;,&lt;等逻辑关系,相信大家看到这个动态查询条件页面后肯定会对大家有莫大的帮助,也许这正是你在网上苦苦寻找的答案,此页面的JSP代码如下:

&lt;%@ page contentType="text/html; charset=GBK"%&gt;  
&lt;%@ taglib prefix="ww" uri="webwork" %&gt;  
&lt;%@ include file="/common/head.jsp"%&gt;  
&lt;%  
String entityName =request.getParameter("entityName");

%&gt;  
&lt;script language="JavaScript"&gt;

&lt;!--  
function changeList( len) {  
var colname = document.queryForm.elements\['conditionCol1'+len].value  
var classname = "&lt;%=entityName%&gt;"  
var htmlFieldName = "queryValueCol1"  
var obj = document.getElementById("selectList"+len);  
var url = "&lt;%=request.getContextPath()%&gt;/module/platform/dictlist.jsp?

classname="+classname+"&colname="+colname+"&htmlFieldName="+htmlFieldName  
window.frames\['selectList'+len].document.location.href= url

}

function addQueryTable()  
{  
var row1 = queryTable.insertRow(); //插入一行

var cell1=row1.insertCell();  
var cell2=row1.insertCell();  
var cell3=row1.insertCell();  
var cell4=row1.insertCell();  
var cell5=row1.insertCell();  
var cell6=row1.insertCell();  
var cell7=row1.insertCell();  
var len = window.queryTable.rows.length  
var logicConn ="&lt;select name=col7"+len+" onChange='addQueryTable()' &gt;&lt;option value='' selected=selected&gt;"+  
"连接符...&lt;/option&gt;&lt;option value=' AND '&gt;并且&lt;/option&gt;&lt;option value=' OR '&gt;或者&lt;/option&gt;&lt;/select&gt;"  
var logicComp = "&lt;select name=col6"+len+" &gt;&lt;option value=' = ' selected=selected&gt;等于&lt;/option&gt;"+  
"&lt;option value=' &gt; '&gt;大于&lt;/option&gt;&lt;option value=' &lt; '&gt;小于&lt;/option&gt;&lt;option value=' = '&gt;"+  
"等于&lt;/option&gt;&lt;option value=' &lt;&gt; '&gt;不等于&lt;/option&gt;&lt;option value=' like '&gt;匹配&lt;/option&gt;"+  
"&lt;option value=' is null '&gt;为空&lt;/option&gt;&lt;option value=' is not null '&gt;非空&lt;/option&gt;&lt;option "+  
"value=' in '&gt;包含&lt;/option&gt;&lt;option value=' not in '&gt;不包含&lt;/option&gt;&lt;/select&gt;"  
cell1.innerHTML = "&lt;td align='left' width='8%'&gt;&lt;input type='text' name='col1"+len+"' size=11 value='('&gt;&lt;/td&gt;"; //左括号  
var tmpSelect='&lt;%=JspBuffer.getQueryHtmlSelect(entityName,"conditionCol1")%&gt;'  
tmpSelect=tmpSelect.replace("conditionCol1","conditionCol1"+len)  
tmpSelect=tmpSelect.replace("changeList()","changeList("+len+")")

cell2.innerHTML = "&lt;td align='left' width='20%'&gt;" + tmpSelect +"&lt;/td&gt;" ;  
cell3.innerHTML = "&lt;td align='left' width='10%'&gt;"+logicComp+"&lt;/td&gt;"; //逻辑运算符  
cell4.innerHTML = "&lt;td align='right' width='12%'&gt;&lt;input type='text' name='col4"+len+"' size=12 value=\\""+"'"+"\\"

&gt;&lt;/td&gt;"; //括号  
cell5.innerHTML = "&lt;td class='add\_content\_' width='40%'&gt;&lt;div style='HEIGHT: 30px;overflow=auto;' &gt;"+  
"&lt;iframe id='selectList"+len+"' src='&lt;%=request.getContextPath()%&gt;/module/platform/dictlist.jsp?className="+

"&lt;%=entityName%&gt;&colname=&htmlFieldName=queryValueCol1' name='selectList"+len+"'"+  
" frameborder='0' width='100%' height='100%' scrolling='no' MARGINHEIGHT=5&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;/td&gt;" //查询条件值

cell6.innerHTML = "&lt;td align='left' width='8%'&gt; &lt;input type='text' name='col1"+len+"' size=8 value=\\""+"')"+"\\"&gt;&lt;/td&gt;";

// 括号  
cell7.innerHTML = "&lt;td align='left' width='8%'&gt;"+logicConn+"&lt;/td&gt;"; //逻辑连接符ect&gt;"

}

//获得设置的查询条件  
function getCondition( )  
{  
//alert(window.tb1.rows.length);表格的长度  
// alert(document.test.a1. value)  
// alert(document.test.conditionCol1 . value)  
//获得标签的名字 循环a\[i].value 肯定能行  
// alert( document.test.getElementsByTagName('input')\[1].value)  
// alert( document.test.getElementsByTagName('select')\[0].value)  
var len = window.queryTable.rows.length  
//alert(window.queryTable.getElementsByName('col1').value)  
//alert(window.queryTable.getElementsByTagName('input')\[0].value)  
var queryString ="";  
for(i=0;i&lt;len;i++)  
{

var col1 = window.queryTable.getElementsByTagName('input')\[i\*3+0].value //左括号  
if(col1==null||col1=='null')col1=""

var col2 = window.queryTable.getElementsByTagName('select')\[i\*3+0].value //获得查询字段名  
if(col2==null||col2=='null')col2=""

var col3 = window.queryTable.getElementsByTagName('select')\[i\*3+1].value //获得逻辑比较符  
if(col3==null||col3=='null')col3=""  
var col4 = window.queryTable.getElementsByTagName('input')\[i\*3+1].value //括号  
if(col14=null||col4=='null')col4=""  
var col5 = window.frames\['selectList'+(i+1)].document.dict.queryValueCol1.value //获得iframe中的值  
if(col5==null||col5=='null')col5=""

//col5="\\'"+col5+"\\'"  
// alert(col5)  
var col6 = window.queryTable.getElementsByTagName('input')\[i\*3+2].value //括号  
if(col6==null||col6=='null')col6=""  
var col7 = window.queryTable.getElementsByTagName('select')\[i\*3+2].value //获得逻辑连接符  
if(col3==' like ')col5='%'+col5+'%' //如果是模糊查询，自动增加%  
if(col3==' is null '||col3==' is not null ')col5=""; //如果设置了is null和is not null表达式，则后面不需要条件值  
if(col3==' is null '||col3==' is not null ')col4="";  
if(col3==' is null '||col3==' is not null ')col6=")";  
if(col7==null||col7=='null')col7=""  
//alert(col5)  
if(col2==null||col2==""||col2=='null')  
{  
//如果col2为空，不再计算条件表达式

break;  
}

queryString += col1+col2+col3+col4+col5+col6+col7

}  
// alert(queryString)  
window.returnValue =queryString;  
window.close();

}  
\--&gt;  
&lt;/script&gt;

&lt;table width="96%" align="center" class="all"&gt;  
&lt;tr class="allcontent"&gt;  
&lt;td class="allcontent"&gt;

&lt;ww:form name="queryForm" action = "listPlan"&gt;  
&lt;fieldset&gt;  
&lt;ww:hidden name="operate"/&gt;  
&lt;ww:hidden name="serviceMethod" /&gt;  
&lt;ww:hidden name="queryValueCol1" /&gt;

&lt;ww:hidden name="userActionId"/&gt;  
&lt;ww:hidden name="actionFlag"/&gt;  
&lt;legend&gt;&lt;/legend&gt;  
&lt;table width="100%" border="0" align="center" cellpadding="2" cellspacing="0" class="query"&gt;  
&lt;tr class="query\_content\_"&gt;

&lt;td colspan="4" class="query\_content\_" align="right"&gt;  
&lt;!--&lt;input type="button" value="新增" onClick="addQueryTable()" class="mybutton"&gt;--&gt;

&lt;input type="button" value="执行查询" onClick="getCondition()" class="mybutton"&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;/fieldset&gt;  
&lt;fieldset&gt;  
&lt;legend&gt;查询条件设置&lt;/legend&gt;  
&lt;table width="100%" class="result" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"&gt;  
&lt;tr id="docktr" align="center" class="result\_title"&gt;  
&lt;td nowrap class="result\_head\_dock" width="10%"&gt;括号&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="20%" &gt;查询条件列&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="10%"&gt;逻辑运算符&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="10%"&gt;括号&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="30%"&gt;查询条件值&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="10%"&gt;括号&lt;/td&gt;  
&lt;td nowrap class="result\_head\_dock" width="10%"&gt;逻辑连接符&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;div id="FixHeadContainer" style="HEIGHT: 295px;overflow=auto;"&gt;  
&lt;table width="100%"&gt;&lt;tr&gt;&lt;td&gt;  
&lt;table width="100%" class="result" id="queryTable" border="1" align="center"

cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"&gt;

&lt;/table&gt;  
&lt;script Language="Javascript1.2"&gt;  
for (i=0;i&lt;queryTable.rows.length;i++) {  
(i%2==0)?(table1.rows(i).className = "result\_content"):(table1.rows(i).className = "result\_content\_");  
}  
document.title="组合查询条件设置";  
addQueryTable() ;

&lt;/script&gt;  
&lt;/td&gt;&lt;/tr&gt;

&lt;/table&gt;  
&lt;/div&gt;  
&lt;table width="100%" class="resbutton"&gt;  
&lt;tr class="resbutton"&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;/fieldset&gt;  
&lt;/ww:form&gt;  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;[%@include](mailto:%@include) file="/common/tail.jsp"%&gt;