使用Bootstrap-Table实现分页

时间:2025-03-19 07:04:16

@model IEnumerable<>
@using
@{
    = "ProductAll";
}

<h2>商品列表</h2>

<table class="table">

</table>

@section scripts{
    <script type="text/javascript">
        $(function () {

            $(".table").bootstrapTable({
                url: "@("GetProductAll", "Home")",                     //请求后台的URL(*)
                method: 'GET',                      //请求方式(*)
                //toolbar: '#toolbar',              //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
                pagination: true,                   //是否显示分页(*)
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                sidePagination: "server",           //分页方式:client客户端分页,server服务端分页(*)
                pageNumber: 1,                      //初始化加载第一页,默认第一页,并记录
                pageSize: rows,                     //每页的记录行数(*)
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数(*)
                search: false,                      //是否显示表格搜索
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列(选择显示的列)
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                //height: 500,                      //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
                uniqueId: "ID",                     //每一行的唯一标识,一般为主键列
                showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                  //是否显示父子表
                columns: [{
                    field: "ProductID",
                    title: "编号"
                },
               {
                    field: 'ProductName',
                    title: '姓名'
                },
               {
                    field: 'IsUp',
                    title: '是否上架'
                },
               {
                    field: 'UnitPrice',
                    title: '价格'
                },
               {
                    field: 'Remark',
                    title: '备注'
                },
               {
                    field: 'CategoryName',
                    title: '类别'
                },
                {
                    title: '操作',
                    formatter: function (value, row, Index)
                    {
                        return "<a href='javascript:;' class='btn btn-danger' data-del='" + + "'>删除</a>"+
                        "<a href='/Home/ProductEdit?ProductID=" + +"' class='btn btn-warning'>修改</a>"

                    }
                }]
            });
        })

        $(document).on("click", "a[data-del]", function () {
            var $this = $(this);
            var id = $("data-del");
            if (confirm("你确定要删除" + id + "吗?")) {
                $.post("@("Delete","Home")", { id: id }, function (data) {
                    if ( == 1) {
                        alert();

                        $(".table").bootstrapTable("refresh");

                    } else {
                        alert();
                    }

                }, "json");
            }
        })
    </script>
}