Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

[RESOLVED] dataobjectset paginating search results


Reply


2 Posts   972 Views

Avatar
gregsaunders

Community Member, 12 Posts

8 June 2011 at 3:24pm

Hi all! Can someone point me to a tutorial or an article on how to put together a search page that returns results requiring pagination; i.e. 10 results per page. I'm having a challenge getting the pagination thing to work. I have a functioning search form, controller that performs the search, returns the results to the record set display template but I get all the matching records in $Results and not just the first 10 (as an example).

Thanks!
Greg

here is the controller function ...

class ListingResidentialSearchPage_Controller extends Page_Controller {

   ...

   function doSubmitSearch($data, $form) {
      //print_r($data);
      $area          = $data['Area'];
      $price_from    = $data['PriceFrom'];
      $price_to       = $data['PriceTo'];
      $property_class = $data['PropertyClass'];
      $bedrooms       = $data['Bedrooms'];
      $bathrooms       = $data['Bathrooms'];
      $query         = "Area: $area, List Price Between $price_from and $price_to, Property Class: $property_class, Bedrooms: $bedrooms, Bathrooms: $bathrooms";
      
      $listings = DataObject::get(
         'Listing',
         "Area = '$area' AND L_Price >= '$price_from' AND L_Price <= '$price_to' AND PClass = ($property_class) AND TBD $bedrooms AND FB $bathrooms",
         'L_Price asc',
         '',
         '500'
      );
      $listings->setPageLength(10);
      
      $data = array(
         'Results' => $listings,
         'Query' => $query,
         'Title' => 'Residential Search Results'
      );
      return $this->customise($data)->renderWith(array('ListingResidentialSearchPage_results', 'Page'));
   }
}

and here is the template ...

<div id="content_main" class="grid_9 content_main" style="padding-bottom: 10px;">
   <% if Level(2) %>
      <% include BreadCrumbs %>
   <% end_if %>

   <div class="typography">

      <h2>$Title: $Results.totalSize</h2>

      <p>You searched for: $Query</p>
      
      <% if Results %>
         <% control Results %>
            <div class="listing_record_summary" onclick="location.href='/listings/residential/view/$MLS_Number';" style="padding: 10px; margin-bottom: 10px; border: solid 1px grey;">
               <div style="width: 170px; float: left;">
                  <img width="150px" src="$getListingImageMain.Url"/>
               </div>
               <div style="width: 500px; float: left;">
                  <h5>$Address, $Area || $$getListPriceMoneyFmt</h5>
                  <p>Bedrooms: $TBD, Bathrooms: $FB || MLS&reg; $MLS_Number</p>
                  <p>$getPublicRemarksSummary ...</p>
               </div>
               <div style="clear:both;"></div>
            </div>
         <% end_control %>
      <% else %>
         <p>Sorry, your search query did not return any results.</p>
      <% end_if %>

      <% if Results.MoreThanOnePage %>
         <div id="PageNumbers">
            <% if Results.NotLastPage %>
            <a class="next" href="$Results.NextLink" title="View the next page">Next</a>
            <% end_if %>
            <% if Results.NotFirstPage %>
            <a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
            <% end_if %>
            <span>
               <% control Results.Pages %>
                  <% if CurrentBool %>
                  $PageNum
                  <% else %>
                  <a href="$Link" title="View page number $PageNum">$PageNum</a>
                  <% end_if %>
               <% end_control %>
            </span>
            <p>Page $Results.CurrentPage of $Results.TotalPages</p>
         </div>
      <% end_if %>
   </div>
</div>

Avatar
gregsaunders

Community Member, 12 Posts

9 June 2011 at 4:26am

Edited: 09/06/2011 4:28am

Solved this one on my own. Two problems: I was returning the entire dataobjectset instead of using the

getRange()

method of the dataobjectset and the other problem was not changing the action method of the form to GET from POST

$form->setFormMethod('get');

This makes the page link URLs pass the search parameters back into the controller.

Refer to
http://silverstripe.org/general-questions/show/10203#post274892
... that's where I found the answer. Thanks sparkalow.

Greg