jqGird 学习小结

建立返回jsp的model

public class BackListModel   {
    public BackListModel(int code, Object data, String msg, int page, int total, int records) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.page = page;
        this.total = total;
        this.records = records;
    }
    private int code;
    private  String msg;
    private  Object data;
    // 下面的三个参数参数名很重要 不要错。
    private int page;
    private int total;
    private int records;
    }
   @RequestMapping(value = "show" )
    public void show( int page, int rows, HttpServletRequest request, HttpServletResponse response) {
        /*page 当前页码  rows 每页几个  这个是页面JQGird传进来的*/
        String msg = "";
        int code  = 0;

        List list = userService.findAll((page-1)*rows,rows);
        code = 1;

        int records =userService.findAllCount();
        int total = (int) Math.ceil(records*1.0/rows);
        /* total总页数  records 记录总个数*/
        String res =  JSONObject.toJSON(new BackListModel(code,list,msg,page,total,records)).toString();
        response.setContentType("application/json;charset=utf-8");
        try {
            response.getWriter().write(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

jsp:

 $(function() {
            init();
            $("#list").jqGrid('navGrid', '#pager',  { edit: false, add: false, del: false, search:false });
        });
        function  init() {
        $("#list").jqGrid({
            jsonReader: {
              root:"data",//解析model的字段
              repeatitems : false
            },
            url : '${ctx}/userApi/show.do',
            datatype : "json",
            colNames : [  'userid','useraccount', 'userpassword','action' ],
            colModel : [

                {name : 'userInfoId',width : 90,sortable:false,align : 'center'},

                {name : 'userInfoAccount',width : 90,sortable:false,align : 'center'},

                {name : 'userInfoPwd',width:90,sortable:false,align :'center'},

                {name : 'userInfoId' , width : 40,sortable:false,align : 'center',formatter : function (value,options,row) {
                    var html = "";

                    html += '<a onclick="del(\''+value+'\')" style="cursor: pointer;color:blue">删除</a>';

                    return html;
                }}
            ],
            rowNum : 5,//初始化每页几个
            rowList : [ 5, 10, 15 ],
            width :  $(window).width()-80,
            height : $(window).height()-300,
            pager : '#pager',//页脚
            page:1,//初始化第几页
            mtype : "post",
            viewrecords : true,
            caption : "JSON Example"//表格的标题名字
        });
        }

        function del(value){
            $.ajax({
                type: "POST",
                url: '${ctx }/userApi/delete.do',
                data: {"userid":value},
                dataType: 'json',
                success: function (data) {
                    if (1 == data.code) {

                    }else{
                        layer.msg(data.msg);
                    }

                }
            });
        }

    </script>
</head>
<body>
<h1> user list</h1>
<table id="list"></table>
<div id="pager"></div>
</body>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值