forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slick.pager.js
146 lines (115 loc) · 5.15 KB
/
slick.pager.js
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
(function($) {
function SlickGridPager(dataView, grid, $container)
{
var $status, $contextMenu;
function init()
{
dataView.onPagingInfoChanged.subscribe(function(pagingInfo) {
updatePager(pagingInfo);
});
constructPagerUI();
updatePager(dataView.getPagingInfo());
}
function getNavState()
{
var cannotLeaveEditMode = !Slick.GlobalEditorLock.commitCurrentEdit();
var pagingInfo = dataView.getPagingInfo();
var lastPage = Math.floor(pagingInfo.totalRows/pagingInfo.pageSize);
return {
canGotoFirst: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum > 0,
canGotoLast: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum != lastPage,
canGotoPrev: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum > 0,
canGotoNext: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum < lastPage,
pagingInfo: pagingInfo,
lastPage: lastPage
}
}
function setPageSize(n)
{
dataView.setPagingOptions({pageSize:n});
}
function gotoFirst()
{
if (getNavState().canGotoFirst)
dataView.setPagingOptions({pageNum: 0});
}
function gotoLast()
{
var state = getNavState();
if (state.canGotoLast)
dataView.setPagingOptions({pageNum: state.lastPage});
}
function gotoPrev()
{
var state = getNavState();
if (state.canGotoPrev)
dataView.setPagingOptions({pageNum: state.pagingInfo.pageNum-1});
}
function gotoNext()
{
var state = getNavState();
if (state.canGotoNext)
dataView.setPagingOptions({pageNum: state.pagingInfo.pageNum+1});
}
function constructPagerUI()
{
$container.empty();
$status = $("<span class='slick-pager-status' />").appendTo($container);
var $nav = $("<span class='slick-pager-nav' />").appendTo($container);
var $settings = $("<span class='slick-pager-settings' />").appendTo($container);
$settings
.append("<span class='slick-pager-settings-expanded' style='display:none'>Show: <a data=0>All</a><a data='-1'>Auto</a><a data=25>25</a><a data=50>50</a><a data=100>100</a></span>");
$settings.find("a[data]").click(function(e) {
var pagesize = $(e.target).attr("data");
if (pagesize != undefined)
{
if (pagesize == -1)
{
var vp = grid.getViewport();
setPageSize(vp.bottom-vp.top);
}
else
setPageSize(parseInt(pagesize));
}
});
var icon_prefix = "<span class='ui-state-default ui-corner-all ui-icon-container'><span class='ui-icon ";
var icon_suffix = "' /></span>";
$(icon_prefix + "ui-icon-lightbulb" + icon_suffix)
.click(function() { $(".slick-pager-settings-expanded").toggle() })
.appendTo($settings);
$(icon_prefix + "ui-icon-seek-first" + icon_suffix)
.click(gotoFirst)
.appendTo($nav);
$(icon_prefix + "ui-icon-seek-prev" + icon_suffix)
.click(gotoPrev)
.appendTo($nav);
$(icon_prefix + "ui-icon-seek-next" + icon_suffix)
.click(gotoNext)
.appendTo($nav);
$(icon_prefix + "ui-icon-seek-end" + icon_suffix)
.click(gotoLast)
.appendTo($nav);
$container.find(".ui-icon-container")
.hover(function() {
$(this).toggleClass("ui-state-hover");
});
$container.children().wrapAll("<div class='slick-pager' />");
}
function updatePager(pagingInfo)
{
var state = getNavState();
$container.find(".slick-pager-nav span").removeClass("ui-state-disabled");
if (!state.canGotoFirst) $container.find(".ui-icon-seek-first").addClass("ui-state-disabled");
if (!state.canGotoLast) $container.find(".ui-icon-seek-end").addClass("ui-state-disabled");
if (!state.canGotoNext) $container.find(".ui-icon-seek-next").addClass("ui-state-disabled");
if (!state.canGotoPrev) $container.find(".ui-icon-seek-prev").addClass("ui-state-disabled");
if (pagingInfo.pageSize == 0)
$status.text("Showing all " + pagingInfo.totalRows + " rows");
else
$status.text("Showing page " + (pagingInfo.pageNum+1) + " of " + (Math.floor(pagingInfo.totalRows/pagingInfo.pageSize)+1));
}
init();
}
// Slick.Controls.Pager
$.extend(true, window, { Slick: { Controls: { Pager: SlickGridPager }}});
})(jQuery);