Hello, I'm new with Umbraco and I'm now working on pagination feature for a page with a grid. I want it to show 4 grid lines in each page. I've been searching for some code snippets to help me but so far couldn't really find any that looks like what I need and to use with grid.
I suspect the reason you've not got any responses as what you're trying to do sounds a little bit out there!
Can you explain what you need to do a bit more clearly - there might be a much easier and cleaner way of doing it?
The grid is a bit of an animal and not suited to all applications. I would suggest if you're hell bent on doing what you're trying to do you render the grid output on the page and then hide all content and create pagination links in Javascript to show and hide the relevant
thank you for your answer. Let's say I have a list of items organized within a grid editor, for example having each line of the grid with 3 items (each item clickable to other pages).
I started with a small list but now my list of items is too big so I want to add paging for better navigation. So I'm looking for a solution that shows the content of my grid in different pages. I would like to work with grid because it's easier for the person who will maintain the website contents to keep updating them easily. All the items of this list are partial views, so another question I have is if I have to create a template to add the pagination, or add the pagination code to my partial view file.
I hope this is not too confusing. If so, I will try to explain better.
Thank you very much for your help! I'm new with Umbraco and web development and so far I've managed to keeps things moving, but now I'm really stuck with this feature.
The grid is stored as a big JSON block so to access part of it you're probably going to have to parse the JSON to get certain rows to power pagination.
If the grid content isn't too large I'd consider doing this in Javascript client side (output the whole content and show/hide what you need) - otherwise what you're describing (in my humble opinion) would be much easier to develop, maintain and content admin in a more simple child node structure.
That said - perhaps someone else here will have a better suggestion for you?!
The content won't be too large, maybe I will need between 5 and 10 pages with 4 grid lines each. So if I want to do the pagination with javascript, I just need to create the contents as I usually do, and then in the template I have to add the javascript right? Is it possible for you to point to me any code snippet with this feature?
By simple child node sctructure you mean creating the pages statically and moving contents from pages when I do update them and creating a child node everytime I add a new page?
Basically change the logic where it changes the text to something that hides all the grid rows and then only shows the "page" splice you want... something like:
$( "div.umb-grid .row" ).hide( );
$( "div.umb-grid .row" ).slice( num * 5, num * 5 + 5 ).show( );
Hi Steve, thank you for your answer!
In my template I have the following:
@CurrentPage.GetGridHtml("work")
to retrieve the grid and render it in my page. This returns the whole grid. How can I specify in the javascript to get only pages (5num , 5num+5?)
I haven't worked much with javascript, so I'm pretty newbie with the whole thing. I think for the pagination layout I got it, but still dont quite understand how to do the mechanism to show only the specific rows from the grid.
Thank you very much Steve! You've been a great help!
first of all I think the grid isn't the right tool for what you try to do. Maybe you should overthink your concept and try to find a better solution.
If you really want to do paging of the grid the easiest way would be to use the skybrud grid model which lets you use an object for your grid data. With that i think you can way better iterate through the lines if your grid and then maybe skip/take the lines based in the "page" you are.
Hi David, thank you for your answer.
I have checked the skybrud grid model and installed it, but still not sure how it can be useful to do the paging. Does it contains any feature for paging?
As others keep saying - this is not what the grid is for! It's too flexible (you can put rows and columns within rows etc etc etc... )
BUT - if you insist this should work though I haven't tested it and can't promise it covers all bases. Note if editors embed rows within rows then this won't work ... This is using the twbs-pagination from the first link. * NOTE I am unable to fully test this.
Another note- if you have multiple grids on the page this won't work - you'll need to add an id selector container div.
$(document).ready(function () {
var itemsPerPage = 3;
// work out how many pages there are
var totalPages = Math.ceil($("div.umb-grid div.grid-section").children('div').length / itemsPerPage);
$('#pagination-demo').twbsPagination({
totalPages: totalPages,
onPageClick: function (event, page) {
var num = page - 1;
// this selector basically finds the grid and "slices" the divs underneath the gridsection div which *should* relate to rows / pages in the grid
$("div.umb-grid div.grid-section").children('div').hide();
$("div.umb-grid div.grid-section").children('div').slice(num * itemsPerPage, num * itemsPerPage + itemsPerPage).show();
}
});
});
I still think you'd be better off changing this to a parent - child node model where to create new "pages" you get the editors to create child nodes under the page. That way they can use the inbuild sort functionality - remove pages at will and you can control the output much more cleanly.
thank you very much for your answer! Sorry for the delay in answering you. I like that idea very much, but how can I create the pagination if I use this model?
However I doesnt seem to work, because I quite not fully understood how to create the pages according to the child nodes I have. I defined in the developer Section a number of 6 items per page, and when I add more than 6 the page still displays more than 6 pages and also the pagination is not showing in my page. I'm sorry if this is such a dumb question, but I'm really a nooby in web development.
I think Steve is right. The better solution here would be a simple child node structure.
You could create a page that only shows content. Below that page you create pagination-pages with a grid on each page.
The structure could look like this:
My first page
Pagination 1
Pagination 2
Pagination 3
My second page
Pagination 1
Pagination 2
You could also make a grid on the content page and make some logic that looks for child pagination pages. In that way you can use the same nodetype for pages without pagination.
Haven't tested this, but it should work:
@{
int pageCount = 1;
var paginationPage = Request.Params["pagination"];
var paginationCollection = CurrentPage.Descendants("pagination");
}
@if(String.IsNullOrEmpty(paginationPage)){
@CurrentPage.GetGridHtml("work")
}else{
int skipPage = paginationPage.AsInt() - 2;
@paginationCollection.Skip(skipPage).Take(1).GetGridHtml("work")
}
@if(paginationCollection.Count > 0){
<ul>
<li><a href="@CurrentPage.Url">@pageCount</a></li>
@foreach(var pagination in paginationCollection){
pageCount++;
<li><a href="@[email protected]">@pageCount</a></li>
}
</ul>
}
The JS code I posted was for the Umbraco Grid which definitely wasn't the right way to go... depending on how many child nodes you have this still might not be quite the right way to go. You need to think about page load times (if there were say 200 child nodes then this solution is potentially loading a lot of content (especially if you're going to have images this will affect the browser as well as the page size).
If you will have a lot of child pages then you should only load what you need and then load the rest via AJAX - there are plenty of solutions using Umbraco (using either alternative templates or an API) - the docs of that plugin will show you how to call ajax from the page links. You could also look at lazy loading images..
For simplicity though to help you along let's fix the JS so your solution works.
So the key bits are:
1) ensure you have included JQuery - jquery plugins only work if JQuery is included before the plugins.
2) You need a way of the plugin knowing what the div that contains your paged records is. I've added an Id called "pagination-results" to the container's row div. This is very important.
3) You then also need to change the JS to identify the records (these are divs with the class "col-sm-4" though it might be an idea to give this a specific class.
Your JS script (if you already have Jquery delete the CDN line).
<script src="https://code.jquery.com/jquery-2.2.2.min.js" integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI=" crossorigin="anonymous"></script>
<script src="jquery.twbsPagination.min.js"></script>
<script>
$(document).ready(function() {
var itemsPerPage = 3;
// work out how many pages there are
var totalPages = Math.ceil($("#pagination-results").children('div.col-sm-4').length / itemsPerPage);
$('#pagination-demo').twbsPagination({
totalPages: totalPages,
onPageClick: function (event, page) {
var num = page - 1;
// this selector basically finds the records and "slices" the divs underneath the container div
$("#pagination-results").children('div.col-sm-4').hide();
$("#pagination-results").children('div.col-sm-4').slice(num * itemsPerPage, num * itemsPerPage + itemsPerPage).show();
}
});
});
</script>
thank you so much, you saved my life!!
It works perfectly! Have some issues in the displaying of the pagination div but Im sure I can work it out.
How can I add '?page={{page}}' to the URL?
I tried the following but it didnt work:
href: '?page={{page}}',
I started to work on a couple pages with the grid, but know I'll have to change most of them the parent-child model as I'll have more contents than I expected. Also, within these pages I will have to implement a feature to filter some items through the selection from a dropdown list and search engine. Could you give me some hints on these?
Cannot thank you enough for the help you provided me, thank you very very much!
Then in your JS you need to show / hide those that match the category filter.
var category = "test"; // get this from your drop down
$("#pagination-results").children('div.col-sm-4').hide();
$("#pagination-results").children('div.col-sm-4[data-selected="'+category+'"]').show();
How this will work with the plugin and pagination I'm not sure - you'll need to reinitialise it after filtering I would think. Recount the total records...
Pagination for a grid
Hello, I'm new with Umbraco and I'm now working on pagination feature for a page with a grid. I want it to show 4 grid lines in each page. I've been searching for some code snippets to help me but so far couldn't really find any that looks like what I need and to use with grid.
Can anyone help me please? Thank you very much!
Anyone can help? Please really need help on this.
Hi,
I suspect the reason you've not got any responses as what you're trying to do sounds a little bit out there!
Can you explain what you need to do a bit more clearly - there might be a much easier and cleaner way of doing it?
The grid is a bit of an animal and not suited to all applications. I would suggest if you're hell bent on doing what you're trying to do you render the grid output on the page and then hide all content and create pagination links in Javascript to show and hide the relevant
Kind regards
Steve
Hi Steve,
thank you for your answer. Let's say I have a list of items organized within a grid editor, for example having each line of the grid with 3 items (each item clickable to other pages).
I started with a small list but now my list of items is too big so I want to add paging for better navigation. So I'm looking for a solution that shows the content of my grid in different pages. I would like to work with grid because it's easier for the person who will maintain the website contents to keep updating them easily. All the items of this list are partial views, so another question I have is if I have to create a template to add the pagination, or add the pagination code to my partial view file.
I hope this is not too confusing. If so, I will try to explain better.
Thank you very much for your help! I'm new with Umbraco and web development and so far I've managed to keeps things moving, but now I'm really stuck with this feature.
Hi,
The grid is stored as a big JSON block so to access part of it you're probably going to have to parse the JSON to get certain rows to power pagination.
If the grid content isn't too large I'd consider doing this in Javascript client side (output the whole content and show/hide what you need) - otherwise what you're describing (in my humble opinion) would be much easier to develop, maintain and content admin in a more simple child node structure.
That said - perhaps someone else here will have a better suggestion for you?!
Steve
The content won't be too large, maybe I will need between 5 and 10 pages with 4 grid lines each. So if I want to do the pagination with javascript, I just need to create the contents as I usually do, and then in the template I have to add the javascript right? Is it possible for you to point to me any code snippet with this feature? By simple child node sctructure you mean creating the pages statically and moving contents from pages when I do update them and creating a child node everytime I add a new page?
Thank you very much!
HI,
A very quick Google turned this up:
http://esimakin.github.io/twbs-pagination/#demo
or this: http://botmonster.com/jquery-bootpag/#example-simple
Basically change the logic where it changes the text to something that hides all the grid rows and then only shows the "page" splice you want... something like:
$( "div.umb-grid .row" ).hide( ); $( "div.umb-grid .row" ).slice( num * 5, num * 5 + 5 ).show( );
// see https://api.jquery.com/slice/
Hi Steve, thank you for your answer! In my template I have the following:
to retrieve the grid and render it in my page. This returns the whole grid. How can I specify in the javascript to get only pages (5num , 5num+5?)
I haven't worked much with javascript, so I'm pretty newbie with the whole thing. I think for the pagination layout I got it, but still dont quite understand how to do the mechanism to show only the specific rows from the grid.
Thank you very much Steve! You've been a great help!
Hi Nelson,
first of all I think the grid isn't the right tool for what you try to do. Maybe you should overthink your concept and try to find a better solution.
If you really want to do paging of the grid the easiest way would be to use the skybrud grid model which lets you use an object for your grid data. With that i think you can way better iterate through the lines if your grid and then maybe skip/take the lines based in the "page" you are.
Regards David
Hi David, thank you for your answer. I have checked the skybrud grid model and installed it, but still not sure how it can be useful to do the paging. Does it contains any feature for paging?
Thanks!
Nelson,
As others keep saying - this is not what the grid is for! It's too flexible (you can put rows and columns within rows etc etc etc... )
BUT - if you insist this should work though I haven't tested it and can't promise it covers all bases. Note if editors embed rows within rows then this won't work ... This is using the twbs-pagination from the first link. * NOTE I am unable to fully test this.
Another note- if you have multiple grids on the page this won't work - you'll need to add an id selector container div.
});
I still think you'd be better off changing this to a parent - child node model where to create new "pages" you get the editors to create child nodes under the page. That way they can use the inbuild sort functionality - remove pages at will and you can control the output much more cleanly.
The tutorial I helped write has this: https://our.umbraco.org/documentation/tutorials/creating-basic-site/Articles-Parent-and-Article-Items
Kind regards
Steve
Hello Steve,
thank you very much for your answer! Sorry for the delay in answering you. I like that idea very much, but how can I create the pagination if I use this model?
Thank you!
Hi,
Follow the instructions on how to use the JS plugin.
http://esimakin.github.io/twbs-pagination/#demo
All I've done is modify the demo code shown to make it work with what you need. You'll obviously need to include the JS plugin on the page.
Steve
Hi Steve,
I included the JS plugin in my page, using the following code:
And I'm testing this with the Blog Overview code from the fanoe starter kit, so my code looks like this:
However I doesnt seem to work, because I quite not fully understood how to create the pages according to the child nodes I have. I defined in the developer Section a number of 6 items per page, and when I add more than 6 the page still displays more than 6 pages and also the pagination is not showing in my page. I'm sorry if this is such a dumb question, but I'm really a nooby in web development.
Thanks you very much!
I think Steve is right. The better solution here would be a simple child node structure.
You could create a page that only shows content. Below that page you create pagination-pages with a grid on each page.
The structure could look like this:
You could also make a grid on the content page and make some logic that looks for child pagination pages. In that way you can use the same nodetype for pages without pagination.
Haven't tested this, but it should work:
Hi Nelson,
The JS code I posted was for the Umbraco Grid which definitely wasn't the right way to go... depending on how many child nodes you have this still might not be quite the right way to go. You need to think about page load times (if there were say 200 child nodes then this solution is potentially loading a lot of content (especially if you're going to have images this will affect the browser as well as the page size).
If you will have a lot of child pages then you should only load what you need and then load the rest via AJAX - there are plenty of solutions using Umbraco (using either alternative templates or an API) - the docs of that plugin will show you how to call ajax from the page links. You could also look at lazy loading images..
For simplicity though to help you along let's fix the JS so your solution works.
So the key bits are: 1) ensure you have included JQuery - jquery plugins only work if JQuery is included before the plugins. 2) You need a way of the plugin knowing what the div that contains your paged records is. I've added an Id called "pagination-results" to the container's row div. This is very important.
3) You then also need to change the JS to identify the records (these are divs with the class "col-sm-4" though it might be an idea to give this a specific class.
So change your Razor to:
Your JS script (if you already have Jquery delete the CDN line).
Steve,
thank you so much, you saved my life!! It works perfectly! Have some issues in the displaying of the pagination div but Im sure I can work it out. How can I add '?page={{page}}' to the URL? I tried the following but it didnt work:
I started to work on a couple pages with the grid, but know I'll have to change most of them the parent-child model as I'll have more contents than I expected. Also, within these pages I will have to implement a feature to filter some items through the selection from a dropdown list and search engine. Could you give me some hints on these?
Cannot thank you enough for the help you provided me, thank you very very much!
Hi,
For the page URL you can do something like:
window.history.replaceState({}, "Page title", "/yourpage#page=" + page);
Can't test that but should be right.
As for filtering - you might now want to look at an Ajax solution to this though it is possible using data attributes and on page JS.
Something like in your razor (say you have an Umbraco field called category):
Then in your JS you need to show / hide those that match the category filter.
How this will work with the plugin and pagination I'm not sure - you'll need to reinitialise it after filtering I would think. Recount the total records...
Check out http://stackoverflow.com/questions/24961833/reset-total-pages-in-jquery-pagination-plugin
At this point it's becoming a pure JS question though so you might just need to have a go and ask on StackOverflow or similar.
Before you go too far into the JS route though I suggest you take a look at doing this in Ajax and using Umbraco more...: http://24days.in/umbraco/2012/ajax-paging-using-alttemplates/
HTH
Steve
Here's a self contained html demo version. If you have problems with the above see how this is workign (copy it into a html file).
is working on a reply...