can you change the way you are generating pagination links?
You should avoid to use link at all. Use simple buttons like this:
<button data-page='1' data-sort='name' data-dir='asc'>1</button>
And in js:
$(document).on('click','.pagination button', function(e){
var page = $(this).attr('data-page');
var sort = $(this).attr('data-sort') || null;
var dir = $(this).attr('data-dir') || null;
getPage(sort, dir, page);
});
I suppose I could. I haven't gone that far into it yet. Currently, I'm using Laravel 5's default with $item->render() method. And I haven't gone into how best to change the default behavior yet.
I ended up doing something like this in jQuery:
$(document).on('click','.pagination a', function(e) {
e.preventDefault();
var sort = $(this).attr('href').split('sort=')[1] || null;
var page = $(this).attr('href').split('page=')[1];
if (sort == null) {
var theurl = '/admin/user/list?page=' + page;
} else {
var theurl = '/admin/user/list?sort=' + sort;
}
$.ajax({
url: theurl
}).done(function(data){
$('.ajax').html(data);
$("html, body").animate({
scrollTop: 0
},
200
);
});
});
$(document).on('click','.ajax th a', function(e) {
e.preventDefault();
var sort = $(this).attr('href').split('sort=')[1];
var theurl = '/admin/user/list?sort=' + sort;
$.ajax({
url: theurl
}).done(function(data){
$('.ajax').html(data);
});
});
Obviously this code leaves a lot to be desired, but it works. I'll be honest, I'm not even sure exactly how it works, because I'm not having any of the query strings at all, just adding a split at the first query string, and it assumes the rest somehow.
In the first on click, I'm just doing a check for page vs sort because Laravel 5's default pagination only selects the page query first before any other query, even with the appends method (I'm sure there is a way around that, but I didn't need to worry about it in my case). The second on click just activates the query string when the headings are clicked on the table.
Anyway, it works. I'm still not sure why the split works without the rest of the query string, so that part I would love if someone could chime in and explain why that's working. Thank you.
If you are sure about query string parameters order, your code works. Looks more like a math problem, nothing related to programming or laravel or javascript.
If you want to forget about parameters order, obtain an object from querystring and use this object in data property of $.ajax object configuration.
$.ajax({url: '/admin/user/list', data:paramsobj});
Take a look at jsbin linked in the post above:
Thank you slillo. For this particular use case, it's not going to change, but I get what you are saying now, and I should probably implement it regardless of whether the order will ever change (one can never be 100% sure right?). Thank you very much for looking at that.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community