当前位置: 代码迷 >> Web前端 >> 不久前刚做的一个通用组合检索弹出窗口
  详细解决方案

不久前刚做的一个通用组合检索弹出窗口

热度:230   发布时间:2012-11-04 10:42:42.0
最近刚做的一个通用组合检索弹出窗口


?


?

dd

?



?

?

代码写的有点糟.

?

一天写的.还没有好好重构. 不过大致功能经过测试基本没啥问题.

?

应大家要求代码贴出来参考下

<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initHistorySearchList()" close="closeWindow()" width="584" height="374" xmlns:code="http://code.google.com/p/flexlib/" xmlns:editor="com.thams.grid.editor.*">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import com.adobe.cairngorm.control.CairngormEventDispatcher;
			import com.adobe.utils.ArrayUtil;
			import com.adobe.utils.StringUtil;
			import com.thams.common.constant.MenuImageClass;
			import com.thams.grid.event.AddSearchHistoryEvent;
			import com.thams.grid.event.ComboBoxEditorEvent;
			import com.thams.grid.event.DataBaseInfoEvent;
			import com.thams.grid.event.SearchHistoryListEvent;
			import com.thams.grid.model.DataBaseInfoModellocator;
			import com.thams.grid.model.SearchHistoryListModellocator;
			import com.thams.grid.vo.CombinedVo;
			import com.thams.grid.vo.TblcodeTable;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.managers.PopUpManager;
			
			import spark.events.IndexChangeEvent;
			
			private var model:DataBaseInfoModellocator = DataBaseInfoModellocator.getInstance();
			
			[Bindable]
			public var dtableGridObject:DtableGrid;
			
			[Bindable]
			private var searchListValues:ArrayCollection = new ArrayCollection();
			
			
			private var searchModel:SearchHistoryListModellocator = SearchHistoryListModellocator.getInstance();
			
			
			[Bindable]
			private var strSelection:ArrayCollection = new ArrayCollection(
					[{label:"等于",value:"="},  
					{label:"不等于",value:"<>"},  
					{label:"大于",value:">"},  
					{label:"小于",value:"<"},  
					{label:"大于等于",value:">="},  
					{label:"小于等于",value:"<="},  
					{label:"包含",value:"like"},  
					{label:"不包含",value:"not like"},  
					{label:"为空",value:"is null"},  
					{label:"不为空",value:"is not null"}]
			);
			[Bindable]
			private var numSelection:ArrayCollection = new ArrayCollection(
					[{label:"等于",value:"="},  
					{label:"不等于",value:"<>"},  
					{label:"大于",value:">"},  
					{label:"小于",value:"<"},  
					{label:"大于等于",value:">="},  
					{label:"小于等于",value:"<="},  
					{label:"为空",value:"is null"},  
					{label:"不为空",value:"is not null"}]
			);
			[Bindable]
			private var dateSelection:ArrayCollection = new ArrayCollection(
					[{label:"迟于",value:">="},  
					{label:"早于",value:"<="},  
					{label:"为空",value:"is null"},  
					{label:"不为空",value:"is not null"}]
			);

			protected function closeWindow():void
			{
				PopUpManager.removePopUp(this);
			}


			protected function field_changeHandler(event:IndexChangeEvent):void
			{
				var dropSelObject:Object = (event.currentTarget as DropDownList).selectedItem;
				if(dropSelObject.fieldtype==1){//字符
					tiaojian.dataProvider = strSelection;
					yunsuanfu.visible = true;
					yunsuanfuDate.visible = false;
					yunsuanfuNum.visible = false;
					yunsuanfuTbl.visible = false;
				}
				if(dropSelObject.fieldtype==3){//整形
					tiaojian.dataProvider = numSelection;
					yunsuanfuNum.visible = true;
					yunsuanfu.visible = false;
					yunsuanfuDate.visible = false;
					yunsuanfuTbl.visible = false;
				}
				if(dropSelObject.fieldtype==6){//浮点型
					tiaojian.dataProvider = numSelection;
					yunsuanfu.visible = true;
					yunsuanfuDate.visible = false;
					yunsuanfuNum.visible = false;
					yunsuanfuTbl.visible = false;
				}
				if(dropSelObject.fieldtype==11){//日期
					tiaojian.dataProvider = dateSelection;
					yunsuanfuDate.visible = true;
					yunsuanfu.visible = false;
					yunsuanfuNum.visible = false;
					yunsuanfuTbl.visible = false;
					CairngormEventDispatcher.getInstance().dispatchEvent(new DataBaseInfoEvent());
				}
				if(dropSelObject.stblcode!=null){
					yunsuanfuTbl.visible = true;
					yunsuanfu.visible = false;
					yunsuanfuDate.visible = false;
					yunsuanfuNum.visible = false;
					var tblcodetable:TblcodeTable = new TblcodeTable(); 
					tblcodetable.descode = dropSelObject.stblcode.descode;
					tblcodetable.desshow = dropSelObject.stblcode.desshow;
					tblcodetable.codetbl = dropSelObject.stblcode.codetbl;
					CairngormEventDispatcher.getInstance().dispatchEvent(new ComboBoxEditorEvent(tblcodetable));
				}
			}


			protected function add_clickHandler(event:MouseEvent):void
			{
				if(field.selectedIndex==-1||tiaojian.selectedIndex==-1){
					return;
				}
				var comb:CombinedVo = getInputValue();
				if(!comb){
					return;
				}
				this.searchListValues.addItem(comb);
			}
			protected function edit_clickHandler(event:MouseEvent):void
			{
				if(searchList.selectedIndex==-1){
					return;
				}
				if(field.selectedIndex==-1||tiaojian.selectedIndex==-1){
					return;
				}
				var comb:CombinedVo = getInputValue();
				if(!comb){
					return;
				}
				this.searchListValues.setItemAt(comb,searchList.selectedIndex);
			}
