【jquery模仿net控件】简单分页控件1.0,附上gridview使用测试

简介:
前言

最近项目需求需要用到jquery的分页功能,所以就自己模仿着其它地方的写了一个,现在配合着原来写的gridview一起使用看看效果。

我们项目有个地方有点痛苦,他不能返回数据的总记录数,这个bug不修复我这边都只能动态附初始值,另外首页尾页等因为刚刚写起皆暂时未实现,

等后面点调整后,有必要便一起发出来。

截图







分页代码使用示例

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../css/pagination.css" rel="stylesheet" type="text/css" />
    <script src="../js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="../js/Pagination.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/03GridView/js/GridViewBase.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/03GridView/js/GridColum.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/03GridView/js/GridRow.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/03GridView/js/GridView.js" type="text/javascript"></script>
    <script type="text/javascript">
        //initGrid

        var gridDataBind = function (data) {
            var tb = $("#tb");
            var grid = new GridView();
            grid.style = {
                width: '70%'
            };
            grid.attribute = {
                className: 'infolist_hr'
            };
            grid.parentEl = tb;
            grid.dataSource = data;
            grid.colModel = [{
                'th': { 'title': '<input type="checkbox" class="th_select" />', 'attribute': { 'className': 'common'} },
                'td': { 'template': '<input type="checkbox" class="td_select" />', 'style': { 'width': '2%' }, 'attribute': { 'className': 'common indentten'} }
            }, {
                'th': { 'title': '标题', 'attribute': { 'className': 'common'} },
                'td': { 'template': '{%title%}', 'style': { 'width': '40%' }, 'attribute': { 'className': 'common indentten'} }
            }, {
                'th': { 'title': '来源', 'attribute': { 'className': 'common'} },
                'td': { 'template': '{%from%}', 'style': { 'width': '20%' }, 'attribute': { 'className': 'common indentten'} }
            }, {
                'th': { 'title': '时间', 'attribute': { 'className': 'common'} },
                'td': { 'template': '{%created%}', 'style': { 'width': '15%' }, 'attribute': { 'className': 'common indentten'} }
            }, {
                'th': { 'title': '', 'attribute': { 'className': 'common'} },
                'td': { 'template': '<span class="edit" style="cursor: pointer;" >编辑</span>&nbsp;&nbsp;<span class="delete" style="cursor: pointer;" >删除</span>', 'style': { 'width': '13%' }, 'attribute': { 'className': 'common indentten'} }
            }];

            grid.render();
        };
        var bind = function (start, limit) {
            if (!start) {
                start = 0;
            }
            if (!limit) {
                limit = 9;
            }
            var url = "http://113.142.30.139/tpw-webapp/rest/proxy/news_ln_statistics?jsonp=?&start=" + start + "&limit=" + limit;
            var tb = $("#tb");
            tb.html("数据正在加载......");
            $.getJSON(url, function (data) {
                tb.html("");
                if (data && typeof (data) == 'string') {
                    data = eval('(' + data + ')');
                } //if
                if (data.data) {
                    data = data.data;
                }

                gridDataBind(data);
               
            }); //$.getJSON(
        };
        var pageChanged = function (page) {
            var start = page.pageIndex * page.cfg.pageSize;
            var limit = page.cfg.pageSize;
            bind(start, limit);
            return false;
        };
        var initPage = function () {
            var page = $('#page');
            var url = "http://113.142.30.139/tpw-webapp/rest/proxy/news_ln_statistics";
            var page = new Pagination({
                parentEl: page,
                pageChanged: pageChanged
            });
            page.allRow = 100;
            page.render();
        };

        $(document).ready(function () {
            bind();
            initPage();
        });      //$(document)
    </script>
</head>
<body>
    <div id="tb">
    </div>
    <div id="page">
    </div>
</body>
</html>
复制代码
 

分页控件代码

复制代码
var Pagination = function (_cfg) {
    var sender = this;
    this.cfg = {
        parentEl: $('body'),
        pageSize: 10,
        pageNum: 7, //每页放几个显示的1,2,3,4
        pageEdge: 2,
        linkTo: '#',
        linkText: 'pageno',
        preText: '上一页',
        nextText: '下一页',
        ellipseText: "...",
        pageChaged: function () { return false; }
    };
    if (_cfg) {
        $.each(_cfg, function (key, value) {
            sender.cfg[key] = value;
        });
    }
    this.pageIndex = 0;
    this.allRow = 0;
    this.totalPage = 0;
    this.el = null;
    this.visible = false;
    this.prePage = null;
    this.nextPage = null;
    this.numPage = null;
};

