简单三层架构mvc+datagrid实现分页排序

在vs中采用简单三层架构搭建

之前也在网上找了很多关于datagrid分页排序的例子,感觉都不太好用,自己就动手写了一个,采用简单地三层架构,下面是详细代码。

首先是做出来的页面效果:

 可以进行分页排序,分页的效果是在排序后重新实现的,这个可以根据项目需要进行更改。

首先是html页面:先放一个datagrid表格

 <div data-options="region:'center',split:true" border="false">
        <table id="datagrid">

        </table>

</div>

使用jq对datagrid进行设置,需要引入一些js和easyui包。

 $(function () {
        $('#datagrid').datagrid({
            url: 'StudentInfo.ashx',
            title: '',
            iconCls: 'icon-save',//图标
            pagination: true,//分页
            pageSize: 10,//每页显示条数
            pageList: [10, 20, 30, 40],//调整每页显示条数
            fit: true,//宽高跟随父面板
            fitColumns: false,//水平滚动条
            nowrap: false,//折行显示
            border: false,
            idField: 'id',//标识
            sortName: 'name',
            sortOrder: 'desc',
            columns: [[
                {
                    title: '姓名',
                    field: 'name',
                    width: 100,
                    sortable: true
                },
                {
                    title: '性别',
                    field: 'sex',
                    width: 100,
                    sortable: true
                },
                {
                    title: '年龄',
                    field: 'age',
                    width: 100,
                    sortable: true
                },
               {
                   title: '出生日期',
                   field: 'birthday',
                   width: 300,
                   sortable: true
                   
               },
                {
                    title: '地址',
                    field: 'adress',
                    width: 200,
                    sortable: true
                },
                {
                    title: '电话',
                    field: 'telphone',
                    width: 200,
                    sortable: true
                },
                {
                    title: '邮箱',
                    field: 'email',
                    width: 200,
                    sortable: true
                }

            ]],

//分页的参数会自动post到URL地址页面,排序的参数需要使用函数进行提交
            onSortColum: function (sort, order) {
                $.post({
                    sort: sort,
                    order: order
                })
            }

    });

下面是一般处理程序页面:

 int page =int.Parse(context.Request["page"]);

//采用int.Parse将接收到分页参数转换为int类型
        int rows =int.Parse(context.Request["rows"]);
        string sortname = context.Request["sort"];
        string sortorder = context.Request["order"];

//调用BLL层,传入分页和排序参数
        StudentInfoBLL bll = new StudentInfoBLL();
        context.Response.Write(bll.GetStudentInfo(page,rows,sortname,sortorder));

下面是BLL层:

//调用DAL层的方法

 StudentInfoDAL dal = new StudentInfoDAL();

        public string GetStudentInfo(int page,int rows,string sortname, string sortorder,string name)
        {

//传入DAL层的参数为分页参数和排序参数,返回来的是list对象和数据总行数,需要对list对象转换
            Comlist cl = new Comlist();
            cl.rows = dal.GetStudentInfo(page, rows, sortname, sortorder,name);
            cl.total = dal.GetStudentCount();
            return JsonConvert.SerializeObject(cl);
        }

接下来是DAL层:

 public List<StudentInfo> GetStudentInfo(int page, int rows, string sortname, string sortorder,string name)
        {

//分页排序sql语句,此处是先进行排序,在进行分页,采用row_number方式
            string sql = "SELECT * FROM (select top " + (page * rows) + " ROW_NUMBER() OVER ( ORDER BY " + sortname + "  " + sortorder + " ) AS rownum ,* FROM  StudentInfo) AS temp WHERE temp.rownum > (" + rows * (page - 1) + ") order by   " + sortname + "   " + sortorder;

//此处是连接数据库,进行查询,返回datatable对象
            DataTable dt = SQLHelper.ExecuteTable(sql);
            List<StudentInfo> si = new List<StudentInfo>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                si.Add(RowToStudentInfo(dt.Rows[i]));
            }
            
            return si;
        }

public int GetStudentCount()
        {

//此处返回的是数据库记录的总行数,即total
            string sql1 = "SELECT count(*) FROM StudentInfo";
            DataTable dt = SQLHelper.ExecuteTable(sql1);
            return int.Parse(dt.Rows[0][0].ToString());
        }

将关系转换成对象就不在此叙述了。

接下来是BLL层中调用了comlist方法的介绍,如果不调用comlist方法,返回的值中包含一个list对象(有多条数据)和一个total总行数,现在需要将两个同时返回,本处采用的是comlist返回,当然也有其他返回方法,高手可以去试试。

下面将comlist介绍一下:

 public class Comlist
    {
        public int total { get; set; }
        public List<StudentInfo> rows { get; set; }
    }
}

comlist包含两个字段,total和rows   注意:此处名字必须是total和rows  否则前段的datagrid接收不到。

猜你喜欢

转载自blog.csdn.net/tianzhen620/article/details/81388315