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

dropdown filtering view of dataobjects from frontend?


Go to End
Reply


11 Posts   4320 Views

Avatar
Jarri

Community Member, 10 Posts

10 April 2009 at 11:40pm

Hi

I am very new to Siverstripe off the beaten track of setting up a basic website up and running so the following may well seem really naive...

I have a big table of exam results that I plan to setup as a dataobject. I should be able to create this under ModelAdmin - creating the ability for the customer to import the table as .csv

I should be able to display this data ok in some form on the website. I need to filter (eg. by level) and sort the results (by grade) based on a number of criteria from the data - I should be able to produce individual get statements to achieve this.

The only problem is that there are a high number of distinct levels - so the brute force approach of creating seperate get statements seems daft.

What would seem ideal is to setup a generic view which would create a get statement based on a filter chosen from a dropdown menu (containing the list of all levels).

I am sure this is possible - could someone point me to some relevant documentation - provide some code snippets - or just generally point me in the right direction?

Thanks!

Avatar
AlaVive

Community Member, 42 Posts

2 September 2009 at 9:39am

Edited: 02/09/2009 9:40am

Was any response ever given to this (or, did you manage a solution)?

Your request seems similar to something I have to address, and I'm a bit lost as to how to proceed.

Avatar
zenmonkey

Community Member, 528 Posts

2 September 2009 at 12:44pm

Hmmm the filtering/sorting sounds very similar to a problem I was having. Take a look at this post

http://www.silverstripe.org/dataobjectmanager-module-forum/show/267505#post267505

I ended up extending the sorting a few more levels, I'll try to post my final code in the morning when I get to the office

You should be able to populate the drop down with with a similar SQL query used in the solution.

Avatar
AlaVive

Community Member, 42 Posts

3 September 2009 at 3:25am

Thanks, zenmonkey. I'd love to see your code, but this should give me some guidance.

Avatar
zenmonkey

Community Member, 528 Posts

3 September 2009 at 5:03am

Okay here is the code from my Controller

function SortByCategory()
   {
      //Sort Continents Based on Current Category
      function Continents($cat){
         
         
         $category = $cat;
         $continents = new DataObjectSet();
         $results = DB::query("SELECT DISTINCT SellerContinent FROM ProductSeller WHERE SellerCategory ='$category' ORDER BY SellerContinent ASC");
         if($results){
            foreach($results as $result){
               $continent = $result['SellerContinent'];
               $continents->push(new ArrayData(array(
                                           'Continent' => $continent,
                                           'Country' => Country($category, $continent)
                                           )));
            }
            return $continents;
         }
         
      }
      //Sort Countries based on Current Category and Continent and OUtput Sellers
      function Country($cat, $con){
         
         $category = $cat;
         $continent = $con;
         
         
         $countries = new DataObjectSet();
         $results = DB::query("SELECT DISTINCT SellerLocation FROM ProductSeller WHERE SellerCategory ='$category' AND SellerContinent='$continent' ORDER BY SellerLocation ASC");
         if($results){
            foreach($results as $result){
               $country = $result['SellerLocation'];
               $countries->push(new ArrayData(array(
                                           'Country' => $country,
                                           'Seller' => DataObject::get("ProductSeller","SellerLocation = '$country' AND SellerContinent='$continent' AND SellerCategory='$category'")
                                           )));
            }
         }
         return $countries;
      }
      
      //Initial Sort
      $categorys = new DataObjectSet();
      $results = DB::query("SELECT DISTINCT SellerCategory FROM ProductSeller ORDER BY SellerCategory ASC");
      if($results)
      {
         foreach($results as $result)
         {
            $category = $result['SellerCategory'];
            
            
            $categorys->push(new ArrayData(array(
                                        'Category' => $category,
                                        'Continents' => Continents($category)
                                        )));
            
            }
            return $categorys;
            }
            return false;
            }

and this is the template

   <ul>
      <% control SortByCategory %>
      <% if Category != "Web" %>
      <li>$Category
         <ul>
            <% control Continents %>
            <li>$Continent
               <ul>
                  <% control Country %>
                  <li>$Country
                     <ul>
                        <% control Seller %>
                        <li>$SellerName $SellerURL $SellerPhone</li>
                        <% end_control %>
                     </ul>
                  
                  </li>
                  <% end_control %>
               </ul>
            </li>
            <% end_control %>
         </ul>
      </li>
      <% end_if %>
      <% end_control %>
   </ul>

I don't know if its the most elegant solution but it work. :)

Avatar
lanks

Community Member, 61 Posts

1 February 2010 at 9:55pm

Edited: 01/02/2010 9:56pm

Hi guys I'm trying to do this as well however I have taken a different approach. I have got my filter query that I want to use however I am not sure how to get this to work with my dropdown field. I have created a form with a dropdownfield where I want it to filter the list of dataobjects without reloading the page, so I need some sort of ajax functionality. I have set up a function that takes the parameter for the form. The function is using pagination but you get the idea it just needs to pass $data from the form and run the query, here it is:

public function CodeSnippetsforform($data) {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
$SQL_start = (int)$_GET['start'];
$doSet = DataObject::get(
$callerClass = "CodeSnippet",
$filter = "`ParentID` = '".$this->ID."' AND `CodeSnippet_Classifications`.ClassificationID = '".$data."'",
$sort = "SnipDate DESC",
$join = "LEFT JOIN `CodeSnippet_Classifications` ON `CodeSnippet`.ID = `CodeSnippet_Classifications`.CodeSnippetID",
$limit = "{$SQL_start},5");
return $doSet ? $doSet : false;
}

This is my code for the form:

public function SnipForm(){
$fields = new FieldSet(
new DropdownField(
'sortauth',
'Choose an Author',
Dataobject::get("Classification")->toDropdownMap("ID", "Title")
)
);
$actions = new FieldSet(
new FormAction('CodeSnippetsforform', 'Filter')
);

return new Form($this, 'SnipForm', $fields, $actions);

}

Another thing is that if I do it this way that I need to create some sort of a flag to allow for the function to only be run when the dropdownfield has been selected or to get it to run in a way that doesn't filter when the page loads because as you will see in my template I am running the function when the page loads which obviously crashes the query, here is the basic code that I'm using:

$SnipForm

<% control CodeSnippetsforform %>

<% end_control %>

So as you can see I'm missing ajax functionality and I am not sure what to do so any help will be appreciated.
Now I suspect that this could possibly be the complete wrong way to do this as I don't really understand the way you guys are doing it, perhaps you could show me your code for your form, or possibly if you or anyone can point me in the right direction to getting my method to work that would be great.

Thanks
Liam

Avatar
zenmonkey

Community Member, 528 Posts

2 February 2010 at 3:05am

There's an ajax basics in the recipes section (I really think they need to hi-light that section of the site better)

http://doc.silverstripe.org/doku.php?id=recipes:ajax_basics

That should get you started :)

Avatar
lanks

Community Member, 61 Posts

2 February 2010 at 12:57pm

Hi zenmonkey.
I feel as though using ajax for this is not necessary. If it's not a problem with you would it be possible for you to show me the code that you have used for your dropdown from the template and how it works with your function. This is all I really need to so if you guys have got this working then I'm sure that I can adapt what I want to do, using the approach that you guys have taken.

Thanks
Liam

Go to Top