Pagination.prototype.render = function () {
    this.onInit();
    this.preRender();
};

Pagination.prototype.preRender = function () {
    var scope = this;
    var prePage = this.prePage;
    var nextPage = this.nextPage;
    var numPage = this.numPage;
    var total = this.totalPage;
    var index = this.pageIndex;
    prePage.attr('class', 'prev');
    if (index == 0) {
        prePage.attr('class', 'current prev');
    }
    nextPage.attr('class', 'next');
    if (index == total - 1) {
        nextPage.attr('class', 'current next');
    }
    $.each(numPage, function (i, item) {
        item.removeAttr('class');
        if (scope.pageIndex == parseInt(item.html()) - 1) {
            item.attr('class', 'current');
        }
    });
};

Pagination.prototype.onInit = function () {
    this.init();
    this.initHtml();
    this.eventBind();
};

Pagination.prototype.init = function () {
    var cfg = this.cfg;
    var totalPage = this.totalPage;
    var allRow = this.allRow;
    var pageSize = cfg.pageSize;
    this.totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1;
    this.totalPage = parseInt(this.totalPage);
    if (totalPage <= 1) {
        this.visible = false;
    }
};

Pagination.prototype.getNumPage = function () {
    var cfg = this.cfg;
    var pageSize = cfg.pageSize;
    var index = this.pageIndex;
    var totalPage = this.totalPage;
    var pageNum = cfg.pageNum;
    var limit = pageNum / 2;
    var _limit = parseInt(limit);
    limit = limit > _limit ? _limit + 1 : _limit;
    var numPage = [];
    var numArr = [];
    for (var i = 0; i < pageNum; i++) {
        if (index < limit) {
            numArr.push(i + 1);
        } else if (totalPage - index <= limit) {
            numArr.push(totalPage - pageNum + i + 1);
        } else {
            numArr.push(index - limit + i + 2);
        }
    }
    var elStr = '';
    var linkTo = cfg.linkTo;
    if (linkTo == '#') {
        for (var i = 0, len = numArr.length; i < len; i++) {
            var tempEl = $('<a href="#">' + numArr[i].toString() + '</a>');
            numPage.push(tempEl)
        }
    } else {
        var linkText = cfg.linkText;
        var sign = '?';
        if (linkTo.indexOf('?') != -1) {
            sign = '&';
        }
        for (var i = 0, len = numArr.length; i < len; i++) {
            var tempEl = $('<a href="' + linkTo + sign + linkText + '=' + i.toString() + '">' + numArr[i].toString() + '</a>');
            numPage.push(tempEl);
        }
    }
    return numPage;
};

Pagination.prototype.initHtml = function () {
    var cfg = this.cfg;
    var pageInfo = $('<div class="pagination"></div>');
    var linkTo = cfg.linkTo;
    var _pre = '<a href="#" class="prev">上一页</a>';
    var _nex = '<a href="#" class="next">下一页</a>';
    if (linkTo != '#') {
        var linkText = cfg.linkText;
        var sign = '?';
        if (linkTo.indexOf('?') != -1) {
            sign = '&';
        }
        _pre = '<a href="' + linkTo + sign + linkText + '=' + (parseInt(this.pageIndex) - 1) + '" class="prev">上一页</a>';
        _nex = '<a href="' + linkTo + sign + linkText + '=' + (parseInt(this.pageIndex) + 1) + '" class="next">下一页</a>';

    }

    var prePage = $(_pre);
    var nextPage = $(_nex);
    var numPage = this.getNumPage();
    pageInfo.append(prePage);

    $.each(numPage, function (i, item) {
        pageInfo.append(item);
    });

    pageInfo.append(nextPage);
    this.el = pageInfo;
    this.prePage = prePage;
    this.nextPage = nextPage;
    this.numPage = numPage;
    cfg.parentEl.append(pageInfo);
};

Pagination.prototype.eventBind = function (func) {
    var scope = this;
    var cfg = this.cfg;
    var prePage = this.prePage;
    var nextPage = this.nextPage;
    var numPage = this.numPage;


    prePage.unbind('click');
    prePage.bind('click', function (e) {
        if (scope.pageIndex != 0) {
            scope.pageIndex = scope.pageIndex - 1;
            scope.pageChanged();
        }
    });
    $.each(numPage, function (i, item) {
        item.unbind('click');
        item.bind('click', function (e) {
            if (scope.pageIndex != parseInt(item.html()) - 1) {
                scope.pageIndex = parseInt(item.html()) - 1;
                scope.pageChanged();
            }
        });
    });

    nextPage.unbind('click');
    nextPage.bind('click', function (e) {
        if (scope.pageIndex != scope.totalPage - 1) {
            scope.pageIndex = scope.pageIndex + 1;
            scope.pageChanged();
        }
    });
};

