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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Help with basic table - managed via modelAdmin - with display limit & multiple page results


Go to End


36 Posts   4408 Views

Avatar
CHD

Community Member, 219 Posts

26 August 2010 at 3:38am

Hi,

fairly new to SS, but loving it so far.
working on a site with a table of products.
simple table, that can be managed via modelAdmin.

i want the products to then be listed in a table on the frontend, with 25 per page, with next, previous links etc. then add a search box that searches ONLY the table of products. each product will not have its own page though, just sit in the table with a link to email/enquire about that product.

so far, i have this:
http://www.worldaircraftsolutions.com.sg/catalogue/

to get to where i am, i created the following:

in mysite>code>myCatalgueAdmin.php

<?php
class MyCatalogAdmin extends ModelAdmin {

public static $managed_models = array( //since 2.3.2
'Product'
);

static $url_segment = 'products'; // will be linked as /admin/products
static $menu_title = 'Catalogue Admin';

}
?>

in mysite>code>Product.php:

<?php
class Product extends DataObject {

static $db = array(
'Title' => 'Varchar',
'PartNumber' => 'Varchar',
'SerialNumber' => 'Varchar',
'AircraftRegistration' => 'Varchar',
'Type' => 'Varchar',
'AircraftSerial' => 'Varchar',
'Quantity' => 'Varchar',
'Price' => 'Currency'
);

static $summary_fields = array(
'Title',
'PartNumber',
'SerialNumber'
);

static $searchable_fields = array(
'Title',
'PartNumber',
'SerialNumber'
);

}
?>

in mysite>code>Page.php:

function ProductList ($limit = 25){
$list = DataObject::get('Product', '', '', '', $limit);
return $list;
}

then in themes>template>catalogue.ss

<table id="ProductList" width="100%" border="0">
<tr>
<th width="">Title</th>
<th width="">Part Number</th>
<th width="">Serial Number</th>
<th width="">Aircraft <br>
Registration</th>
<th width="">Type</th>
<th width="">Aircraft <br>
Serial</th>
<th width="">Quantity </th>
<th width="">Email us</th>
</tr>
<% if ProductList %>

<!--$ProductList.sort(Title)-->
<% control ProductList %>
<tr height="20px">
<td>$Title</td>
<td>$PartNumber</td>
<td>$SerialNumber</td>
<td>$AircraftRegistration</td>
<td>$Type</td>
<td>$AircraftSerial</td>
<td>$Quantity </td>
<td><a href="mailto:test@test.com?subject=$Title">Email us</a> </td>
</tr>

<% end_control %>
<a href="$ProductList.NextLink">Next</a>
<a href="$ProductList.PrevLink">Previous</a>
<% end_if %>

</table>

$SearchForm

i cant figure out the following:
A) am i even on the right track??
B) how do i get the next and previous links to work? currently they just change append the URL with ?start=25 but the list doesn't change
C) the search box searches the whole site (and seems to skip this table of products all together)

if anybody can help with this i will be eternally grateful!

thank you!!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

26 August 2010 at 4:21am

First, your ProductList function has its limit hardcoded, so you're negating any possible pagination. I'm not sure what that function is doing in Page.php. Wouldn't you want it in a controller specifically for this product list page? Unless you're showing the product list on multiple page types, I don't think it belongs there.

Here's your new function:

public function ProductList ($filter = null){
if(!isset($_REQUEST['start'])) $_REQUEST['start'] = 0;
$limit = $_REQUEST['start'].",25";
return DataObject::get('Product', $filter, null, null, $limit);
}

As for the serachform, you're trying to repurpose the site search for something entirely different. You'll need to create a custom search form.

public function ProductSearchForm() {
return new Form (
$this,
"ProductSearchForm",
new FieldSet(new TextField('s','')),
new FieldSet(new FormAction('doProductSearch','Search'))
);
}

public function doProductSearch($data, $form) {
if(isset($data['s'])) {
$filter = "SomeField LIKE '%".Convert::raw2sql($data['s'])."%'";
}
return array (
'ProductList' => $this->ProductList($filter);
);
}

Avatar
CHD

Community Member, 219 Posts

26 August 2010 at 4:24am

amazingly fast reply, thanks!
that code looks great, i'll test this evening and get back to you!!

thanks again.

Avatar
CHD

Community Member, 219 Posts

26 August 2010 at 8:52pm

well the first half worked perfectly!
http://www.worldaircraftsolutions.com.sg/catalogue/

but not sure where to paste the second half? (search coding)
i tried putting it into page.php but then the site wouldnt load up.

am i supposed to paste that somewhere else? (im using 2.4 btw)

p.s - i pasted everything into page.php for now, just to keep it in one place, once i figure it all out and it works i'll separate the code into its relevant pages.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

27 August 2010 at 1:27am

The search form can go in your ProductPage_Controller class or whatever controller you're using on that page.

And then of course you need to add $ProductSearchForm to that template.

Avatar
CHD

Community Member, 219 Posts

27 August 2010 at 4:33am

hmmm still cant figure it out.

ive put it all in my Catalogue.php file:

<?php
class Catalogue extends Page {
public function ProductList ($filter = null){
if(!isset($_REQUEST['start'])) $_REQUEST['start'] = 0;
$limit = $_REQUEST['start'].",25";
return DataObject::get('Product', $filter, null, null, $limit);
}

public function ProductSearchForm() {
return new Form (
$this,
"ProductSearchForm",
new FieldSet(new TextField('s','')),
new FieldSet(new FormAction('doProductSearch','Search'))
);
}

public function doProductSearch($data, $form) {
if(isset($data['s'])) {
$filter = "SomeField LIKE '%".Convert::raw2sql($data['s'])."%'";
}
return array (
'ProductList' => $this->ProductList($filter);
);
}
}

class Catalogue_Controller extends Page_Controller {

}

?>

but now the site wont load.
(it works fine when i take out the search part)

where am i going wrong?

thanks so much for your help!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

27 August 2010 at 5:38am

What does "won't load" mean?

Avatar
CHD

Community Member, 219 Posts

27 August 2010 at 5:46am

Well as soon as i upload the file, then reload the site i just get a blank white screen. Im sure firebug could tell you more but i dont know how to use it for debugging! (i only use it to see css in action etc)

Go to Top