easyUI学习笔记-----datagrid

这篇博客记录了作者在项目中使用easyUI框架的datagrid组件时的经验,主要聚焦于jsp页面上的实现和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中用到easyUI,记录下自己写的代码。

jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>库管设置</title>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/client/set_warehouse.js"></script>
</head>
<body>
	<table id="warehouseManage" style="height: 566px;width:100%"></table>
	<div id="addWarehouse" class="easyui-dialog" style="display:none;">
		<form method="post" id="addWarehouse_fm" >
				<table cellspacing="8px;">
					<tr>
						<td>排序:</td>
						<td>
							<input type="text" id="sortCode_fm" name="sortCode_fm" 
							value="100"class="easyui-validatebox"/> 
							<span style="color: red">*</span>
						</td>
					</tr>
					<tr>
						<td>库名称:</td>
						<td>
							<input type="text" id="warehouseName_fm" name="warehouseName_fm" 
							value="" class="easyui-validatebox"/> 
							<span style="color: red">*</span>
						</td>
					</tr>
					<tr>
						<td>备注:</td>
						<td>
							<textarea rows="8" cols="16"  id="remark_fm" 
							name="remark_fm" class="easyui-validatebox"></textarea>
						</td>
					</tr>
				</table>
			</form>
	</div>
	<div id="updateWarehouse" style="display:none;">
			<form method="post" id="updateWarehouse_fm" >
			<table cellspacing="8px;">
				<tr>
					<td>库id:</td>
					<td>
						<input type="text" id="warehouseId_update_fm" name="warehouseId_update_fm" 
						class="easyui-validatebox" disabled/> 
						<span style="color: red"></span>
					</td>
				</tr>
				<tr>
					<td>排序:</td>
					<td>
						<input type="text" id="sortCode_update_fm" name="sortCode_update_fm" 
						class="easyui-validatebox" /> 
						<span style="color: red">*</span>
					</td>
				</tr>
				<tr>
					<td>库名称:</td>
					<td>
						<input type="text" id="warehouseName_update_fm" name="warehouseName_update_fm" 
						class="easyui-validatebox"/> 
						<span style="color: red">*</span>
					</td>
				</tr>
				<tr>
					<td>备注:</td>
					<td>
						<textarea rows="8" cols="16"  id="remark_update_fm" name="remark_update_fm" 
						class="easyui-validatebox"/></textarea>
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

JS页面

//******库管设置JS*******


