iteye_5647 2009-09-12 11:33
浏览 236
已采纳

关于EXT用json返回结果分页的问题

最近在用ext,可遇到了一个很奇怪的问题
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"

  • request.getServerName() + ":" + request.getServerPort()
  • path + "/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">







    href="resources/css/ext-all.css" />

    <br> function renderSex(value) {<br> if (value == &#39;male&#39;) {<br> return &quot; <span style='color:red;font-weight:bold;'>红男 </span> <img src='user_male.png' />&quot;;<br> } else {<br> return &quot; <span style='color:green;font-weight:bold;'>绿女 </span> <img src='user_female.png' />&quot;;<br> }<br> }<br> function renderDescn(value, cellmeta, record, rowIndex, columnIndex, store) {<br> var str = &quot; <input type='button' value='查看详细信息' onclick='alert(\"" +<br> "这个单元格的值是:" + value + "\\n" +<br> "这个单元格的配置是:{cellId:" + cellmeta.cellId + ",id:" + cellmeta.id + ",css:" + cellmeta.css + "}\\n" +<br> "这个单元格对应行的record是:" + record + ",一行的数据都在里边\\n" +<br> "这是第" + rowIndex + "行\\n" +<br> "这是第" + columnIndex + "列\\n" +<br> "这个表格对应的Ext.data.Store在这里:" + store + ",随便用吧。" +<br> "\")'>&quot;;<br> return str;<br> }</p> <pre><code>Ext.onReady(function(){ </code></pre> <p>var sm = new Ext.grid.CheckboxSelectionModel();<br> var cm = new Ext.grid.ColumnModel([<br> new Ext.grid.RowNumberer(),<br> sm,<br> {header:&#39;编号&#39;,dataIndex:&#39;id&#39;,sortable:true},<br> {header:&#39;性别&#39;,dataIndex:&#39;sex&#39;,sortable:true,renderer:renderSex},<br> {header:&#39;名称&#39;,dataIndex:&#39;name&#39;,sortable:true},<br> {header:&#39;描述&#39;,dataIndex:&#39;descn&#39;,sortable:true,renderer:renderDescn}<br> ]);<br> var ds = new Ext.data.Store({<br> proxy: new Ext.data.HttpProxy({url:&#39;gird.jsp&#39;}),<br> reader: new Ext.data.JsonReader({<br> totalProperty: &#39;totalProperty&#39;,<br> root: &#39;root&#39;<br> },[<br> {name: &#39;id&#39;,mapping:0},<br> {name: &#39;sex&#39;,mapping:1},<br> {name: &#39;name&#39;,mapping:2},<br> {name: &#39;descn&#39;,mapping:3}<br> ])<br> });</p></li> </ul> <p>var grid = new Ext.grid.GridPanel({<br> renderTo: &#39;grid&#39;,<br> height:500,<br> ds: ds,<br> cm: cm,<br> sm: sm,<br> bbar: new Ext.PagingToolbar({<br> pageSize: 2,<br> store: ds,<br> displayInfo: true,<br> displayMsg: &#39;显示第 {0} 条到 {1} 条记录,一共 {2} 条&#39;,<br> emptyMsg: &quot;没有记录&quot;<br> })<br> });<br> ds.load({params:{start:0,limit:2}});<br> grid.render();<br> });<br>




    gird.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">








    <%
    String start = request.getParameter("start");
    String limit = request.getParameter("limit");
    System.out.println("start=" + start);
    System.out.println("limit=" + limit);
    try {
    int index = Integer.parseInt(start);
    int pageSize = Integer.parseInt(limit);
    String json = "{totalProperty:100,root:[";
    for (int i = index; i < pageSize + index; i++) {
        json += "{id:" + i + ",sex:'sex" + i + "',name:'name" + i + "',descn:'descn" + i + "'}";
        if (i != pageSize + index - 1) {
            json += ",";
        }
    }
    json += "]}";
    System.out.println("json===" + json);
    response.getWriter().write(json);
    response.getWriter().close();
    

    } catch(Exception ex) {
    }
    %>


    刷新index.jsp打印出来
    start=0
    limit=2
    json==={totalProperty:100,root:[{id:0,sex:'sex0',name:'name0',descn:'descn0'},{id:1,sex:'sex1',name:'name1',descn:'descn1'}]}

    点分页的下一页打印出
    start=2
    limit=2
    json==={totalProperty:100,root:[{id:2,sex:'sex2',name:'name2',descn:'descn2'},{id:3,sex:'sex3',name:'name3',descn:'descn3'}]}

    可页面gird里就是没有数据,分页有效 gird.jsp里的json是出来了,但index.jsp里的ext接收不到数据,表格里就是没数据显示,但表格也只显示2列,就是没实际的数据结果
    这是为什么?

  • 写回答

2条回答 默认 最新

  • sno2wolf 2009-09-14 10:09
    关注

    [color=darkred]把这两个函数放到Ext.onReady(){ }里面[/color]function

    renderSex(value) {
    if (value == 'male') {
    return " 红男 ";
    } else {
    return " 绿女 ";
    }
    }
    function renderDescn(value, cellmeta, record, rowIndex, columnIndex, store) {
    var str = " ";
    return str;
    }

    [color=darkred]把[/color]

        {name: 'id',mapping:0}, 
        {name: 'sex',mapping:1}, 
        {name: 'name',mapping:2}, 
        {name: 'descn',mapping:3} 
    

    [color=darkred]里面的mapping属性去掉,就可以了。但做好改成[/color]

    {name: 'id',type: 'int'},
    {name: 'sex', type: 'string'},
    {name: 'name', type: 'string'},
    {name: 'descn', type: 'string'}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化