0

I am facing an issue in pagination counter, I am using yii2 pagination, on keypress it is going to another page i.e http://localhost/web/index.php?r=test/page=1 so I need to increment the page value in the upper url I am already doing like this but it is not working, the value is increment only once, and I need to increment the value for every page

<script type="text/javascript">
var val = 1;
$(document).keydown(function (event) {
  val++
    if (event.keyCode == '39') {
        window.location.href = "localhost/web/index.php?r=test/page="+ val;
    }
});

</script>

please give some suggestions, thanks a lot.

Asif Khan
  • 131
  • 1
  • 13

2 Answers2

1

Looks like you're defining the page number (val) as 1 on every page so when you click the right arrow it sends you to page 2 every time. You'll need to first get the query parameter page to decide the value of val. Here's an example of getting query parameters with vanilla javascript How can I get query string values in JavaScript?.

You're also incrementing the page count on any button press, that looks like it should be within the right arrow keyCode condition.

Community
  • 1
  • 1
0

Of course this won't work because you assign "1" to "val". So after keydown, "val" should always be "2". You need to store the page number somewhere else, for example, get it from url;

var pageNumber = getParameter('page');
$('document').on('keydown', function(event){
    if(event.keyCode === 39) && window.location.href = ......
})
jilykate
  • 5,430
  • 2
  • 17
  • 26