D
G
O
L

html&&test

<!DOCTYPE html>
<html>
<head>
    <title>商品管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-table.min.css"rel="stylesheet" >
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table-zh-CN.min.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title text-center">商品管理</h3>
    </div>
    <div class="panel-body">

        <div id="toolbar" class="btn-group">
            <button id="btn_save" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            <button id="btn_edit" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
            </button>
            <button id="btn_delete" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
            </button>
            <button id="btn_buy" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>加入购物车
            </button>
            <button id="btn_shopcart" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>查看购物车
            </button>
        </div>

        <table data-toggle="table" id="table" data-height="400"
               data-classes="table table-hover" data-striped="true"
               data-pagination="false" data-side-pagination="server"
               data-show-refresh="true" data-show-toggle="true"
               data-show-columns="true" data-toolbar="#toolbar">
            <thead>
            <tr>
                <th data-field="state" data-checkbox='ture'></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<!-- 添加商品模态框(Modal) -->
<div class="modal fade" id="saveModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">添加商品信息</h4>
            </div>
            <div class="modal-body">
                <form id="productForm">
                    <input type="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label>商品名称</label>
                        <input type="text" class="form-control" id="productName" name="productName">
                    </div>
                    <div class="form-group">
                        <label>价格</label>
                        <input type="text" class="form-control" id="price" name="price">
                    </div>
                    <div class="form-group">
                        <label>库存数量</label>
                        <input type="number" class="form-control" id="count" name="count">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="saveConfirmBtn">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
</body>
<script type="text/javascript">
    $(document).ready(function() {
        $("button[name='toggle']").height(20);
        $("button[name='refresh']").height(20);
        $("#saveModal").modal('hide');
    });

    //添加按钮点击事件
    $("#btn_save").click(function() {
        $("#saveModal").modal('show');
        $("#id").val("");
        $("#productName").val("");
        $("#price").val("");
        $("#count").val("");
    });

    //编辑按钮点击事件
    $("#btn_edit").click(function edit() {
        var list = $("#table").bootstrapTable('getSelections');
        if (list == null || list.length <= 0) {
            alert("未选中任何项!");
            return;
        }
        else if (list.length > 1) {
            alert("请仅选择一条数据!");
            return;
        }

        var id = list[0].id;
        $("#saveModal").modal('show');
        $.ajax({
            url : 'product/queryProductById',
            data : {
                'id' : id
            },
            success : function(ret) {
                $("#id").val(ret.id);
                $("#productName").val(ret.productName);
                $("#price").val(ret.price);
                $("#count").val(ret.count);
            }
        });
    });

    //添加用户模态框下的确认按钮点击事件
    $("#saveConfirmBtn").click(function() {
        $("#saveModal").modal('hide');
        $.ajax({
            url : "product/save",
            method : 'post',
            dataType:'json',
            data : $("#productForm").serialize(),
            success : function() {
                alert("操作成功!");
                //重新加载表格
                $("#table").bootstrapTable("refresh");
            }
        });
    });

    //删除按钮点击事件(可以批量删除)
    $("#btn_delete").click(function() {
        var list = $("table").bootstrapTable('getSelections');
        if (list == null || list.length <= 0) {
            alert("未选中任何项!");
            return;
        }

        var idList = new Array();
        for (var i = 0; i < list.length; i++) {
            idList[i] = list[i].id;
        }
        if (confirm("是否删除选中的数据?")) {
            $.ajax({
                url : "product/deleteByList",
                type : 'post',
                dataType : 'json',
                data : {
                    idList : idList
                },
                success : function(data) {
                    alert("删除成功!");
                    //重新加载表格
                    $("#table").bootstrapTable("refresh");
                }
            });
        }
    });

    //添加到购物车按钮点击事件
    $("#btn_buy").click(function() {
        var list = $("table").bootstrapTable('getSelections');
        if (list == null || list.length <= 0) {
            alert("未选中任何项!");
            return;
        }

        var idList = new Array();
        for (var i = 0; i < list.length; i++) {
            idList[i] = list[i].id;
        }
        if (confirm("是否确认加入购物车?")) {
            $.ajax({
                url : "shopcart/add",
                type : 'post',
                dataType : 'json',
                data : {
                    idList : idList
                },
                success : function(data) {
                    alert("商品已添加到购物车");
                }
            });
        }
    });

    $("#btn_shopcart").click(function() {
        window.location.href="shopcart.html"
    });

    $("#table").bootstrapTable({
        url : "product/list",
        clickToSelect : true,
        dataType : "json",
        contentType : "application/x-www-form-urlencoded;charset=utf-8",
        method : 'get',
        pagination:true,
        pageNumber:1,
        pageSize:5,
        queryParams:function(params){
            return {
                pageSize:params.limit,
                pageNumber:(params.offset/params.limit)
            }
        },
        responseHandler : function(res) {
            return res;
        },
        columns : [
            {
                field : 'state',
            },
            {
                field : 'id',
                title : 'ID',
                align : 'center'
            },
            {
                field : 'productName',
                title : '产品名称',
                align : 'center'
            },
            {
                field : 'price',
                title : '产品价格',
                align : 'center'
            },
            {
                field : 'count',
                title : '库存数量',
                align : 'center'
            }]
    });
</script>
</html>

posted @ 2023-02-09 15:03  jinganglang567  阅读(28)  评论(0)    收藏  举报