首页 新闻 搜索 专区 学院

Mvc怎么实现翻页?

0
悬赏园豆:10 [已解决问题] 解决于 2014-04-11 11:01

public ActionResult Index()
{

OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
conn.Open();
DataTable datatable = new DataTable();
OleDbCommand comm = new OleDbCommand("select * from UserList",conn);
OleDbDataAdapter dar = new OleDbDataAdapter(comm);
dar.Fill(datatable);
conn.Close();
conn.Dispose();
///////////////////////////
List<Model.UserList> list_m = new List<UserList>();
list_m = (List<Model.UserList>)Model.ModelConvertHelper<Model.UserList>.ConvertToModel(datatable);
PagedList<UserList> list_m_P =这个地方不知道怎么写,因为数据源是list<T>,不是linq出来的
return View(list_m_P);
}

jason2013的主页 jason2013 | 小虾三级 | 园豆:1998
提问于:2014-04-10 21:16
< >
分享
最佳答案
0
/**************************************************************************************************************/
/***************************************************分页插件*************************************************/
/*
*参数说明
*options.recordCount 总数
*options.pageSize  每页大小
*options.pagenumber  当前页数
*options.customerText 是否自定义文本,默认不显示
*options.buttonClickCallback 分页的回调函数
* 用法 :$("#mypager").pager({ pagenumber: pageclickednumber, recordCount: recordCount, pageSize: pageSize,cKey:value, buttonClickCallback: PageClick})
*/
; (function ($) {
    $.fn.pager = function (options) {
        var opts = $.extend({}, $.fn.pager.defaults, options);
        var pagecount = GetPageCount(parseInt(opts.recordCount), parseInt(opts.pageSize));
        opts.pagecount = pagecount;
        $(this).empty();
        $(this).append(renderpager(parseInt(opts.pagenumber), pagecount, parseInt(opts.pageSize), parseInt(opts.recordCount), opts.customerText, opts.buttonClickCallback));

        if (opts.buttonClickCallback != undefined && opts.buttonClickCallback != null) {
            $(".pager_items").children("li").click(function () {
                var data = $(this).attr("data");
                if (data > 0) {
                    opts.buttonClickCallback(data, opts.pageSize);
                }
            });

            $(".pageselectpage").change(function () {
                var size = $(this).val();
                opts.buttonClickCallback(1, size);
            });
        }
    };

    //获得总页数
    function GetPageCount(recordCount, pageSize) {
        if (recordCount <= 0) return 1;
        if (recordCount % pageSize == 0)
            return parseInt(recordCount / pageSize);
        else
            return parseInt(recordCount / pageSize) + 1;
    }
    //pagenumber
    function renderpager(pagenumber, pagecount, pageSize, recordCount, customerText, buttonClickCallback) {
        var items = [1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
        var $pager = "";
        $pager += "<div class=\"paget\">";
        //$pager += "<div class=\"dataTables_info\">当前页 " + pagenumber + " / " + pagecount + "&nbsp; 共 " + recordCount + " 行 ,每页";
        $pager += "<div class=\"dataTables_info\">共 " + recordCount + " 行 ,每页";
        $pager += "<select class=\"input-small pageselectpage\" style='width:60px;'>";

        for (var i = 0; i < items.length; i++) {
            if (items[i] == pageSize) {
                $pager += "<option selected=\"selected\" value=\"" + items[i] + "\">" + items[i] + "</option>";
            } else {
                $pager += "<option value=\"" + items[i] + "\">" + items[i] + "</option>";
            }
        }
        $pager += "</select>行</div>";
        $pager += "</div>";
        $pager += "<div class=\"pagen\">";
        $pager += "<div class=\"dataTables_paginate paging_bootstrap pagination\">";
        $pager += "<ul class='pager_items'>";

        if (pagenumber <= 1) {
            $pager += "<li class=\"prev disabled\" data=\"1\"><a href=\"javascript:void(0)\">首页</a></li>";
        } else {
            $pager += "<li class=\"prev\" data=\"1\"><a href=\"javascript:void(0)\">首页</a></li>";
        }

        var start = pagenumber - 3;
        start = start <= 1 ? 1 : start;
        if (start > 1) {
            $pager += "<li data=\"0\"><a href=\"javascript:void(0)\">...</a></li>";
        }
        for (var i = start; i < pagenumber; i++) {
            $pager += "<li data=\"" + i + "\"><a href=\"javascript:void(0)\">" + i + "</a></li>";
        }
        $pager += "<li class=\"active\" data=\"" + pagenumber + "\"><a href=\"javascript:void(0)\">" + pagenumber + "</a></li>";

        var end = pagenumber + 3;
        end = end >= pagecount ? pagecount : end;

        for (var i = pagenumber + 1; i <= end; i++) {
            $pager += "<li data=\"" + i + "\"><a href=\"javascript:void(0)\">" + i + "</a></li>";
        }
        if (pagecount > end) {
            $pager += "<li data=\"0\"><a href=\"javascript:void(0)\">...</a></li>";
        }

        if (pagenumber >= pagecount) {
            $pager += "<li class=\"next disabled\" data=\"" + pagecount + "\"><a href=\"javascript:void(0)\">尾页</a></li>";
        } else {
            $pager += "<li class=\"next\" data=\"" + pagecount + "\"><a href=\"javascript:void(0)\">尾页</a></li>";
        }
        $pager += "</ul>";
        $pager += "</div>";
        $pager += "</div>";
        return $pager;
    }


    function GetPageNum() {
        alert("请输入要跳转的页码!");
    }

    $.fn.pager.defaults = {
        pagenumber: 1,
        pagecount: 1,
        recordCount: 100,
        pageSize: 10
    };
})(jQuery);
收获园豆:2
贺臣 | 初学一级 |园豆:107 | 2014-04-10 21:48

public ActionResult Index()
{

OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
conn.Open();
DataTable datatable = new DataTable();
OleDbCommand comm = new OleDbCommand("select * from UserList",conn);
OleDbDataAdapter dar = new OleDbDataAdapter(comm);
dar.Fill(datatable);
conn.Close();
conn.Dispose();
///////////////////////////
List<Model.UserList> list_m = new List<UserList>();
list_m = (List<Model.UserList>)Model.ModelConvertHelper<Model.UserList>.ConvertToModel(datatable);
PagedList<UserList> list_m_P =这个地方不知道怎么写,因为数据源是list<T>,不是linq出来的
return View(list_m_P);
}

jason2013 | 园豆:1998 (小虾三级) | 2014-04-11 10:28
其他回答(4)
0

翻页和MVC有关系吗?

收获园豆:2
爱编程的大叔 | 园豆:30698 (高人七级) | 2014-04-10 21:33
0

翻页和你用什么Web框架没有直接关系,这里估计你关心的无非是两点:分页的数据和前端的展示:

1、分页的数据提取,如果你自己写的话,用ASP.NET MVC默认搭配的EF,查询时lambda表达式的Skip和Take两个方法搭配能够满足基本需要,自己写SQL或者其他ORM原理一致。推荐搜索下“MVC分页控件”,有很多网友开发的服务端控件可用。

2、页面上分页导航的展示部分,上述服务端分页控件一般都提供该功能。当然,有了数据源,也可以自己写,也有不少JS的插件可以选择。

可参考:

http://www.webdiyer.com/mvcpager/

http://www.cnblogs.com/longgel/archive/2010/05/03/1726415.html

http://www.cnblogs.com/lhb25/archive/2012/08/08/ajax-jquery-pagination-plugin-tutorial.html

收获园豆:2
李宝亨 | 园豆:1433 (小虾三级) | 2014-04-10 21:56
0
收获园豆:2
单恋 | 园豆:678 (小虾三级) | 2014-04-11 09:26
0

我是自己写了一个方法来输出页码标签

收获园豆:2
吴瑞祥 | 园豆:28841 (高人七级) | 2014-04-11 10:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册