日期:2014-05-16  浏览次数:20724 次

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,