首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ExtJs 表格数据load的有关问题

2012-03-07 
ExtJs 表格数据load的问题 - Web 开发 / Ajaxdata.jspJava code%@ page languagejava importjava.ut

ExtJs 表格数据load的问题 - Web 开发 / Ajax
data.jsp

Java code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>></title>        <meta http-equiv="pragma" content="no-cache">        <meta http-equiv="cache-control" content="no-cache">        <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">        <meta http-equiv="description" content="This is my page">    </head>    <body>        <%            String start = request.getParameter("start");            String limit = request.getParameter("limit");            //StringBuffer json = new StringBuffer();            String json = "";            try {                int index = Integer.parseInt(start);                int pageSize = Integer.parseInt(limit);                //json.append("{ totalProperty : 100 , root :[");                json += "{totalProperty:5,root:[";                for (int i = index; i < pageSize + index; i++) {                    json += "{id:" + i + ",name:'chen" + i + "',descn:'descn"                            + i + "'}";                    if (i != pageSize + index - 1) {                        json += ",";                    }                    /*                    json.append("{ id : ").append(i).append(", name: ").append(                            "chen").append(i).append(",descn : ").append(                            "descn").append(i).append("}");                    if (i != pageSize + index - 1) {                        json.append(",");                    }                     */                }                //    json.append("]").append("}");                json +="]}";            } catch (Exception e) {            }            System.out.println(json);            response.getWriter().write(json);            response.getWriter().flush();            //out.print(json.toString());        %>    </body></html>


控制台输出:
{
  totalProperty : 10,
  root:[
  {id:1,name:'cccc',descn:'sddssd'},
  {id:2,name:'cccc',descn:'sddssd'},
  {id:3,name:'cccc',descn:'sddssd'}
  ]
}

tt.js
JScript code
Ext.onReady(function() {    var myrecord = Ext.data.Record.create([{        name : 'id',        mapping : 'id',        type : 'int'    }, {        name : 'name',        mapping: 'name',        type : "string"    }, {        name : 'descn',        mapping: 'descn',        type : "string"    }]);    var ds = new Ext.data.Store({        proxy : new Ext.data.HttpProxy({            url : 'data.jsp'        }),        reader : new Ext.data.JsonReader({            totalProperty : 'totalProperty ',            root : 'root'        }, myrecord)    });    ds.load({        params : {            start : 0,            limit : 5        }    });    var cm = new Ext.grid.ColumnModel([{        header : '编号',        dataIndex : 'id'    }, {        header : '姓名',        dataIndex : 'name'    }, {        header : '描述',        dataIndex : 'descn'    }]);    var grid = new Ext.grid.GridPanel({        store : ds,        cm : cm,        loadMask : true,        width : 500,        height : 300,        el : 'tt',        bbar : new Ext.PagingToolbar({            pageSize : 5,            store : ds,            displayMsg : "第 {0} 到{1} 条记录,共有 {2} 条记录",            displayInfo : true,            emptyMsg : '没有记录'        })    });    ds.load({        params : {            start : 0,            limit : 5        }    });    grid.render();    ds.load({        params : {            start : 0,            limit : 5        }    });    Ext.Msg.alert("无标题", ds.getCount() + "条记录");     //为什么这里总是没数据呢。。    for (var v = 1; v < ds.getCount(); v++) {        Ext.Msg.alert("我郁闷", ds.getAt(v).get('name'));    }}); 



为什么ds.load();//总是没数据呢。。 而data.jsp 里面的数据打印了出来,说明调用了.好郁闷的..


[解决办法]
这是个异步加载的问题,你执行这句的时候
Ext.Msg.alert("无标题", ds.getCount() + "条记录"); //为什么这里总是没数据呢。。
for (var v = 1; v < ds.getCount(); v++) {
Ext.Msg.alert("我郁闷", ds.getAt(v).get('name'));
}
可能还没有加载完成

这样试试
JScript code
 ds.load({        params : {            start : 0,            limit : 5        },callback:function(){             Ext.Msg.alert("无标题", ds.getCount() + "条记录");     //为什么这里总是没数据呢。。    for (var v = 1; v < ds.getCount(); v++) {        Ext.Msg.alert("我郁闷", ds.getAt(v).get('name'));    }        }    }); 

热点排行