沙世明
2022-09-13 bfca7a84bec815da594f1d12558535ed06d2490b
1
2
3
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { LightningElement, track, api } from 'lwc';
 
export default class JzDataTablePaging extends LightningElement {
    @api name;
    @api options = {};
 
    connectedCallback(){
        this.initJzDataTablePaging(this.options);
    }
 
    @track
    config = {
        // 单页是否显示
        singlePageHide: true,
        // 总页数
        pageNum: 0,
        // 上一页Class
        prevClass: '',
        // 下一页Class
        nextClass: '',
        // 首页Class
        homeClass: '',
        // 尾页Class
        lastClass: '',
        // ul Class 根据类型不同显示不同样式
        ulClass: '',
        // 页码列表 {dataIndex:页码  pagesLiClass:页码所在对样式}
        pagesList: [],
    }
 
    @track
    pagingConfig = {
        // 容器名
        elementName: '',
        // 样式类型  1或2
        type: 1,
        // 当前页
        pageIndex: 1,
        // 每页显示数量
        pageSize: 0,
        // 总条数
        total: 0,
        // 按钮数量
        pageCount: 9,
        // 上一页文字
        prevText: '',
        // 下一页文字
        nextText: '',
        // 首页文字
        homeText: '',
        // 尾页文字
        lastText: '',
        // 输入框跳转
        jumper: false,
        // 单页隐藏
        singlePageHide: true,
        // 是否禁用
        disabled: false,
        /**
         * @description 按钮事件回调
         * @param index [number] 当前页码
         */
        currentChange: function (index) { },
    };
 
    ceshi(){
        let options01 = {
            elementName: 'pages',
            type: 1,
            pageIndex: 1,
            pageSize: 10,
            total: 100,
            pageCount: 5,
            jumper: true,
            singlePageHide: false,
            prevText: '上一页',
            nextText: '下一页',
            // 首页文字
            homeText: '首页',
            // 尾页文字
            lastText: '尾页',
            disabled: true,
            currentChange: function(index) {
                // console.log(index);
            }
        }
        this.initJzDataTablePaging(options01);
    }
 
    @api
    initJzDataTablePaging(options) {
        this.pagingConfig = {...this.pagingConfig, ...options};
        if (this.validate(this.pagingConfig)) {
            this.render();
        }
    };
 
    @api
    get getPage() {
        return {
            currentPage: this.pagingConfig.pageIndex,
            total: this.pagingConfig.total,
            pageSize: this.pagingConfig.pageSize
        };
    }
 
    // 提供初始化
    render = function () {
        // 总页数
        this.config.pageNum = Math.ceil(this.pagingConfig.total / this.pagingConfig.pageSize);
        // 单页隐藏
        if (this.config.pageNum === 1 && this.pagingConfig.singlePageHide){
            this.config.singlePageHide = true;
            return;
        } else {
            this.config.singlePageHide = false;
        }
        this.config.ulClass = "_pages _pages_" + this.pagingConfig.type;
        // 刷新分页控件内容
        this.refreshPaging(this.pagingConfig.pageIndex);
    };
 
    // 刷新分页控件内容
    refreshPaging(index){
        // 最大页码
        if (index > this.config.pageNum)
            index = this.config.pageNum;
        // 最小页码
        if (index <= 0)
            index = 1;
 
        this.pagingConfig.pageIndex = index;
        
        // 禁用上一页
        var prev_disabled = this.pagingConfig.pageIndex <= 1 ? ' _disabled_c ' : '';
        // 手势禁止
        if (this.pagingConfig.pageIndex <= 1 && this.pagingConfig.disabled)
            prev_disabled += ' _disabled ';
        // 首页样式
        if (this.pagingConfig.type <= 1) {
            this.config.homeClass = '_home' + prev_disabled;
        }
        // 上一页
        this.config.prevClass = '_prev_ ' + prev_disabled + (this.pagingConfig.prevText ? ' _prev' : '_iconfont iconzuo');
        // 区间值
        let between = this.getBetween();
        let pagesList = [];
        let pagesLi = {};
        let active;
        for (var i = 1; i <= this.config.pageNum; i++) {
            if (i >= between.min && i <= between.max) {
                active = "_pages_li_" + this.pagingConfig.type;
                active += i === this.pagingConfig.pageIndex ? " _active_" + this.pagingConfig.type : "";
                // 手势禁止
                if (i === this.pagingConfig.pageIndex && this.pagingConfig.disabled)
                    active += ' _disabled';
                pagesLi = {
                    'dataIndex': i,
                    'pagesLiClass': active
                }
                pagesList.push(pagesLi);
            }
        }
        this.config.pagesList = pagesList;
 
        // 禁用下一页
        var next_disabled = this.pagingConfig.pageIndex >= this.config.pageNum ? ' _disabled_c ' : '';
        // 手势禁止
        if (this.pagingConfig.pageIndex >= this.config.pageNum && this.pagingConfig.disabled)
            next_disabled += ' _disabled ';
        // 下一页
        this.config.nextClass = '_next_ ' + next_disabled + (this.pagingConfig.nextText ? ' _next' : ' _iconfont iconGroup-')
        // 尾页
        if (this.pagingConfig.type <= 1) {
            this.config.lastClass = '_last ' + next_disabled;
        }
    }
 
