teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

修正

2020/03/20 14:49

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- 【jQuery】絞り込み検索の値を維持したままページングをしたい
1
+ 質問質問質問質問質問
body CHANGED
@@ -1,376 +1,1 @@
1
- 下記を参考にjQueryの絞り込み検索(カテゴリ間はAND検索、カテゴリ内はOR検索)までお陰様で問題なく実装することができましたが、ページングの実装で躓きました。
2
- https://teratail.com/questions/119008
3
-
4
- ●問題点
5
- 例えば「a-1」「a-2」のチェックボックスを選択した場合、「1,3,4,6」が絞り込まれます。
6
- 想定であれば、ページングの「次へ」を押すと3件表示のため、「6」が表示されます。
7
- 「1,3,4」→ページ送り→「6」→ページ戻り→「1,3,4」(**動作OK**)
8
-
9
- ただし、現状はページングの「次へ」を押すと「4,5,6」が表示されます。
10
- 「1,3,4」→ページ送り→「4,5,6」→ページ戻り→「1,2,3」(**動作NG**)
11
-
12
- どのコードを改修するべきなのか、大変困っています。
13
- どなたか方法をご教示いただけますと幸いです。
14
- 何卒よろしくお願い申し上げます。
15
-
16
- 下記にソースを貼り付けます。
17
- ```HTML
18
- <body>
19
- <form>
20
- <div id="select">
21
- <!--all(ページリロード)ボタン-->
22
- <input type="button" value="選択を解除する" onclick="allselect()">
23
-
24
- <!--検索-->
25
- <div id="category1">
26
- <label for="a-1">
27
- <input type="checkbox" id="a-1">
28
- a-1</label>
29
- <label for="a-2">
30
- <input type="checkbox" id="a-2">
31
- a-2</label>
32
- <label for="a-3">
33
- <input type="checkbox" id="a-3">
34
- a-3</label>
35
- </div>
36
- <div id="category2">
37
- <label for="b-1">
38
- <input type="checkbox" id="b-1">
39
- b-1</label>
40
- <label for="b-2">
41
- <input type="checkbox" id="b-2">
42
- b-2</label>
43
- <label for="b-3">
44
- <input type="checkbox" id="b-3">
45
- b-3</label>
46
- </div>
47
- <div id="category3">
48
- <label for="c-1">
49
- <input type="checkbox" id="c-1">
50
- c-1</label>
51
- <label for="c-2">
52
- <input type="checkbox" id="c-2">
53
- c-2</label>
54
- <label for="c-3">
55
- <input type="checkbox" id="c-3">
56
- c-3</label>
57
- </div>
58
- </div>
59
- </form>
60
-
61
- <!--検索結果-->
62
- <div class="result">
63
- <ul>
64
- <li class="a-1 b-2 c-3">1</li>
65
- <li class="a-3 b-1 c-2">2</li>
66
- <li class="a-2 b-3 c-1">3</li>
67
- <li class="a-2 b-2 c-3">4</li>
68
- <li class="a-1 b-1 c-2">5</li>
69
- <li class="a-3 b-3 c-1">6</li>
70
- </ul>
71
- <div class="pager"></div>
72
-
73
- </div>
74
- <script>
75
- $(function($) {
76
- $('.result').pagination({
77
- element :'li',
78
- prevText : '<',
79
- nextText : '>',
80
- firstText : '≪',
81
- lastText : '≫',
82
- ellipsisText : '…',
83
- viewNum : 3,
84
- pagerNum : 3,
85
- ellipsis : true,
86
- linkInvalid : true,
87
- goTop : true,
88
- firstLastNav : true,
89
- prevNextNav : true
90
- });
91
- });
92
- </script>
93
-
94
- <script>
95
- function allselect() {
96
- $('.result ul li').show();
97
- $('.result ul li:visible:lt(2)').show();
98
- $('.result ul li:visible:gt(2)').hide();
99
- $('#select input[type="checkbox"]').prop('checked', false);
100
- }
101
- </script>
102
-
103
- </body>
104
- ```
105
-
106
- ```Javascript
107
- $(function () {
108
- $('[type=checkbox]').on('change', function () {
109
- if ($('#category1,#category2,#category3').find('[type=checkbox]:checked').length == 0) {
110
- $('.result ul li').show();
111
- } else {
112
- var myList = [];
113
- ["category1", "category2", "category3"].forEach(function (x) {
114
- if ($('#' + x + ' [type=checkbox]:checked').length > 0) {
115
- myList.push($('#' + x + ' [type=checkbox]:checked').map(function () {
116
- return '.' + $(this).attr('id');
117
- }).get());
118
- }
119
- });
120
- var classList = [];
121
- for (var i = 0; i < myList.length; i++) {
122
- if (classList.length == 0) classList = myList[i];
123
- if (i >= myList.length - 1) break;
124
- var tmp = [];
125
- for (var j = 0; j < classList.length; j++) {
126
- for (var k = 0; k < myList[i + 1].length; k++) {
127
- tmp.push(classList[j] + myList[i + 1][k]);
128
- }
129
- }
130
- classList = tmp;
131
- }
132
-
133
- $('.result ul li').hide().filter(classList.join(",")).show();
134
- $('.result ul li:visible:lt(2)').show();
135
- $('.result ul li:visible:gt(2)').hide();
136
-
137
- }
138
- }).eq(0).trigger('change');
139
-
140
-
141
- $("input[type=checkbox]").change(function () {
142
- $("input[type=checkbox]").each(function () {
143
- if ($(this).is(":checked")) {
144
- $(this).parent().addClass("selected");
145
- } else {
146
- $(this).parent().removeClass("selected");
147
- }
148
- });
149
- });
150
-
151
-
152
- });
153
-
154
- ```
155
-
156
- ```Javascript
157
- $(function($){
158
- $.fn.pagination = function(config) {
159
- // オプション
160
- var o = $.extend({
161
- element :'li',
162
- prevText : '<',
163
- nextText : '>',
164
- firstText : '≪',
165
- lastText : '≫',
166
- ellipsisText : '…',
167
- viewNum : 3,
168
- pagerNum : 3,
169
- ellipsis : true,
170
- linkInvalid : true,
171
- goTop : true,
172
- firstLastNav : true,
173
- prevNextNav : true
174
- }, config);
175
-
176
- // セレクタ
177
- var $this = $(this);
178
- var $element = $this.find(o.element);
179
- var $pager = $this.find('.pager');
180
- if(o.ellipsis){
181
- var $ellipsisFirst= $('<span class="ellipsis"/>').text(o.ellipsisText);
182
- var $ellipsisLast= $('<span class="ellipsis"/>').text(o.ellipsisText);
183
- }
184
-
185
- // 値取得
186
- var tatalItemNum =$element.length;
187
- var tatalpageNum = Math.ceil(tatalItemNum /o.viewNum);
188
- var ellipsisFlag = false;
189
-
190
- // 変数設定
191
- var currentIndex = 0;
192
-
193
- // 省略記号フラグ判定
194
- if(tatalpageNum > o.pagerNum) { ellipsisFlag = true;}
195
-
196
- // ページカウンター
197
- $this.find('.nownum').text('1');
198
- $this.find('.totalnum').text(tatalpageNum);
199
-
200
- // ページャーの生成
201
- if(tatalpageNum == 1){return false;}
202
- for (var i=0; i < tatalpageNum; i++) {
203
- $('<span/>').text(i + 1).appendTo($pager);
204
- };
205
- if(o.prevNextNav){
206
- $('<span class="prev"/>').text(o.prevText).prependTo($pager);
207
- $('<span class="next"/>').text(o.nextText).appendTo($pager);
208
- }
209
- if(o.firstLastNav){
210
- $('<span class="first"/>').text(o.firstText).prependTo($pager);
211
- $('<span class="last"/>').text(o.lastText).appendTo($pager);
212
- }
213
- var $pagerInner = $pager.find('span').not('.first, .last, .prev, .next');
214
-
215
- // コンテンツの初期表示
216
- $element.not('.pager, .pageNum').hide();
217
- for (var i=0; i < o.viewNum; i++) {
218
- $($element[i]).show();
219
- };
220
-
221
- // ページャーの初期表示
222
- $pagerInner.hide();
223
- for (var i=0; i < o.pagerNum; i++) {
224
- $($pagerInner[i]).show();
225
- };
226
- $($pagerInner[0]).addClass('current');
227
- if (o.linkInvalid) {
228
- $('.prev').addClass('invalid');
229
- $('.first').addClass('invalid');
230
- }
231
- if(o.ellipsis) {
232
- if(ellipsisFlag){
233
- if(tatalpageNum - o.pagerNum > 1) {
234
- $($pagerInner[tatalpageNum - 1]).before(function() {
235
- return $ellipsisLast;
236
- });
237
- }
238
- $($pagerInner[tatalpageNum - 1]).show();
239
- }
240
- }
241
-
242
- // 最初のボタンをクリック
243
- $this.find('.first').on('click', function(){
244
- var current = 0;
245
- if(currentIndex == 0){
246
- if (o.linkInvalid) {
247
- return false;
248
- }
249
- }
250
- change(current);
251
- });
252
-
253
- // 最後のボタンをクリック
254
- $this.find('.last').on('click', function(){
255
- var current = tatalpageNum - 1;
1
+ 質問質問質問質問質問質問質問質問質問質問質問質問質問質問質問
256
- if(currentIndex == $pagerInner.length - 1){
257
- if (o.linkInvalid) {
258
- return false;
259
- }
260
- }
261
- change(current);
262
- });
263
-
264
- // 前のボタンをクリック
265
- $this.find('.prev').on('click', function(){
266
- var current = currentIndex - 1;
267
- if(currentIndex == 0){
268
- if (o.linkInvalid) {
269
- return false;
270
- } else {
271
- current = currentIndex;
272
- }
273
- }
274
- change(current);
275
- });
276
-
277
- // 次のボタンをクリック
278
- $this.find('.next').on('click', function(){
279
- var current = currentIndex + 1;
280
- if(currentIndex == $pagerInner.length - 1){
281
- if (o.linkInvalid) {
282
- return false;
283
- } else {
284
- current = currentIndex;
285
- }
286
- }
287
- change(current);
288
- });
289
-
290
- // 番号をクリック
291
- $pagerInner.each(function (current) {
292
- $(this).on('click', function(){
293
- if($(this).hasClass('current')){
294
- if (o.linkInvalid) {
295
- return false;
296
- }
297
- }
298
- change(current);
299
- });
300
- });
301
-
302
- // 切り替え実行
303
- var change = function (current) {
304
- // コンテンツの表示
305
- $element.not('.pager, .pageNum').hide();
306
- for(var i = (current * o.viewNum) ; i < ((current + 1) * o.viewNum) ; i++){
307
- $($element[i]).show();
308
- }
309
- // ページャーの表示
310
- $pagerInner.hide();
311
- if(o.ellipsis) {
312
- if(ellipsisFlag){
313
- $ellipsisFirst.remove();
314
- $ellipsisLast.remove();
315
- }
316
- }
317
- var num = current - 1;
318
- if(num < 0) {
319
- num = 0;
320
- }
321
- if(num > (tatalpageNum - o.pagerNum)){
322
- num = tatalpageNum - o.pagerNum;
323
- }
324
- for(var i = num ; i < (num + o.pagerNum) ; i++){
325
- $($pagerInner[i]).show();
326
- };
327
- // 省略記号の表示
328
- if(o.ellipsis) {
329
- if(ellipsisFlag){
330
- // 前の省略記号
331
- $($pagerInner[0]).show();
332
- if(num > 1){
333
- $($pagerInner[0]).after(function() {
334
- return $ellipsisFirst;
335
- });
336
- }
337
- // 後ろの省略記号
338
- if(num < (tatalpageNum - o.pagerNum - 1)) {
339
- $($pagerInner[tatalpageNum - 1]).before(function() {
340
- return $ellipsisLast;
341
- });
342
- }
343
- $($pagerInner[tatalpageNum - 1]).show();
344
- }
345
- }
346
- // 現在位置設定
347
- currentIndex = current;
348
- // デザイン
349
- $pagerInner.removeClass('current');
350
- $($pagerInner[current]).addClass('current');
351
- if (o.linkInvalid) {
352
- if(current == 0 ) {
353
- $this.find('.prev').addClass('invalid');
354
- $this.find('.first').addClass('invalid');
355
- } else {
356
- $this.find('.prev').removeClass('invalid');
357
- $this.find('.first').removeClass('invalid');
358
- }
359
- if(current == tatalpageNum - 1 ) {
360
- $this.find('.next').addClass('invalid');
361
- $this.find('.last').addClass('invalid');
362
- } else {
363
- $this.find('.next').removeClass('invalid');
364
- $this.find('.last').removeClass('invalid');
365
- }
366
- }
367
- // ページトップへの戻り
368
- if(o.goTop){
369
- $('html,body').scrollTop(0);
370
- }
371
- // ページカウンター
372
- $this.find('.nownum').text(currentIndex + 1);
373
- }
374
- }
375
- });
376
- ```