jqgrid表格控件

本文介绍了如何在项目中引入并使用jqGrid表格控件,包括在service层和controller层的配置与操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 需引入
    <link rel="stylesheet" href="${path}/style/bootstrap/jqgrid/css/css/hot-sneaks/jquery-ui-1.8.16.custom.css">
    <link rel="stylesheet" href="${path}/style/bootstrap/jqgrid/boot/css/trirand/ui.jqgrid-bootstrap.css">
    <script src="${path}/style/bootstrap/jqgrid/js/i18n/grid.locale-cn.js"></script>
    <script src="${path}/style/bootstrap/jqgrid/boot/js/trirand/jquery.jqGrid.min.js"></script>
    <script src="${path}/style/bootstrap/js/ajaxfileupload.js"></script>
  1. 一个例子
<script>
    $(function (){
        pageInit();
    })

    function pageInit() {
        $("#videoTable").jqGrid({
            url: "${path}/emp/fenye",   //page,rows   //返回数据:total总页数 records总条数  page当前页  row数据
            editurl: "${path}/emp/edit",
            datatype: "json",
            cellEdit:true,
            rowNum: 2,
            rowList: [2, 10, 20, 30],
            pager: '#videoPage',
            styleUI: "Bootstrap",
            autowidth: true,
            reloadAfterSubmit: true,
            height: "auto",
            viewrecords: true,  //是否显示总条数
            colNames: ['ID', '员工名', '工资', '年龄', '部门'],
            colModel: [
                {name: 'id', index: 'id', width: 55},
                {name: 'empname', editable: true, index: 'invdate', width: 90},
                {name: 'salary', editable: true, index: 'invdate', width: 90},
                {
                    name: 'age', editable: false, index: 'name asc, invdate', width: 100, align: "center"
                },
                {
                    name: 'deptname',editable: true, index: 'amount', width: 80, align: "right"
                }
            ]
        });
        //编辑工具栏
        //编辑工具栏
        $("#videoTable").jqGrid('navGrid', '#videoPage',
            {edit: true, add: true, del: true, addtext: "添加"},
            {
                closeAfterEdit: true,
                reloadAfterSubmit: false,
                afterSubmit: function (data) {
                    $.ajaxFileUpload({
                        success: function () {
                            //上传成功 所作的事情
                            //刷新页面
                            $("#videoTable").trigger("reloadGrid");
                        }
                    });
                    return "hh";
                }
            },  //修改之后的额外操作
            {
                closeAfterAdd: true,//关闭添加框
                afterSubmit: function (data) {  //添加成功之后执行的内容
                    //1.数据入库    文件数据不对   文件没有上传
                    //2.文件异步上传   添加成功之后  上传
                    //3.修改文件路径   (id,要的的字段内容)
                    //id=  data.responseText
                    console.log(data.responseText + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                    $.ajaxFileUpload({
                        <%--url: "${path}/video/headUpload",--%>
                        <%--type: "post",--%>
                        <%--data: {"id": data.responseText},--%>
                        <%--fileElementId: "videoPath", //文件选择框的id属性,即<input type="file">的id--%>
                        success: function () {
                            //上传成功 所作的事情
                            //刷新页面
                            $("#videoTable").trigger("reloadGrid");
                        }
                    });
                    return "hello";
                }
            },  //添加之后的额外操作
            {}   //删除之后的额外操作
        );
    }

<body>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                <form id="aa">
                    标题:<input type="text" name="title"><br>
                    描述:<input type="text" name="brief"><br>
                    视频:<input type="file" name="videoPath" id="vv"><br>
                    类别:<select id="cat" name="categoryId"></select>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="addVideo()">Save changes</button>
            </div>
            <div id="addOneClassOk" class="alert alert-success alert-dismissable"
                 style="display: none">添加成功
            </div>
        </div>
    </div>

</div>
<%--初始化一个面板--%>
<div class="panel panel-info">

    <%--面板头--%>
    <div class="panel panel-heading">
        <h2>视频信息</h2>
    </div>

    <%--选项卡--%>
    <div>
        <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#home">视频管理</a></li>
        </ul>
    </div>
    <br>

    <%--按钮组--%>
    <div>
        <button class="btn btn-info">导出视频</button>
        <button class="btn btn-success" data-toggle="modal" data-target="#myModal">添加</button>
    </div>
    <br>

    <%--表格--%>
    <table id="videoTable"/>

    <%--工具栏--%>
    <div id="videoPage"/>


</div>
</body>
  1. service层
@Override
    public Map<String,Object> queryAllByLimit(int rows, int page) {
        int start = (page - 1) * rows;
        List<Emp> list = empDao.queryAllByLimit(start, rows);
        int i = getTotal();
        System.out.println(i);
        int pageCount = i % rows == 0 ? i / rows : i / rows + 1;
        /*
         * 1. 计算起始下标
         * 2. 计算总页数
         *
         * page: 当前页
         * rows: 查询到的数据
         * total: 总页数
         * records: 总条数
         * */
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("rows", list);
        map.put("records", i);
        map.put("total", pageCount);

        return map;
    }
  1. controller层
	@RequestMapping("fenye")
    public Map<String ,Object> fenye(int rows, int page){
        return this.empService.queryAllByLimit(rows, page);
    }

    @RequestMapping("edit")
    @ResponseBody
    public String edit(Emp emp, String oper, String[] id) {
        String uid = null;
        if ("add".equals(oper)) {
            System.out.println("添加数据");
            Emp insert = empService.insert(emp);
            uid = insert.getId();
        } else if ("edit".equals(oper)) {
            System.out.println("修改数据");
            Emp update1 = empService.update(emp);

            uid = update1.getId();
        } else if ("del".equals(oper)) {
            empService.deleteById(emp.getId());
            System.out.println("删除数据");
        }
        return uid;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值