Pagination.prototype.pageChanged = function () {
    var scope = this;
    var cfg = this.cfg;
    scope.el.remove();
    var pageChaged = cfg.pageChanged;
    if (pageChaged && typeof (pageChaged) == 'function') {
        pageChaged(this);
      
    }
    this.render();
    //    alert(this.pageIndex);
};
复制代码
 

后续

由于各方面皆不完整,此处便不作详细说明,有必要的话,以后整理后会形成API,但是可能没有必要,因为代码总的来说还是很水的......

在学习吧......




本文转自叶小钗博客园博客,原文链接:http://www.cnblogs.com/yexiaochai/archive/2012/06/07/2540994.html,如需转载请自行联系原作者
相关文章
|
开发框架 JavaScript 前端开发
震撼!破解 ASP.NET 服务器控件 Button 执行顺序之谜,颠覆你的开发认知!
【8月更文挑战第16天】在ASP.NET开发中,通过Button控件实现先执行JavaScript再触后台处理的需求十分常见。例如,在用户点击按钮前需前端验证或提示,确保操作无误后再传递数据至后台深度处理。此过程可通过设置Button的`OnClientClick`属性调用自定义JavaScript函数完成验证;若验证通过,则继续触发后台事件。此外,结合jQuery也能达到相同效果,利用`__doPostBack`手动触发服务器端事件。这种方式增强了应用的交互性和用户体验。
135 8
winform .net6 和 framework 的图表控件,为啥项目中不存在chart控件,该如何解决?
本文讨论了在基于.NET 6和.NET Framework的WinForms项目中添加图表控件的不同方法。由于.NET 6的WinForms项目默认不包含Chart控件,可以通过NuGet包管理器安装如ScottPlot等图表插件。而对于基于.NET Framework的WinForms项目,Chart控件是默认存在的,也可以通过NuGet安装额外的图表插件,例如LiveCharts。文中提供了通过NuGet添加图表控件的步骤和截图说明。
winform .net6 和 framework 的图表控件,为啥项目中不存在chart控件,该如何解决?
|
12月前
|
开发者 Windows
.NET 开源扁平化、美观的 C/S 控件库
【10月更文挑战第23天】介绍了三款适用于 .NET 平台的开源扁平化、美观的 C/S 控件库:MaterialSkin 采用 Google Material Design 风格,适合现代感界面;Krypton Toolkit 提供丰富控件,界面易于定制;Fluent Ribbon Control Suite 模仿 Office 界面,适合复杂功能应用。每款控件库均附有示例代码及 GitHub 链接。
462 0
|
12月前
|
C# Android开发 iOS开发
一组.NET MAUI绘制的开源控件 - AlohaKit
一组.NET MAUI绘制的开源控件 - AlohaKit
216 0
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
410 0
|
JavaScript 前端开发 测试技术
《手把手教你》系列技巧篇(三十八)-java+ selenium自动化测试-日历时间控件-下篇(详解教程)
【5月更文挑战第2天】在自动化测试过程中,经常会遇到处理日期控件的点击问题。宏哥之前分享过一种方法,但如果输入框是`readonly`属性,这种方法就无法奏效了。不过,通过修改元素属性,依然可以实现自动化填写日期。首先,定位到日期输入框并移除`readonly`属性,然后使用`sendKeys`方法输入日期。这样,即使输入框设置了`readonly`,也能成功处理日期控件。
369 1
|
Java 测试技术 Python
《手把手教你》系列技巧篇(三十七)-java+ selenium自动化测试-日历时间控件-上篇(详解教程)
【5月更文挑战第1天】该文介绍了使用Selenium自动化测试网页日历控件的方法。首先,文章提到在某些Web应用中,日历控件常用于选择日期并筛选数据。接着,它提供了两个实现思路:一是将日历视为文本输入框,直接输入日期;二是模拟用户交互,逐步选择日期。文中给出了JQueryUI网站的一个示例,并展示了对应的Java代码实现,包括点击日历、选择日期等操作。
301 0
|
JavaScript 前端开发
采用jquery来分页,排序,查询
页面标签全面采用jquery来分页,排序,查询   又花了半天的时间将标签库中中的分页,排序和查询功能通过jquery来实现,充分利用jquery的ajax特性,实现页面中内容的局部刷新和分页及查询操作,jquery太酷了。呵呵
679 0
|
7月前
|
JavaScript 前端开发
百叶窗效果的jQuery幻灯片插件
百叶窗效果的jQuery幻灯片插件

热门文章

最新文章