datagrid介面,連結資料庫讀取資料,datagrid介面

來源:互聯網
上載者:User

datagrid介面,連結資料庫讀取資料,datagrid介面

 

 

1、學生列表的 HTML部分

<script type="text/javascript">$(function(){    //建立dataGrid    $("#dg").datagrid({        url:'StudentServlet',    //資料來源        //凍結列        frozenColumns:[[        {field:'id',checkbox:true},        {field:'sno',title:'學號',width:100,sortable:true} ]],        \\可排列                //列的定義        columns:[[            {field:'sname',title:'姓名',width:100},            {field:'sclass',title:'班級',width:100,align:'right'},            {field:'ssex',title:'性別',width:100,align:'center',hidden:false},                        {field:'sbirthday',title:'生日',width:100,align:'center'    }        ]],        fitColumns:true, //不能和凍結列同時設定        striped:true,//斑馬線效果        idField:'sno',//主鍵列,        rownumbers:true,//顯示行號        singleSelect:false,//是否單選        pagination:true,//顯示分頁欄        pageList:[5,10,20],//每頁行數挑選清單        pageSize:5,//初始頁面大小        remoteSort:false,//是否伺服器端排序        toolbar:[                 {iconCls:'icon-edit',text:'跳到第2頁',                     handler:function(){$('#dg').datagrid('gotoPage', 2)}                                      },                 {iconCls:'icon-edit',text:'跳到第3頁',                     handler:function(){$('#dg').datagrid('gotoPage', 3)}                                      }                 ]    });})</script>資料表格<br><table id="dg"></table></body>

 2、StudentServlet部分(資料來源)

 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                request.setCharacterEncoding("UTF-8");                response.setCharacterEncoding("UTF-8");        response.setContentType("text/html");      \\漢化                String spage = request.getParameter("page");    \\擷取page        String srows = request.getParameter("rows");        \\擷取rows                            if(spage!=null&&srows!=null)        {        int page =Integer.parseInt(spage);        int rows =Integer.parseInt(srows);            String json = new StudentService().getPageJSON(page,rows);    \\調用方法                response.getWriter().println(json);        }        else        {            response.getWriter().println("{'total':0,'row':[]}" );        }                    }

3、StudentService() 類

public class StudentService {        //查詢分頁資料    //返回JSON    public String getPageJSON(int page,int rows)    {        String rtn =    "{\"total\":0,\"row\":[]}" ;                int total  =  new StudentDAO().getTotal();                if(total > 0 )        {            List<Student> ls = new StudentDAO().getPageList(page, rows);                        String ls_json = JSONArray.toJSONString(ls);                        rtn = "{\"total\":"+total+",\"rows\":"+ls_json+"}";          \\規範 json 格式        }        return rtn;    }    }

 

4、StudentDAO()類內的 getPageList(擷取分頁資料集合) 和 getTotal(擷取資料條數) 的方法

//擷取分頁資料集合    public List<Student> getPageList(int page, int rows)    {        List<Student> rtn = new ArrayList<Student>();                init();                rtn = se.createQuery("from Student order by sno").                setMaxResults(rows).setFirstResult((page-1)*rows)                .list();                destroy();                        return rtn;            }            //擷取資料條數    public int getTotal()    {        int rtn = 0;                init();                List<Object> lo = se.createQuery("select count(1) from Student").list();                    if(lo!=null&&lo.size()>0)        {            rtn = Integer.parseInt(lo.get(0).toString());        }                        destroy();                return rtn;    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.