Blame view

build/Pagination/Pagination.js 4.37 KB
3a3ecabe   Imshann   init
1
  import template from "./Pagination.html";
dd962f77   Imshann   优化
2
3
  import style from "antd/lib/pagination/style/index.css";
  angular.module("esNgAntd").directive("esPagination", function (esNgAntd) {
3a3ecabe   Imshann   init
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      return {
          controllerAs: "esPagination",
          restrict: "E",
          transclude: true,
          replace: true,
          scope: {
              defaultCurrent: "@",
              current: "@",
              total: "@",
              defaultPageSize: "@",
              pageSize: "@",
              onChange: "&",
              onShowSizeChange: "&",
              showQuickJumper: "@",
              showSizeChanger: "@",
          },
          template: template,
          controller: function ($scope, $element) {
              this.getContext = function () {
                  return $scope;
              };
  
              $scope.state = {
                  total: null,
                  current: null,
                  pageSize: null,
                  pageNum: null,
                  pageNumList: null,
              };
  
              $scope.getPageNum = function () {
                  return (
                      Math.ceil(
                          $scope.state.total /
                              ($scope.pageSize || $scope.defaultPageSize || 10)
                      ) || 1
                  );
              };
  
              $scope.handleNext = function () {
                  if ($scope.state.current === $scope.state.pageNum) {
                      return false;
                  }
  
                  $scope.handleClick(++$scope.state.current);
              };
  
              $scope.getPopupContainer = function () {
                  return $element[0].querySelector(".ant-pagination-options");
              };
  
              $scope.handlePrev = function () {
                  if ($scope.state.current === 1) {
                      return false;
                  }
  
                  $scope.handleClick(--$scope.state.current);
              };
  
              $scope.handleClick = function (value) {
                  $scope.state.current = value; // 更新回调
  
                  $scope.onChange({
                      page: $scope.state.current,
                      pageSize: $scope.state.pageSize,
                  });
              };
  
              $scope.handleChange = function () {
                  let current = $scope.state.current;
  
                  if ($scope.state.current > $scope.state.pageNum) {
                      current = $scope.state.pageNum;
                  } else if ($scope.state.current < 1) {
                      current = 1;
                  }
  
                  $scope.onChange({
                      page: current,
                      pageSize: $scope.state.pageSize,
                  });
              };
  
              $scope.handleSelectChange = function (value) {
                  $scope.state.current = 1;
                  $scope.state.pageSize = parseInt(value);
                  $scope.handleChange();
              };
  
              $scope.getCurrent = function (number) {
                  if (number > $scope.state.pageNum) {
                      return $scope.state.pageNum;
                  }
  
                  if (number < 1) {
                      return 1;
                  }
  
                  return parseInt(number);
              };
  
1b6f912f   Imshann   优化
105
              $scope.setCurrent = function (value) {
3a3ecabe   Imshann   init
106
107
108
109
110
111
                  if (!value) {
                      return;
                  }
  
                  $scope.state.current = $scope.getCurrent(value);
                  $scope.handleChange();
1b6f912f   Imshann   优化
112
113
114
115
              };
  
              $scope.handleBlur = function (event) {
                  $scope.setCurrent(event.target.value);
3a3ecabe   Imshann   init
116
117
                  event.target.value = null;
              };
1b6f912f   Imshann   优化
118
119
120
121
122
123
124
  
              $scope.onKeyPress = function (event) {
                  if (event.keyCode === 13 || event.keyCode === 32) {
                      $scope.setCurrent(event.target.value);
                      event.target.value = null;
                  }
              };
3a3ecabe   Imshann   init
125
126
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
dd962f77   Imshann   优化
127
              esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
128
129
130
131
132
133
134
135
136
137
138
139
140
              $scope.state.total = Number($scope.total || 0);
              $scope.state.current = Number(
                  $scope.current || $scope.defaultCurrent || 1
              );
              $scope.state.pageSize =
                  $scope.pageSize || $scope.defaultPageSize || 10;
              $scope.state.pageNum = $scope.getPageNum();
              $scope.state.pageNumList = Array($scope.state.pageNum)
                  .fill(0)
                  .map((v, i) => i + 1);
          },
      };
  });