$(function(){
	
 		//-----------------ajax-----------
 		function warehouseManageListAjax(url,data,callback){
 			$.ajax({
 				type: 'post',
 				url: url,
 				dataType:"json", 
 			    /*contentType:"application/json",*/
 				data: data,
 				success: function(result){
 					callback(result);
 				}
 			});
 		};

 		function resetValue() {
 			$('#warehouseName_fm').val("");
 			$('#remark_fm').val("");
 		};
 		//------------查询-----------
 		function searchWarehouseListByWarehouseId(){
 			
 			//var a = $('#inputwarehouseId').val();
 			//$('#warehouseManage').datagrid({url:'../warehouse/getWarehouseById',queryParams:{warehouseId: 'a' }
 			//})
 			$('#warehouseManage').datagrid({  
 			    url:'../warehouse/getWarehouseListById',  
 			    queryParams:{  
 			    	warehouseId:'1'
 			    }  
 			}); 
 			//$('#warehouseManage').datagrid("options").url = '../warehouse/getWarehouseById';  
 			//$('#warehouseManage').datagrid('load',{
 				//warehouseId: $('#inputwarehouseId').val()
 			//})
 			//$('#inputCurrentStock_warehouseId').val('');
 		};	
 		
 		
 		
 		
 		
		//datagrid设置
		$('#warehouseManage').datagrid(//------datagrid设置 start------
					{
					title : '仓库列表', 		//表格标题
					iconCls : 'icon-list',  //表格图标
					nowrap : false, 		//是否只显示一行,即文本过多是否省略部分。
					striped : true,			//隔行变色特性 
					fitColumns : true, 		//防止水平滚动
					scrollbarSize : 0, 		//去掉右侧滚动条列
					//url : '../warehouse/listWarehouse',
					loader:firstloader,
					checkOnSelect : false,  //选中一行同时不会勾选复选框
					idField : 'warehouseId',         //指明哪一个字段是标识字段
					collapsible : false,	//是否可折叠的 
					singleSelect : true,	//只能单选
					frozenColumns : [ [
					{
						field : 'ck',
						checkbox : true
					}
							] ],
					pagination : true, //包含分页
					pageSize : 10,
					pageList : [10, 20, 30 ],//可以设置每页记录条数的列表 
					rownumbers : true,  //如果为true,则显示一个行号列.默认false
					singleSelect : true,//只能单选
					
					columns : [ [  //-----columns start-----
					{
						field : 'warehouseId',
						title : "仓库编号",
						width : 20,
						align : 'center'
					},
					{
						field : 'warehouseName',
						title : "仓库名称",
						width : 20,
						align : 'center'
					},
					{
						field : 'createUsername',
						title : "创建者",
						width : 20,
						align : 'center'
					},
					{
						field : 'createUserId',
						title : "创建者编号",
						width : 20,
						align : 'center',
						hidden : true
					},
					{
						field : 'hotelId',
						title : "宾馆编号",
						width : 20,
						align : 'center',
						hidden : true
					},
					{
						field : 'isPrimary',
						title : "是否主客",
						width : 20,
						align : 'center',
						hidden : true
					},
					{
						field : 'modifyTime',
						title : "修改时间",
						width : 30,
						align : 'center',
						formatter : function(value,row,index) {//将时间戳转换格式
							var times = getDate(value);
							return times;
						} 
					},
					{
						field : 'createTime',
						title : "创建时间",
						width : 30,
						align : 'center',
						formatter : function(value,row,index) {//将时间戳转换格式
							var times1 = getDate(value);
							return times1;
						} 
					},
					{
						field : 'modifyUserId',
						title : "修改者编号",
						width : 20,
						align : 'center',
						hidden : true
					},
					{
						field : 'sortCode',
						title : "排序",
						width : 20,
						align : 'center'
					},
					{
						field : 'version',
						title : "version",
						width : 20,
						align : 'center',
						hidden : true
					},
					{
						field : 'remark',
						title : "备注",
						width : 20,
						align : 'center'
					} 
							  ] ],//-----columns end-----

					toolbar : [  //-----toolbar start-----
							{
								text : '新增',
								iconCls : 'icon-add',
								handler : function() {
						 		//-----------warehouseManageListCallBack-------------
								var addWarehouseManageListCallBack = function (data){
									if(data!=-3333){
										if(data!=-111){
											resetValue();
											$.messager.alert('系统提示','保存成功!');
											addWarehouse_dialog.dialog("close");
											loadList();	
										}else if(data == -111){
											$('#warehouseName_fm').val("");
											$('#remark_fm').val("");
											$.messager.alert('系统提示','输入有误,保存失败!');
										}
									}else{
										$.messager.alert('系统提示','保存失败!');
										addWarehouse_dialog.dialog("close");
									}
								};
								//-----------addWarehouseManageListCallBack end-----------
								//显示添加界面
								var addWarehouse_dialog = $('#addWarehouse').dialog(
											{
												title : '添加仓库信息',
												width : 300,
												iconCls : 'icon-add',
												height : 300,
												closed : false,
												cache : false,
												modal : true, //定义是否将窗体显示为模式化窗口。
												buttons:[{
													text:'保存',
													handler:function(){
														var addWarehouse_url = '../warehouse/AddWarehouse';
														var addWarehouse_data = {};//传递参数
														addWarehouse_data.sortCode = $('#sortCode_fm').val();
														addWarehouse_data.warehouseName = $('#warehouseName_fm').val();
														addWarehouse_data.remark = $('#remark_fm').val();
														warehouseManageListAjax(addWarehouse_url,addWarehouse_data,
																addWarehouseManageListCallBack);
													}
												},{
													text:'关闭',
													handler:function(){
														resetValue();
														addWarehouse_dialog.dialog("close");
														}
												}]
											});
								}
							},
							'-',//在两个按钮中间加    ‘|’ 分割
							{
								text : "删除",
								iconCls : "icon-cut",
								handler : function() {
									//选中要修改删除的行
									var rows = $("#warehouseManage").datagrid('getSelections'); //返回所有选中的行
									
									if (rows.length == 1) {//选中几行的话触发事件
											//获得库id
											var warehouseId = rows[0].warehouseId;
											// 获得删除行索引    //返回指定行的索引号,该行的参数可以是一行记录或一个ID字段值
											var tableindex = $("#warehouseManage").datagrid('getRowIndex',rows[0]); 
											
										$.messager.confirm("提示","您确定要删除该仓库信息吗?",
												function(res) {//提示是否删除
														if (res) {
															 $.post('../warehouse/DelWarehouse',{warehouseId : warehouseId},
																	function(data) {
																		if (data != -3333) {
																			//删除选中的行
																			$("#warehouseManage").datagrid("deleteRow",tableindex);
																			//刷新
																			$("#warehouseManage").datagrid("reload");
																			$.messager.alert("系统提示", "数据已成功删除!");
																		} else {
																			$.messager.alert('系统提示','删除失败!','error');
																		}
																	}); 
														}
												});
									}else{
										$.messager.alert("系统提示", "请选择要删除的数据!");
									}
								}
							},
							'-',
							{
								text : "修改",
								iconCls : "icon-edit",
								handler : function() {
								//-----------editWarehouseManageListCallBack start-----------
								var editWarehouseManageListCallBack = function (data){
									if(data!=-3333){
										if(data!=-111){
											$.messager.alert('系统提示','修改成功!');
											editWarehouse_dialog.dialog("close");
											loadList();	
										}else if(data == -111){
											$.messager.alert('系统提示','修改失败!');
										}
									}else{
										$.messager.alert('系统提示','修改失败!');
										editWarehouse_dialog.dialog("close");
									}
								};
								//-----------editWarehouseManageListCallBack end-----------
									
								var rows = $("#warehouseManage").datagrid('getSelections'); //返回所有选中的行
								if (rows.length == 1) {//选中1行的话触发事件
									//获得修改前的信息
									var warehouseId = rows[0].warehouseId;
									var warehouseName = rows[0].warehouseName;
									var sortCode = rows[0].sortCode;
									var remark = rows[0].remark;
									$('#warehouseId_update_fm').val(warehouseId);
									$('#warehouseName_update_fm').val(warehouseName);
									$('#sortCode_update_fm').val(sortCode);
									$('#remark_update_fm').val(remark);
									
									//选中要修改修改的行的索引   //返回指定行的索引号,该行的参数可以是一行记录或一个ID字段值
									var tableindex = $("#warehouseManage").datagrid('getRowIndex',rows[0]); 
									//显示修改界面
									var editWarehouse_dialog = $('#updateWarehouse').dialog(
											{
												title : '修改仓库信息',
												width : 300,
												iconCls : 'icon-edit',
												height : 300,
												closed : false,
												cache : false,
												modal : true,
												buttons:[{
													text:'保存',
													handler:function(){
															var editWarehouse_url = '../warehouse/EditWarehouse';
															var editWarehouse_data = {};//传递参数
															editWarehouse_data.remark = $('#remark_update_fm').val();
															editWarehouse_data.sortCode = $('#sortCode_update_fm').val();
															editWarehouse_data.warehouseId = $('#warehouseId_update_fm').val();
															editWarehouse_data.warehouseName = $('#warehouseName_update_fm').val();
															warehouseManageListAjax(editWarehouse_url,editWarehouse_data,
																	editWarehouseManageListCallBack);
														    }
												},{
													text:'关闭',
													handler:function(){
														resetValue();
														updateWarehouse_dialog.dialog("close")
														}
												}]
											});
									}else{
										$.messager.alert("系统提示", "请选择要修改的数据!");
										return;
									}
								}
							},
							'-',
				            {
				            	text: '仓库编号:'
				            },
				            '-',
				            {
				            	text: '<input type="text" id="inputwarehouseId" placeholder="仓库编号"/>'
				            }, 
				            '-',
				            {
				            	text: '查询',
				            	iconCls: 'icon-search',
				            	handler: function(){
				            		if($('#inputwarehouseId').val() == ""){//输入为空,查询所有
				            			//$('#warehouseManage').datagrid("options").url = '../warehouse/listWarehouse';
				            			$('#warehouseManage').datagrid("options").loader=firstloader;
				            			$("#warehouseManage").datagrid("reload");
				            		
				            		}else{
				            			//$('#warehouseManage').datagrid("options").url = '../warehouse/getWarehouseListById';
				            			//$('#warehouseManage').datagrid('load',{warehouseId: $("#inputwarehouseId").val()});
				            			
				            			intwarehouseid=parseInt($('#inputwarehouseId').val());

				            			$('#warehouseManage').datagrid("options").loader=searchloader;
				            			$("#warehouseManage").datagrid("reload");  
				            
				            		}
				            		
				            		
				            	/*	var newData =[];
				            		var gridData = $("#warehouseManage").datagrid('getData');//获取当前grid的所有数据
				            		console.info(gridData);
				            		for(var i =0;i<gridData.total;i++){
				            			console.info(gridData[i].warehouseId);*/
				            		    /*if (gridData[i].warehouseId == $('#inputwarehouseId').val()) {
				            		        newData.push(gridData[i]);
				            		    }*/

				            		//}
				            		//$("#warehouseManage").datagrid('loadData',newData);
				            	}
				            } ], //-----toolbar end-----
							
							onLoadError : function(){
								$.messager.alert('温馨提示','数据加载失败!');
							}
			});//------datagrid设置 end------
						
			displayMsg();
			 //改变分页显示
		    function displayMsg(){
				 $('#warehouseManage').datagrid('getPager').pagination({  
		        		displayMsg : '当前显示<font color="red"> {from} - {to} </font>条记录   共 <font color="red">{total}</font> 条记录'  
		   		});  
			}
			 //刷新页
		    function loadList(){
		    	$("#warehouseManage").datagrid("reload");
			};
			
});//-----ready function end-----


