欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AIR桌面程序-实现对客户信息管理(FLEX+XML数据库)

程序员文章站 2022-03-02 13:37:36
...

本程序实现了对XML的增、删、改、查操作,并同步到xml数据库中

用到的技术:

1.Flex基本布局技术,常用控件操作

2.基本事件的触发、自定义事件的触发

3.flex读写更改 操作XML对象

4.基本数据库知识

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication 
						xmlns:mx="http://www.adobe.com/2006/mxml" 
						layout="absolute"
						width="720" 
						height="600" 
						creationComplete="initApp()"
						xmlns:s="library://ns.adobe.com/flex/spark" 
						viewSourceURL="srcview/index.html">
	
	<mx:Script>
		<![CDATA[
			import event.*;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.events.IndexChangedEvent;
			import mx.modules.Module;
			import mx.utils.StringUtil;
			
			import tree.*;  
			
			//set bindable
			[Bindable]
			internal var currentPeople:people=new people;
			public var peopledata:XML;
			public var path:File=File.documentsDirectory.resolvePath("D:/peoples.xml");
			public var n:Number;
			public var warning:String="填写信息不全";
			public var warningtitle:String="警告";
			public var searchwarning:String="填写内容不全";
			
			/**
			 * InitApp function handler
			 * */
			internal function initApp():void{				
				var peoples:FileStream=new FileStream;
				peoples.open(path,FileMode.READ);
				peopledata=XML(peoples.readUTFBytes(peoples.bytesAvailable));
				peoples.close();
				var users:Object=peopledata.user;
				n=0;
				
				for(var i:* in users){
					var newPeople:peopleItem=new peopleItem();
					var peopleInfo:people=new people;
					peopleInfo.setpeople(users[i]);
					newPeople.p=peopleInfo;
					holder_user.addChild(newPeople);
					newPeople.addEventListener(selectPeopleEvent.CLICK_PEOPLE,selectHandler);
					newPeople.y=n*newPeople.height;
					n++;
				}
			}
			/**
			 * writedata
			 **/
			public function writedata():void{
				var writedata:FileStream=new FileStream;
				writedata.open(path,FileMode.WRITE);
				writedata.writeUTFBytes(peopledata.toString());
				writedata.close();
			}
			
			/**
			 * 
			 * match
			 * */
			public function match(selectedone:people):int{
				var index:int=new int;
				var conter:int;
				for(conter=n-1;conter>=0;conter--){
					if(selectedone.id==peopledata.user[conter].id){
						index=conter;
					}else{
						continue;
					}
				}
				return index;
			}
			/**
			 * 
			 * 
			 * */
			internal function selectHandler(evt:selectPeopleEvent):void{
				currentPeople=evt.selectedUser;
			}
			/**
			 * 
			 * 
			 * */
			public function clearHandler():void{
				addname.text="";
				addotherinfo.text="";
				addphone.text="";
				addotherinfo.text = "";
				addmale.selected=false;
				addfemale.selected=false;
			}
			/**
			 * 
			 * 
			 * */
			public function addHandler():void{
				var addcontent:XML;
				if(addname.text==""||addotherinfo.text==""||addphone.text==""||(!(addmale.selected||addfemale.selected))){
					Alert.show(warning,warningtitle,Alert.OK);
				}
				else if(addmale.selected){
					 addcontent = <user>
						 					<id>{n}</id>
											<name>{addname.text}</name>
											<sex>male</sex>
											<phonenum>{addphone.text}</phonenum>
											<otherinfo>{addotherinfo.text}</otherinfo>
											</user>;
					peopledata.appendChild(addcontent);
					writedata();
					holder_user.removeAllChildren();
					initApp();
					Alert.show("添加成功!");
				}
				else{
					addcontent  =   <user>
											<id>{n}</id>
											<name>{addname.text}</name>
											<sex>female</sex>
											<phonenum>{addphone.text}</phonenum>
											<otherinfo>{addotherinfo.text}</otherinfo>
											</user>;
					peopledata.appendChild(addcontent);
					writedata();
					holder_user.removeAllChildren();
					initApp();
					Alert.show("添加成功!");
				}
				clearHandler();
			}
			public function textChangeHandler(modified:people):void
			{
				var num:int=match(modified);
				peopledata.user[num].name=peoplename.text;
				peopledata.user[num].otherinfo=peopleotherinfo.text;
				peopledata.user[num].phonenum=peoplephone.text;
				writedata();
				holder_user.removeAllChildren();
				initApp();
				if(male.selected){
					peopledata.user[num].sex="male";
				}
				else{
					peopledata.user[num].sex="female";
				}
			}
			/**
			 * 
			 * 
			 * */
			public function modifyHandler(modified:people):void{
				if(this.ModifyBtn.selected)
				{
					peoplename.enabled = true;
					peoplesex.enabled = true;
					peopleotherinfo.enabled = true;
					peoplephone.enabled = true;
					this.ModifyBtn.label = "保存信息";
				}		
				else
				{
					var num:int=match(modified);
					/* 				if(peoplename.text==""||peoplephone.text==""||peopleid.text==""||(!(male.selected||female.selected))){
					Alert.show(warning,warningtitle,Alert.OK);
					} */
					peopledata.user[num].name=StringUtil.trim(peoplename.text);
					peopledata.user[num].phonenum=StringUtil.trim(peoplephone.text);
					peopledata.user[num].otherinfo=StringUtil.trim(peopleotherinfo.text);
					writedata();
					holder_user.removeAllChildren();
					initApp();
					if(male.selected){
						peopledata.user[num].sex="male";
					}				
					else{
						peopledata.user[num].sex="female";
					}
					Alert.show("修改成功");
					selectPeopleEvent.userItemClicked = false;
					this.ModifyBtn.label = "修改此客户";
				}
			}
			/**
			 * 
			 * 
			 * */
			public function deleteHandler(deleted:people):void{				
				var num:int=match(deleted);
				delete peopledata.user[num];
				writedata();
				holder_user.removeAllChildren();
				n--;
				//Alert.show("Delete success!");
				initApp();
				selectPeopleEvent.userItemClicked = false;
			}
			/**
			 * Search Handler
			 * search a user by its name or phone(part of its name or phonenum)
			 * */
			public function searchHandler():void{
				var aim:int=new int;
				aim=n-1;
				if(searchname.text==""&&searchphone.text==""){
					Alert.show(searchwarning,warningtitle,Alert.OK);
				}else{
					for(aim;aim>=0;aim--){
						if(peopledata.user[aim].name.indexOf(StringUtil.trim(searchname.text)) != -1 || peopledata.user[aim].phonenum.indexOf(StringUtil.trim(searchphone.text)) != -1
							|| peopledata.user[aim].name == StringUtil.trim(searchname.text) || peopledata.user[aim].phonenum == StringUtil.trim(searchphone.text)
							|| peopledata.user[aim].name.search(StringUtil.trim(searchname.text)) != -1 || peopledata.user[aim].phonenum.search(StringUtil.trim(searchphone.text)) != -1	){
							break;
						}
					}
					currentPeople.setname(peopledata.user[aim].name);
					currentPeople.setsex(peopledata.user[aim].sex);
					currentPeople.setphone(peopledata.user[aim].phonenum);
					currentPeople.setotherInfo(peopledata.user[aim].otherinfo);
				}
				holder_user.removeAllChildren();
				initApp();
			}

		]]>
	</mx:Script>
	
	<mx:Panel width="200" layout="absolute" cornerRadius="15" title="客户列表" left="10" top="10" height="540">
		<mx:Canvas id="holder_user"   width="100%" height="100%">
		</mx:Canvas>
	</mx:Panel>
	<mx:Panel id="userInfo" width="435" layout="absolute" height="540" title="客户详细信息" cornerRadius="15" top="10" left="218">
		<s:VGroup paddingTop="10" paddingLeft="20" id="manageUser">	
			<s:HGroup gap="10" width="100%" height="25">
				<s:Label text="客户信息管理" paddingTop="5" fontSize="14" fontWeight="bold" toolTip="点击左边的用户即可"/>
				<s:HGroup gap="10" width="100%" height="30" paddingLeft="10" visible="{selectPeopleEvent.userItemClicked}" includeInLayout="{selectPeopleEvent.userItemClicked}" >
					<s:ToggleButton label="修改此客户"  width="100" id="ModifyBtn" change="modifyHandler(currentPeople)"/>
					<mx:Button  label="删除此客户"  width="100" click="deleteHandler(currentPeople)"/>	
				</s:HGroup>
			</s:HGroup>
					
			<s:HGroup gap="10"  width="400" height="30" paddingTop="5">
				<mx:Label text="姓名:"/>
				<mx:TextInput text="{currentPeople.name}"  id="peoplename" enabled="{!selectPeopleEvent.userItemClicked}" />
			</s:HGroup>
			<s:HGroup gap="10"  width="400" height="30" paddingTop="5">
				<mx:Label text="性别:"/> 
				<mx:TextInput text="{currentPeople.sex}"  id="peoplesex" enabled="{!selectPeopleEvent.userItemClicked}" />
				<mx:RadioButton  label="男"   id="male"/>
				<mx:RadioButton  label="女"  id="female"/>
			</s:HGroup>
			<s:HGroup  gap="10"  width="400" height="30" paddingTop="5">
				<mx:Label text="手机:"/>
				<mx:TextInput  text="{currentPeople.phonenum}" id="peoplephone" enabled="{!selectPeopleEvent.userItemClicked}" />
			</s:HGroup>
			<s:HGroup gap="10"  width="400" height="30" paddingTop="5">
				<mx:Label text="其他:"/>
				<mx:TextInput  text="{currentPeople.otherinfo}"  id="peopleotherinfo" enabled="{!selectPeopleEvent.userItemClicked}" />
			</s:HGroup>
		</s:VGroup>		
	
		<s:VGroup gap="2" width="100%" paddingTop="200" horizontalAlign="left" paddingLeft="20" id="searchUser">
			<mx:HRule width="80%" height="4" />
			<s:Label text="搜索客户 (请输入要查询的客户姓名 或 客户手机号码)" fontSize="14" fontWeight="bold" paddingTop="5"/>
			<s:HGroup gap="10">
				<mx:Label  text="客户姓名:" />
				<mx:TextInput   id="searchname"/>
				<mx:Button  label="搜索" click="searchHandler()"/>
			</s:HGroup>	
			<s:HGroup gap="10">
				<mx:Label  text="手机号码:"  />
				<mx:TextInput    id="searchphone"/>		
				<mx:Button  label="搜索" click="searchHandler()"/>
			</s:HGroup>
			<s:HGroup paddingTop="10"/>
			<mx:HRule width="80%" height="4"/>
		</s:VGroup>
		
		<s:VGroup gap="2" width="100%" paddingTop="300" paddingLeft="20" id="addUser">
			<s:Label text="添加新客户信息" fontSize="14" fontWeight="bold" paddingTop="5"/>
			<s:HGroup gap="10" paddingTop="5" width="400" height="30">
				<mx:Label  text="姓名:" />
				<mx:TextInput   id="addname"/>
			</s:HGroup>	
			<s:HGroup width="400" height="30" paddingTop="5">
				<mx:Label  text="性别:" />
				<mx:RadioButton  label="男"   id="addmale"/>
				<mx:RadioButton  label="女"  id="addfemale"/>
			</s:HGroup>
			<s:HGroup gap="10" width="400" height="30" paddingTop="5">
				<mx:Label  text="手机:"  />
				<mx:TextInput   id="addphone"/>		
			</s:HGroup>
			<s:HGroup gap="10" width="400" height="30" paddingTop="5">
				<mx:Label  text="其他:"  />
				<mx:TextInput  id="addotherinfo"/>
			</s:HGroup>						
			<s:HGroup gap="10" width="400" height="40" paddingLeft="10" paddingTop="10">
				<s:Button  label="添加" click="addHandler()"/>
				<s:Button label="取消" click="clearHandler()"/>
			</s:HGroup>			
		</s:VGroup>
			
	</mx:Panel>
</mx:WindowedApplication>

 完整源码   待续……

  • AIR桌面程序-实现对客户信息管理(FLEX+XML数据库)
            
    
    博客分类: Flex AIRXMLFlexAdobe 
  • 大小: 14.9 KB