//			/**
//			 * @param inputText 用户输入的值
//			 */
//			private function getCombinedVo(inputText:String):CombinedVo{
//				var comb:CombinedVo = new CombinedVo();
//				comb.value = field.selectedItem.chname;
//				comb.sql = field.selectedItem.fieldname;
//				if(tiaojian.selectedItem.label=="为空"||tiaojian.selectedItem.label=="不为空"){
//					comb.value += " "+tiaojian.selectedItem.label;
//					comb.sql += " "+tiaojian.selectedItem.value;
//				}else{
//					if(!inputText){
//						comb = null;
//					}else if(tiaojian.selectedItem.label=="包含"||tiaojian.selectedItem.label=="不包含"){
//						comb.value += " "+tiaojian.selectedItem.label;
//						comb.value += " "+inputText;
//						comb.sql += " "+tiaojian.selectedItem.value;
//						comb.sql += " '%"+inputText+"%'";
//					}else{  // ....明天搞别的
//						comb.value += " "+tiaojian.selectedItem.label;
//						comb.value += " "+inputText;
//						comb.sql += " "+tiaojian.selectedItem.value;
//						comb.sql += " '"+inputText+"'";
//					}
//				}
//				return comb;
//			}

			
			private function getInputValue():CombinedVo{
				if(field.selectedIndex==-1||tiaojian.selectedIndex==-1){
					return null;
				}
				var _inputText:String;
				var comb:CombinedVo = new CombinedVo();
				comb.value = field.selectedItem.chname;
				comb.sql = field.selectedItem.fieldname;
				if(yunsuanfu.visible){ //当前为浮点型和字符型输入
					_inputText = "'"+yunsuanfu.text+"'";
				}
				if(yunsuanfuNum.visible&&yunsuanfuNum.value){ //当前为数字型输入
					_inputText = yunsuanfuNum.value.toString();
				}
				if(yunsuanfuDate.visible&&yunsuanfuDate.selectedDate){//当前为日期型输入
					_inputText = df.format(yunsuanfuDate.selectedDate);
				}
				if(yunsuanfuTbl.visible&&yunsuanfuTbl.selectedItem){//当前为代码表
					_inputText = "'"+yunsuanfuTbl.selectedItem.data+"'";
				}
				if(tiaojian.selectedItem.label=="为空"||tiaojian.selectedItem.label=="不为空"){
					comb.value += " "+tiaojian.selectedItem.label;
					comb.sql += " "+tiaojian.selectedItem.value;
				}else{
					if(!_inputText||_inputText=="''"){
						return null;
					}
					if((tiaojian.selectedItem.label=="包含"||tiaojian.selectedItem.label=="不包含")){
						_inputText = StringUtil.replace(_inputText,"'","");
						comb.value += " "+tiaojian.selectedItem.label;
						comb.value += " '%"+_inputText+"%'";
						comb.sql += " "+tiaojian.selectedItem.value;
						comb.sql += " '%"+_inputText+"%'";
					}else{
						comb.value += " "+tiaojian.selectedItem.label;
						comb.value += " "+_inputText;
						comb.sql += " "+tiaojian.selectedItem.value;
						if(yunsuanfuDate.visible&&yunsuanfuDate.selectedDate){//当前为日期型输入 再次判断. 上面保留
							if(model.isOracle){ //如果是oracle数据库
								_inputText = "TO_DATE('"+_inputText+"', 'yyyy-MM-dd')";
							}else{
								_inputText = "cast('" + _inputText + "' as datetime)";
							}
						}
						comb.sql += " "+_inputText;
					}
				}
				return comb;
			}


			protected function del_clickHandler(event:MouseEvent):void
			{
				if(searchList.selectedIndex==-1){
					return;
				}
				searchListValues.removeItemAt(searchList.selectedIndex);
			}




			protected function button1_clickHandler(event:MouseEvent):void
			{
				var comb:CombinedVo = new CombinedVo();
				comb.value = (event.currentTarget as Button).label;
				switch((event.currentTarget as Button).label){
					case "(":
						comb.sql = "(";
						break;
					case ")":
						comb.sql = ")";
						break;
					case "并且":
						comb.sql = "and";
						break;
					case "或者":
						comb.sql = "or";
						break;
					case "非":
						comb.sql = "not";
						break;
				}
				searchListValues.addItem(comb);
			}


			protected function search(event:MouseEvent):void
			{
				var whereSQL:String = "";
				var arrayShow:String = "";
				var arraySql:String = "";
				var searchArea:uint = extendsSearch.selected?1:0; //默认为继承条件内检索
				for each(var comb:CombinedVo in searchListValues){
					trace(comb.sql);
					whereSQL += " "+comb.sql;
					arrayShow += "‖"+comb.value;
					arraySql += "‖"+comb.sql;
				}
				if(!whereSQL&&!((event.currentTarget as Button).label=="列所有")){
					Alert.show("请输入条件","出错了");
					return;
				}
				if(whereSQL){
					CairngormEventDispatcher.getInstance().dispatchEvent(new AddSearchHistoryEvent(
						dtableGridObject.dataLoaderClass.libcode,
						dtableGridObject.dataLoaderClass.level,
						replaceSpecialcharacters(whereSQL),
						replaceSpecialcharacters(arrayShow),
						replaceSpecialcharacters(arraySql),
						searchArea
					));
				}
				if(allSearch.selected){
					dtableGridObject.dataLoaderClass.pid = -2;
					dtableGridObject.dataLoaderClass.qxwhere = "";
				}
				if(dtableGridObject.dataLoaderClass.qxwhere){
					dtableGridObject.dataLoaderClass.qxwhere += " and ("+whereSQL+")";
				}else{
					dtableGridObject.dataLoaderClass.qxwhere = "("+whereSQL+")";
					
				}
				if((event.currentTarget as Button).label=="列所有"){
					dtableGridObject.dataLoaderClass.pid = -2;
					dtableGridObject.dataLoaderClass.qxwhere = "";
				}
				dtableGridObject.dataLoaderClass.pageNumber=1;
				dtableGridObject.dataLoaderClass.pageSize= 50;
				dtableGridObject.loadDataGrid();
				closeWindow();
			}
			/**
			 * 默认正序.替换
			 * desc = true 则为倒序替换
			 */
			private function replaceSpecialcharacters(str:String,desc:Boolean = false):String{
				if(!desc){
					str = StringUtil.replace(str,"[","&po;");
					str = StringUtil.replace(str,"]","&qo;");
					str = StringUtil.replace(str,"(","&mn;");
					str = StringUtil.replace(str,")","&nm;");
					str = StringUtil.replace(str,"_","&bb;");
					str = StringUtil.replace(str,"'","&lf;");
					str = StringUtil.replace(str,"\"","&ks;");
				}else{
					str = StringUtil.replace(str,"&po;","[");
					str = StringUtil.replace(str,"&qo;","]");
					str = StringUtil.replace(str,"&mn;","(");
					str = StringUtil.replace(str,"&nm;",")");
					str = StringUtil.replace(str,"&bb;","_");
					str = StringUtil.replace(str,"&lf;","'");
					str = StringUtil.replace(str,"&ks;","\"");
				}
				return str;
			}


			protected function initHistorySearchList():void
			{
//				Alert.show("ffff");
				CairngormEventDispatcher.getInstance().dispatchEvent(new SearchHistoryListEvent(dtableGridObject.dataLoaderClass.libcode,dtableGridObject.dataLoaderClass.level));
			}


			private var searchnum:int = -1;
			protected function changeSearchKey(event:MouseEvent):void
			{
				
				if(!searchModel||!searchModel.searchHistoryList||searchModel.searchHistoryList.length<=0){
					return;
				}
				switch((event.currentTarget as Button).label){
					case "首个查询":
						searchnum = 0;
						break;
					case "上一个":
						if(searchnum<0){
							searchnum = searchModel.searchHistoryList.length-1;
						}else if(searchnum>0){
							searchnum = searchnum - 1;
						}else if(searchnum==0){
							return;
						}
						break;
					case "下一个":
						if(searchnum<0||searchnum==(searchModel.searchHistoryList.length-1)){
							return;
						}else{
							searchnum = searchnum + 1;
						}
						break;
					case "最后查询":
						searchnum = searchModel.searchHistoryList.length-1;
						break;
				}
				searchListValues.removeAll();
				var s:Object = searchModel.searchHistoryList[searchnum];
				if((s.searcharea as int) == 0){
					allSearch.selected = true;
				}else{
					extendsSearch.selected = true;
				}
				var showArray:Array = replaceSpecialcharacters(String(s.arrayshow),true).split("‖");
				var sqlArray:Array = replaceSpecialcharacters(String(s.arraysql),true).split("‖");
				for(var i:int=0;i<sqlArray.length;i++){
					if(showArray[i]&&sqlArray[i]&&sqlArray[i]!="\""&&showArray[i]!="\""){
						var comb:CombinedVo = new CombinedVo();
						comb.value = showArray[i];
						comb.sql = sqlArray[i];
						searchListValues.addItem(comb);
					}
				}
				
				
			}


			protected function shangyi(event:MouseEvent):void
			{
				var selIndex:int = searchList.selectedIndex;
				if(selIndex <=0){
					return;
				}
				var _sel:Object = searchListValues.getItemAt(selIndex);
				var _bak:Object = searchListValues.getItemAt(selIndex-1);
				searchListValues.setItemAt(_bak,selIndex);
				searchListValues.setItemAt(_sel,selIndex-1);
				searchList.selectedIndex = selIndex -1;
			}
			protected function xiayi(event:MouseEvent):void
			{
				var selIndex:int = searchList.selectedIndex;
				if(selIndex!=-1&&selIndex<searchListValues.length-1){
					var _sel:Object = searchListValues.getItemAt(selIndex);
					var _nex:Object = searchListValues.getItemAt(selIndex+1);
					searchListValues.setItemAt(_nex,selIndex);
					searchListValues.setItemAt(_sel,selIndex+1);
					searchList.selectedIndex = selIndex +1;
				}
				return;
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<mx:DateFormatter id="df" formatString="YYYY-MM-DD"/>
		<s:RadioButtonGroup id="group3"/>
	</fx:Declarations>
	<s:controlBarContent>
		<s:HGroup>
			<mx:Button label="首个查询" toolTip="首个查询" icon="{MenuImageClass.shougechaxun}" click="changeSearchKey(event)"/> 
			<mx:Button label="上一个" toolTip="上一个查询" icon="{MenuImageClass.shangyige}" click="changeSearchKey(event)"/> 
			<mx:Button label="下一个" toolTip="下一个查询" icon="{MenuImageClass.xiagechaxun}" click="changeSearchKey(event)"/> 
			<mx:Button label="最后查询" toolTip="最后一个查询" icon="{MenuImageClass.house_go}" click="changeSearchKey(event)"/> 
			<mx:Button label="查询" toolTip="执行查询" icon="{MenuImageClass.jiansuo}" click="search(event)"/> 
			<mx:Button label="列所有" toolTip="列出所有" icon="{MenuImageClass.liechusuoyou}" click="search(event)"/> 
			<mx:Button label="退出" toolTip="退出" icon="{MenuImageClass.tuichu}"  click="closeWindow()"/> 
		</s:HGroup>
	</s:controlBarContent>
		
		<s:Group  width="100%" height="100%">
			<s:Label x="97" y="10" text="字段名"/>
			<s:Label x="392" y="9" text="运算符"/>
			<s:Label x="256" y="10" text="条件值"/>
			
			<s:DropDownList id="field" dataProvider="{ArrayCollection(dtableGridObject.columnHeaderList)}" labelField="chname" change="field_changeHandler(event)" x="62" y="27"></s:DropDownList>
			<s:DropDownList id="tiaojian" x="217" y="27"></s:DropDownList>
			<code:PromptingTextInput id="yunsuanfu" x="361" y="27" prompt="请输入条件"></code:PromptingTextInput>
			<s:NumericStepper id="yunsuanfuNum" x="361" y="27" visible="false" minimum="1" maximum="100000"></s:NumericStepper>
			<mx:DateField id="yunsuanfuDate" x="361" y="27" visible="false" formatString="YYYY-MM-DD" selectedDate="{new Date()}"></mx:DateField>
			<editor:ComboBoxEditor id="yunsuanfuTbl" x="361" y="27" visible="false"></editor:ComboBoxEditor>
			
			<mx:HRule x="29" y="56" width="80%" />
			<mx:Button label="上移" click="shangyi(event)"  x="64" y="153" icon="{MenuImageClass.shangyi}"/>
			<mx:Button label="下移" click="xiayi(event)"  x="65" y="186" icon="{MenuImageClass.xiayi}"/>
			<mx:Button id="del" label="删除" toolTip="删除" icon="{MenuImageClass.deleteSearchStr}"  x="64" y="120" click="del_clickHandler(event)"/> 
			<mx:Button id="add" label="添加" toolTip="添加检索条件" icon="{MenuImageClass.addSearchStr}" click="add_clickHandler(event)" x="166" y="69"/> 
			<mx:Button id="edit" label="修改" toolTip="修改检索条件" icon="{MenuImageClass.editSearchStr}"  x="254" y="69" click="edit_clickHandler(event)"/> 
			<mx:Button label="清空" toolTip="清空查询条件" icon="{MenuImageClass.qingkong}"  x="344" y="69" click="searchListValues.removeAll()"/> 
			<s:List id="searchList" x="150" y="100" width="272" height="151" dataProvider="{searchListValues}" labelField="value">
			</s:List>
			<s:Label x="73" y="100" text="检索条件:"/>
			<mx:Button label=")"  x="437" y="213" click="button1_clickHandler(event)" />
			<mx:Button label="("  x="437" y="186"  click="button1_clickHandler(event)" />
			<mx:Button label="非" x="437" y="158"  click="button1_clickHandler(event)" />
			<mx:Button label="或者" x="437" y="131"  click="button1_clickHandler(event)" />
			<mx:Button label="并且" x="437" y="101"  click="button1_clickHandler(event)" />
			<mx:HRule x="29" y="263" width="80%" />
			<s:RadioButton id="allSearch" label="全部数据中检索" toolTip="全部数据中检索" groupName="{group3}"  x="166" y="273"/>
			<s:RadioButton id="extendsSearch" label="当前结果中检索" toolTip="当前结果中检索" groupName="{group3}" selected="true" x="281" y="273"/>
		</s:Group>
</s:TitleWindow>
?

.

1 楼 fashionia 2010-08-26  
代码不贴出来让大家学习下?
2 楼 godson_2003 2010-08-27  
做的挺漂亮 不过还是喜欢js+html+css
  相关解决方案