angular+bootstrap分页指令案例

AngularJS中不仅内置了许多指令,而且开发人员也可以通过自定义指令来完成特殊操作,指令创建成功后可以到处复用。

Web应用中的分页处理最为常见,我们可以将分页模块编写成一个可以复用的Angular指令,在使用时无需考虑指令的内部实现细节,像使用普通HTML元素一样简单。

1:index.html

<!DOCTYPE html>

<html ng-app="myApp">
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/bootstrap.css"/>
    </head>
    <body>

        <div class="container" ng-controller="PageCtrl">
            <pagination num-pages="pages" curr-page="page" on-select-page="selectPage(page)"></pagination>
        </div>

    </body>

    <script src="../js/angular.js"></script>
    <script src="index.js"></script>
</html>

分页指令的使用如下:

<pagination num-pages="pages" curr-page="page" on-select-page="selectPage(page)"></pagination>

index.js

var myApp = angular.module(‘myApp‘, []);

myApp.controller(‘PageCtrl‘,[‘$scope‘, function($scope){
        $scope.pages = 10;
        $scope.page = 1;

        $scope.selectPage = function(page){
          console.log("选择的页:"+page);
        };
}]);

myApp.directive(‘pagination‘, function(){
    return {
        restrict: ‘E‘,
        scope: {
            numPages: ‘=‘,
            currPage: ‘=‘,
            onSelectPage: ‘&‘
        },

        template: ‘<ul class="pagination">‘
            +‘<li ng-class="{disabled: noPreviousPage()}">‘
            +‘<a ng-click="selectPreviousPage()">上一页</a>‘
            +‘</li>‘

            +‘<li ng-repeat="page in pages" ng-class="{active: isActivePage(page)}">‘
            +‘<a ng-click="selectPage(page)">{{page}}</a>‘
            +‘</li>‘

            +‘<li ng-class="{disabled: noNextPage()}">‘
            +‘<a ng-click="selectNextPage()">下一页</a>‘
            +‘</li>‘

            +‘</ul>‘,

        replace: true,
        link: function(s){
            s.$watch(‘numPages‘, function(value){
                s.pages = [];

                for(var i=1;i<=value;i++){
                    s.pages.push(i);
                }

                if(s.currPage > value){
                    s.selectPage(value);
                }
            });

            //判读是否有上一页
            s.noPreviousPage = function(){
                return s.currPage === 1;
            };

            //判断是否有下一页
            s.noNextPage = function(){
                return s.currPage === s.numPages;
            };

            //判断当前页是否被选中
            s.isActivePage = function(page){
                return s.currPage===page;
            };

            //选择页数
            s.selectPage = function(page){
                if(!s.isActivePage(page)){
                    s.currPage = page;

                    s.onSelectPage({ page:page });
                }
            };

            //选择下一页
            s.selectNextPage = function(){
                if(!s.noNextPage()){
                    s.selectPage(s.currPage+1);
                }
            };

            //选择上一页
            s.selectPreviousPage = function(){
                if(!s.noPreviousPage()){
                    s.selectPage(s.currPage-1);
                }
            };
        }
    };
});

3:运行效果如下:

时间: 2024-08-06 21:43:51

angular+bootstrap分页指令案例的相关文章

angular封装分页组件

1)分页模板页面product_pag.html <div style="margin-right:4px;float:right;"> <span style="color: blue; margin-right: 6px;">总 页 数 <strong class="colorred">{{pageObj.totalPage}}</strong> </span> <span s

嘿!@野兽,你的ng api 掉了 - - angular.bootstrap

@野兽的 ng api 学习 -- angular.bootstrap angular.bootstrap 使用这个功能来手动启动angular应用.基于ngScenario的端对端测试不能使用bootstrap手动启动,需要使用ngApp. Angular会检测应用在浏览器是否已启动并且只允许第一次的启动,随后的每次启动都将会导致浏览器控制台报错. 这可以防止应用出现多个Angular实例尝试在Dom上运行的一些奇异结果. 格式:angular.bootstrap(element,[nodul

(六)使用angular.bootstrap完成模块的手动加载

之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <head> <script src="angular.js"></script> <script> // 创建moudle1 var rootMoudle = angular.module('moudle1', []); rootMoudle.cont

使用angular.bootstrap() 完成模块的手动加载

之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <head> <script src="angular.js"></script> <script> // 创建moudle1 var rootMoudle = angular.module('moudle1', []); rootMoudle.cont

angular.js分页代码的实例

对于大多数web应用来说显示项目列表是一种很常见的任务.通常情况下,我们的数据会比较多,无法很好地显示在单个页面中.在这种情况下,我们需要把数据以页的方式来展示,同时带有转到上一页和下一页的功能.现在在学习angular,使用angularjs 分页,基于 directive 实现,样式使用的 bootstrap,直接在 html代码中加入 标签即可调用. 先来看下效果图 实例代码 1 app.directive('pagePagination', function(){ 2 return {

SQL基础分页存储过程(案例一)

1 --分页 存储过程 案例 2 3 -- 所执行的存储过程 4 create proc pageForUsers 5 @currPage int, --当前页数 6 @pageSize int, --每页多少条记录 7 @count int output --总记录数 8 as 9 declare @firstIndex int 10 declare @lastIndex int 11 declare @sqlText varchar(200) 12 13 --统计总记录数 14 select

Angular 2 属性指令 vs 结构指令

Angular 2 的指令有以下三种: 组件(Component directive):用于构建UI组件,继承于 Directive 类 属性指令(Attribute directive):  用于改变组件的外观或行为 结构指令(Structural directive):  用于动态添加或删除DOM元素来改变DOM布局 组件 import { Component } from '@angular/core'; @Component({       selector: 'my-app', // 

angular的自定义指令---详解

1.angualr指令 在angualr自己里面有许多丰富的指令,但都是平时所常见的,但对于自己所需要的可能有所欠缺,所以自己可能会摒弃原声指令,自己封装更为健壮灵活的指令: 其实angular里面的指令也是基于下面所用到的步骤来创建的,只不过他都添加到了全局中,所以可以直接使用: 2.创建自定义指令 首先创建模块app,再使用app的服务directive方法, 创建指令内容第一个参数自定义的名字,第二个参数是自定义参数属性对象,该对象包括的属性基本在代码注释解释清楚: // a.创建模块 v

bootstrap分页及弹框

一.bootstrap分页 1,js内容 var pageitem = 20;//每页显示数 //是否是第一次加载js var isfirstload = true; function initPagination(count){ var num_entries = 0;//总数 if(null== count){ num_entries = $("#count").val(); if(null== num_entries){ num_entries = 0; }else{ total