    //点击首页
    homeClick(){
        if (this.pagingConfig.pageIndex > 1) {
            this.refreshPaging(1);
            this.callback(1);
        }
    }
    //点击上一页
    prevClick(){
        if (this.pagingConfig.pageIndex - 1 > 0) {
            this.refreshPaging(this.pagingConfig.pageIndex - 1);
            this.callback(this.pagingConfig.pageIndex - 1);
        }
    }
    //区间值点击事件
    pagesClick(event){
        let pagesVal = event.target.innerText;
        if (pagesVal != this.pagingConfig.pageIndex) {
            this.refreshPaging(Number(pagesVal));
            this.callback(Number(pagesVal));
        }
    }
    //点击下一页
    nextClick(){
        if (this.pagingConfig.pageIndex < this.config.pageNum) {
            this.refreshPaging(this.pagingConfig.pageIndex + 1);
            this.callback(this.pagingConfig.pageIndex + 1);
        }
    }
    //点击尾页
    lastClick(){
        if (this.pagingConfig.pageIndex < this.config.pageNum) {
            this.refreshPaging(this.config.pageNum);
            this.callback(this.config.pageNum);
        }
    }
    //输入页号
    jumperPageNumBlur(event){
        let value_1 = ~~event.target.value;
        let value_2 = value_1;
        // 最大页码
        if (value_1 > this.config.pageNum)value_2 = this.config.pageNum;
        // 最小页码
        if (value_1 <= 0)value_2 = 1;
 
        if (value_2 !== this.pagingConfig.pageIndex){
            this.refreshPaging(value_2);
            this.callback(value_2);
        } else {
            if(value_1 !== this.pagingConfig.pageIndex){
                this.pagingConfig.pageIndex = value_1;
                this.pagingConfig.pageIndex = value_2;
            }
        }
    }
    // 回调
    callback(index){
        // 回调
        typeof this.pagingConfig.currentChange === 'function' && this.pagingConfig.currentChange(index);
 
        let page = {
            currentPage: this.pagingConfig.pageIndex,
            total: this.pagingConfig.total,
            pageSize: this.pagingConfig.pageSize
        };
        const getPageEvent = new CustomEvent('pagingclick', {
            detail: {page: page}
        });
        this.dispatchEvent(getPageEvent);
    }
 
    getBetween = function () {
        // 最小下标
        var min = this.pagingConfig.pageIndex - Math.floor(this.pagingConfig.pageCount / 2);
        // 最小下标最大值
        if (min > this.config.pageNum - this.pagingConfig.pageCount) {
            min = this.config.pageNum - this.pagingConfig.pageCount + 1;
        }
        // 最小值
        if (min <= 1)
            min = 1;
        // 最大下标
        var max = this.pagingConfig.pageIndex + Math.floor(this.pagingConfig.pageCount / 2);
        // 最大下标最小值
        if (max < this.pagingConfig.pageCount)
            max = this.pagingConfig.pageCount;
        // 最大值
        if (max > this.config.pageNum)
            max = this.config.pageNum;
        return { min: min, max: max };
    };
    validate = function (options) {
        if (!options)
            throw new Error('options of null');
        if (typeof options !== 'object')
            throw new Error('options not an object');
        try {
            ['type', 'pageIndex', 'pageSize', 'pageCount', 'total'].forEach(function (v) {
                if (options[v]) {
                    if (isNaN(options[v]))
                        throw new Error(v + " not an number");
                    if (v === 'pageCount' && options[v] % 2 === 0)
                        throw new Error(v + " not an odd number");
                }
            });
        }
        catch (error) {
            throw new Error(error);
        }
        return true;
    };
 
    // 判断类型是否为1
    get isType1(){
        return this.pagingConfig.type === 1;
    }
}