Join Sara Morgan for an in-depth discussion in this video Adding pagination, part of Visualforce: Improving Performance.
- In a previous video I showed you how to change the Account Search page that I introduced you in Chapter two, to use the standard set controller. But in this video I'll continue to expand that same page to have it include some basic pagination so the user can actually click through all the records a search returns, because at this point we're only displaying the first 20 records in our page set, and we have no method for getting to the additional records.
So to show you how this works, I'm also going to show you some documentation that is available on the force.com Developer website. And it's at this URL that you can get access to the standard set controller class. And within it, we have access to the methods that shows us that we can get the first, which shows us the first page of records. And also, we can get variables such as, hasNext, and hasPrevious, which are bullion variables just telling us whether or not there are any more records in the page set.
And there's a lot more methods that you can go through and find out what they do here. But I'm going to go ahead and go back to my Account Search class and I'm going to go to the page markup and I'm going to add a Command button here in my Command button section. And my going to go ahead and give it a value attribute and I'm going to use just some characters to indicate that we're going to the first record in the page set.
And I'll go ahead and use a title attribute that actually has the word first in case they hover over that button. And then at this point my action attribute will use the expression syntax to call the setController variable and access the first method within it. And now that I have my first button I could go ahead and click Save and you can see I have that first button.
But I want to have one for also the Previous, Next, and Last records, so I'm just going to duplicate this button and add three instances of it below and then just make slight changes to it so that I can get also the Previous, by calling the previous method. Change the direction of my arrows to get the, I just have one, and get the Next and call the next method.
And finally, the Last. And now if I were to save my page you'll see all four of those buttons appear right next to the Search Accounts. And if I do a search, and pull this down so we can see the results of our search a little better, I can now page through this and get to more records, until I get all the way to the very last set of records, or go all the way back up here to the first set of records.
So now I have some basic functonality that I can use, and if we go and look also at the view state here, notice that it's very small. Because we're only returning 20 records at a time, that's all that's being stored in view state, and that's not very much, so the size of my page state and my view state here is not very high, even though I'm not using the transient variable. And this is why I told you earlier, you didn't have to worry about the fact that you couldn't use this at that point.
But I do want to point out a potential problem. And that is that if I go ahead and refresh this page, to where I don't have any records returned, and then try to access one of these buttons, look what happened. I get an error that tells me that I'm trying to access something that doesn't exist. And that's because of the fact that I haven't actually returned any results. Now the best way to avoid this is to just not have any of those buttons appear unless there is some data that has been returned with that result set.
So to do that, I can take advantage of a really nice attribute here, called Rendered, that I can add to the Command button, and it returns a bullion value. And inside of here I can use my expression syntax to also reference from the set controller variable the hasPrevious. And this will tell me that I will only render that button when I have some results.
Now unfortunately I added this here to my Search Accounts. and where I really wanted to add this was to the first button here, so I'm going to go ahead and change that, and add it also here to the previous method. But now for the Next and Last, I want to go ahead and change that to instead be whether or not I have any Next records. So now that I have these changes made, I can go ahead and click Save.
And it looks like I forgot one other thing, let's go back and see. Ah, I have this a little too close there. Let's go ahead and click Save again and now you'll see that my buttons have disappeared because I don't have any results. And if I go back and do a search again for A, now my buttons will appear and I can page through and get to the last record. So in other words, I will only have the Previous buttons, or the Next buttons if there are any that are actually to be delivered.
And so now my is avoiding that error and it's also still using the paging functionality that I want. In this video you saw how easy it is to utilize the standard set controller and its built in pagination features. By using this highly optimized class, we were able to keep a page that returned hundreds of records rendering just as fast as it would if it were displaying results for only 20 records.
- Reducing and eliminating view states
- Evaluating SOQL for efficiency
- Using Workbench and the Query Plan tool to evaluate queries
- Reducing use of action tags through Visualforce remoting
- Working with the StandardSetController class
- Using static resources