{"__v":1,"_id":"55097b4e4c7c3f2300aabf12","category":{"__v":8,"_id":"550974cc368a56170041475d","project":"55070e814bb83b2500ec9404","version":"550974cb368a561700414757","pages":["55097b214c7c3f2300aabf0f","55097b272dd6a11900e6e7bb","55097b2edd77250d00736a09","55097b352dd6a11900e6e7c2","55097b3e2dd6a11900e6e7c6","55097b4e4c7c3f2300aabf12","55097b712dd6a11900e6e7c8","55097b8bad1f0523008ecbdc"],"reference":false,"createdAt":"2015-03-18T11:08:31.853Z","from_sync":false,"order":4,"slug":"displaying-views-to-users","title":"Displaying Views to Users"},"project":"55070e814bb83b2500ec9404","user":"55070d24d30b3f190011b941","version":{"__v":1,"_id":"550974cb368a561700414757","forked_from":"55070e814bb83b2500ec9407","project":"55070e814bb83b2500ec9404","createdAt":"2015-03-18T12:51:23.709Z","releaseDate":"2015-03-18T12:51:23.709Z","categories":["550974cc368a561700414758","550974cc368a561700414759","550974cc368a56170041475a","550974cc368a56170041475b","550974cc368a56170041475c","550974cc368a56170041475d","550974cc368a56170041475e"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.4.0","version":"1.4"},"updates":[],"createdAt":"2015-03-18T13:19:10.911Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":5,"body":"In the chapter titled [Getting Paginated Data](doc:getting-paginated-data), we talked about how to get pages of records from the database (records 11-20, for example). Now we'll show you how to create links to the other pages in your view.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Displaying Paginated Links with the paginationLinks Function\"\n}\n[/block]\n If you have fetched a paginated query in your controller (normally done using [findAll()](doc:findall) and the page argument), all you have to do to get the page links to show up is this: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfoutput>#paginationLinks()#</cfoutput>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nGiven that you have only fetched one paginated query in your controller, this will output the links for that query using some sensible defaults.\n\nHow simple is that? \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Arguments Used for Customization\"\n}\n[/block]\nSimple is good, but sometimes you want a little more control over how the links are displayed. You can control the output of the links in a number of different ways. We'll show you the most important ones here. Please refer to the [paginationLinks()](doc:paginationlinks) documentation for all other uses.\n\n#### The _name_ Argument\n\nBy default, Wheels will create all links with page as the variable that holds the page numbers. So the HTML code will look something like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<a href=\\\"/main/userlisting?page=1\\\">\\n<a href=\\\"/main/userlisting?page=2\\\">\\n<a href=\\\"/main/userlisting?page=3\\\">\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nTo change page to something else, you use the name argument like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfoutput>#paginationLinks(name=\\\"pgnum\\\")#</cfoutput>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nBy the way, perhaps you noticed how Wheels chose to use that hideous question mark in the URL, despite the fact that you have URL rewriting turned on? Because [paginationLinks()](doc:paginationlinks) uses [linkTo()](doc:linkto) in the background, you can easily get rid of it by creating a custom route. You can read more about this in the [Using Routes](doc:using-routes) chapter.\n\n#### The _windowSize_ Argument\n\nThis controls how many links to show around the current page. If you are currently displaying page 6 and pass in `windowSize=3`, Wheels will generate links to pages 3, 4, 5, 6, 7, 8, and 9 (three on each side of the current page).\n\n#### The _alwaysShowAnchors_ Argument\n\nIf you pass in true here, it means that no matter where you currently are in the pagination or how many page numbers exist in total, links to the first and last page will always be visible.\n\n### Managing More Than One Paginated Query Per Page\n\nMost of the time, you'll only deal with one paginated query per page. But in those cases where you need to get/show more than one paginated query, you can use the handle argument to tell Wheels which query it is that you are referring to.\n\nThis argument has to be passed in to both the `findAll()` function and the `paginationLinks()` function. (You assign a handle name in the `findAll()` function and then request the data for it in `paginationLinks()`.)\n\nHere is an example of using handles:\n\nIn the controller... \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfset users = model(\\\"user\\\").findAll(handle=\\\"userQuery\\\", page=params.page, perPage=25)>\\n<cfset blogs = model(\\\"blog\\\").findAll(handle=\\\"blogQuery\\\", page=params.page, perPage=25)>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nIn the view...\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<ul>\\n    <cfoutput query=\\\"users\\\">\\n        <li>#users.name#</li>\\n    </cfoutput>\\n</ul>\\n\\n<cfoutput>#paginationLinks(handle=\\\"userQuery\\\")#</cfoutput>\\n\\n<cfoutput query=\\\"blog\\\">\\n    #address#<br />\\n</cfoutput>\\n\\n<cfoutput>#paginationLinks(handle=\\\"blogQuery\\\")#</cfoutput>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nThat's all you need to know about showing pagination links to get you started. As always, the best way to learn how the view functions work is to just play around with the arguments and see what HTML is produced.","excerpt":"How to create links to other pages in your paginated data in your views.","slug":"displaying-links-for-pagination","type":"basic","title":"Displaying Links for Pagination"}

Displaying Links for Pagination

How to create links to other pages in your paginated data in your views.

In the chapter titled [Getting Paginated Data](doc:getting-paginated-data), we talked about how to get pages of records from the database (records 11-20, for example). Now we'll show you how to create links to the other pages in your view. [block:api-header] { "type": "basic", "title": "Displaying Paginated Links with the paginationLinks Function" } [/block] If you have fetched a paginated query in your controller (normally done using [findAll()](doc:findall) and the page argument), all you have to do to get the page links to show up is this: [block:code] { "codes": [ { "code": "<cfoutput>#paginationLinks()#</cfoutput>", "language": "text" } ] } [/block] Given that you have only fetched one paginated query in your controller, this will output the links for that query using some sensible defaults. How simple is that? [block:api-header] { "type": "basic", "title": "Arguments Used for Customization" } [/block] Simple is good, but sometimes you want a little more control over how the links are displayed. You can control the output of the links in a number of different ways. We'll show you the most important ones here. Please refer to the [paginationLinks()](doc:paginationlinks) documentation for all other uses. #### The _name_ Argument By default, Wheels will create all links with page as the variable that holds the page numbers. So the HTML code will look something like this: [block:code] { "codes": [ { "code": "<a href=\"/main/userlisting?page=1\">\n<a href=\"/main/userlisting?page=2\">\n<a href=\"/main/userlisting?page=3\">", "language": "text" } ] } [/block] To change page to something else, you use the name argument like so: [block:code] { "codes": [ { "code": "<cfoutput>#paginationLinks(name=\"pgnum\")#</cfoutput>", "language": "text" } ] } [/block] By the way, perhaps you noticed how Wheels chose to use that hideous question mark in the URL, despite the fact that you have URL rewriting turned on? Because [paginationLinks()](doc:paginationlinks) uses [linkTo()](doc:linkto) in the background, you can easily get rid of it by creating a custom route. You can read more about this in the [Using Routes](doc:using-routes) chapter. #### The _windowSize_ Argument This controls how many links to show around the current page. If you are currently displaying page 6 and pass in `windowSize=3`, Wheels will generate links to pages 3, 4, 5, 6, 7, 8, and 9 (three on each side of the current page). #### The _alwaysShowAnchors_ Argument If you pass in true here, it means that no matter where you currently are in the pagination or how many page numbers exist in total, links to the first and last page will always be visible. ### Managing More Than One Paginated Query Per Page Most of the time, you'll only deal with one paginated query per page. But in those cases where you need to get/show more than one paginated query, you can use the handle argument to tell Wheels which query it is that you are referring to. This argument has to be passed in to both the `findAll()` function and the `paginationLinks()` function. (You assign a handle name in the `findAll()` function and then request the data for it in `paginationLinks()`.) Here is an example of using handles: In the controller... [block:code] { "codes": [ { "code": "<cfset users = model(\"user\").findAll(handle=\"userQuery\", page=params.page, perPage=25)>\n<cfset blogs = model(\"blog\").findAll(handle=\"blogQuery\", page=params.page, perPage=25)>", "language": "text" } ] } [/block] In the view... [block:code] { "codes": [ { "code": "<ul>\n <cfoutput query=\"users\">\n <li>#users.name#</li>\n </cfoutput>\n</ul>\n\n<cfoutput>#paginationLinks(handle=\"userQuery\")#</cfoutput>\n\n<cfoutput query=\"blog\">\n #address#<br />\n</cfoutput>\n\n<cfoutput>#paginationLinks(handle=\"blogQuery\")#</cfoutput>", "language": "text" } ] } [/block] That's all you need to know about showing pagination links to get you started. As always, the best way to learn how the view functions work is to just play around with the arguments and see what HTML is produced.