var firstloader=function(param,success,error){
	
	$.ajax({
        url:"../warehouse/listWarehouse",
        type:"post",
        dataType:"json",
        success: function(data){
        	if(data=="-3333" || data=="")
        		{
        		success("[]");
        		return true;
        		}
        	//data="[]";
        	success(data);
        }
        ,error:function(err){
        	alert(err);
        }
	});

};
   
var intwarehouseid=0;
var searchloader=function(param,success,error){
	
	$.ajax({
        url:"../warehouse/getWarehouseListById",
        data:{warehouseId:intwarehouseid},
        type:"post",
        dataType:"json",
        success: function(data){
        	//alert(data);
        	var tmpjson=JSON.stringify(data);
        	//alert(tmpjson);
        	if(tmpjson=="[null]")
        		{
        		//清空数据
                $('#warehouseManage').datagrid('loadData', { total: 0, rows: [] });
                success("[]");
        		return true;
        		}
        	if(data=="-3333" || data=="")
    		{
        		//清空数据
                $('#warehouseManage').datagrid('loadData', { total: 0, rows: [] });
    		success("[]");
    		return true;
    		}
        	//如果不是用的list方法,这里给json前后加变成数组放入
        	success(data);
        	return true;
        }
        ,error:function(err){
        	alert(err);
        }
	});
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值