天天看点

easyui datagrid请求数据失败,页面提示

    datagrid方法用来将请求到的数据加载到表格中,但考虑一种特殊情况,就是服务器崩掉比如tomcat崩了,请求不到数据,还是会加载出一个表格的框框,如下图,

easyui datagrid请求数据失败,页面提示

    这样就会影响用户体验,并且报错:Uncaught TypeError: Cannot read property 'length' of undefined。这是因为没有请求到数据,渲染表格出错。

easyui datagrid请求数据失败,页面提示

    所以当没有请求到数据时,需要给用户一个提示。

    实现方式可参照easyui官方提供的api

loader: function(param, success, error){
				var opts = $(this).datagrid('options');
				if (!opts.url) return false;
				$.ajax({
					type: opts.method,
					url: opts.url,
					dataType: 'json',
					success: function(data){
						if (data.success==false){
							error(data);
						} else {
							success(data);				
						}
					},
					error: function(){
						error.apply(this, arguments);
					}
				});
			},
           

首先要调用loader方法,上面opts是easyui封装的一个对象,有请求的url、请求的方法'post'/'get',然后发送ajax请求,根据请求到的结果data判断执行success()方法还是error()方法,success和error方法对应easyui的onLoadSuccess和onLoadError方法。

onLoadSuccess:function(data){
				//
			}
           
onLoadError: function(data){
			    // ...
				$.messager.alert('提示信息','请求异常','info',function(){
					 
			 	});   
			},
           

在loader方法中执行error(data)方法时,就会调用onLoadError方法,弹框给用户提示,如下图

easyui datagrid请求数据失败,页面提示

(老铁们,如果解决了你的问题,点个赞噻)

继续阅读