0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

搜索栏的里int型,显示出对应的String文字

Last updated at Posted at 2019-06-22
  • 实体类中&表中
student.java
    private int sclass; //关联班级-页面中是combobox 
	private int senterprise;  //关联所属企业-页面中是combobox combobox 
	private int sstatus; //管理状态 -页面中是combobox select 

  • dao方法中
daoimpl.java

//在servlet中,接收的是从页面传来的数据,比如接收了页面中id 为 search_class,search_enterprise的数据, 
//它接受数据的类型是String,(注意,虽然值是0,1 之类,但是它是String 类型)。 
//那么为什么值是0,1,页面上显示的却是 文字的“java基础班”,“阿里巴巴公司”呢。----->>往下看页面的写法。


//所以在调用dao层方法,注意此时,也把参数做成了String. 
public ArrayList<Student> queryStu(String sname, String sclass, String senterprise, PageUtil pageUtil) {
		String sql="select * from student where 1=1  ";
		//做一个集合for 动态拼接
		ArrayList<Object> al=new ArrayList<>();
		
		//动态拼接
		if(sname!=null&&!sname.equals("")) {
			sql=sql+ "  and sname like ? " ;
			al.add('%'+sname +'%');		
		}
	
		//在表中是int型 不能进行模糊查询 !注意!!!!!!!!!
		if(sclass!=null&&!"".equals(sclass)) {
			sql=sql+ "  and sclass = ? " ;
			al.add(Integer.parseInt(sclass));			
		}
		
		if(senterprise!=null&&!"".equals(senterprise)) {
			sql=sql+ "  and senterprise = ? " ;
			al.add(Integer.parseInt(senterprise));			
		}
		
		sql=sql+"  limit ?,?" ;
		
		al.add(pageUtil.getStartIndex());
		al.add(pageUtil.getPageSize());
		
		//把集合转成数组
		Object [] obj=al.toArray();
		//做一个集合for学生
		 ArrayList<Student>  ssal=new ArrayList<Student> (); 
		
		//工具类方法 
		ResultSet rs= DBUtils.query(sql, obj);	
		try {
			while(rs.next()) {
				//new对象
				Student s=new Student(rs.getInt("sno"),
						rs.getString("sid"),
						rs.getString("spwd"),
						rs.getString("sname"),
						rs.getString("sgender"),
						rs.getInt("sage"),
						rs.getString("stel"),
						rs.getString("saddress"),
						rs.getInt("sclass"),
						rs.getInt("senterprise"),
						rs.getString("smtime"),
						rs.getInt("sstatus")
						);			
				
				//对象放入集合里
				ssal.add(s);				
			}
			
			return ssal;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

  • 页面中
student.html
//显示方式,用“formatter”这个方法实现! 

 {field:'sclass',title:'班级',width:150, 
 		        	formatter: function(value,row,index){
 						if (row.sclass){
//含义为:combobox里加载全部班级得到一个集合,去遍历这个集合,如果集合中一个班级的编号,与 学生选定这一行的学生班级(int型的数字)相同,则返回一个 这个 班级的名字。 
columns: [[  
			{field:'chk',checkbox: true,width:50},
 		        {field:'sno',title:'学生编号',width:50, sortable: true},    
 		        {field:'sid',title:'用户名',width:200, sortable: true}, 
 		        {field:'spwd',title:'密码',width:200, sortable: true},
 		     	       
 		        {field:'sclass',title:'班级',width:150, 
 		        	formatter: function(value,row,index){
 						if (row.sclass){
 							var ccList = $("#search_sclass").combobox("getData");
 							for(var i=0;i<ccList.length;i++ ){
 								//console.log(clazzList[i]);
 								if(row.sclass == ccList[i].cno)
 									return ccList[i].cname;
 							}						
 							
 							return row.sclass;
 						} else {
 							return 'not found';
 						}
 					}
				},
				
				{field:'senterprise',title:'所属公司',width:150, 
 		        	formatter: function(value,row,index){
 						if (row.senterprise){
 							var eeList = $("#search_senterprise").combobox("getData");
 							for(var i=0;i<eeList.length;i++ ){
 								//console.log(clazzList[i]);
 								if(row.senterprise == eeList[i].eno)return eeList[i].ename;
 							}
 							return row.senterprise;
 						} else {
 							return 'not found';
 						}
 					}
				},
                           

//含义为选定这行的状态如果是0则显示的是"已毕业" 	
				   {field:'sstatus',title:'状态',width:50,
					  formatter: function(value,row,index){
	 							if(row.sstatus==0){
	 								return "已毕业";
	 							}else if(row.sstatus==1){
	 								return "学习中";
	 							}	 							
	 					}				  			  
				  }, 


//关于上文的悬念: 那么为什么值是0,1,页面上显示的却是 文字的java基础班”,“阿里巴巴公司----->>往下看页面的写法。

	
	  	//班级下拉框
	  	function preLoadClass(){
	  		$("#search_sclass").combobox({
		  		width: "150",
		  		height: "25",
//valueField-值是cno(编号), textField-显示的文字是cname(名字)
		  		valueField: "cno",
		  		textField: "cname",
		  		multiple: false, //可多选
		  		editable: true, //不可编辑
		  		method: "post",
		  		url: "ClaServlet?method=getClassList&from=combox",
		  		onChange: function(newValue, oldValue){
		  			//加载班级下的学生
		  			//$('#dataList').datagrid("options").queryParams = {clazzid: newValue};
		  			//$('#dataList').datagrid("reload");
		  		}
		  	});
	  	}
	  	
	  	//所属公司下拉框
	  	function preLoadEnt(){
	  		$("#search_senterprise").combobox({
		  		width: "150",
		  		height: "25",
		  		valueField: "eno",
		  		textField: "ename",
		  		multiple: false, //可多选
		  		editable: false, //不可编辑
		  		method: "post",
		  		url: "EntServlet?method=entList&from=combox",
		  		onChange: function(newValue, oldValue){
		  			//加载班级下的学生
		  			//$('#dataList').datagrid("options").queryParams = {clazzid: newValue};
		  			//$('#dataList').datagrid("reload");
		  		}
		  	});
	  	}

////////在add,edit 窗口中, status的写法 
<td>
	   			
	    			<select  id="edit_sstatus"  name="edit_sstatus"class="easyui-combobox"    style="width: 200px; height: 30px;" > 
	    			 <option value="0">已毕业 </option>  
	    			 <option value="1">学习中</option>  
	    			 </select>   
	    			 </td>


//编辑窗口中,先显示本来的值。 
	$("#edit_sstatus").combobox('setValue', selectRow.sstatus);



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?