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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Dynamic dropdown field in DOM


Go to End
Reply


16 Posts   4656 Views

Avatar
Bright Eyes David

Community Member, 26 Posts

16 February 2011 at 6:02am

Wow, that's great. I had seen that SSbits page on dropdowns, but your example has given me a concrete example that's working. Thanks Andi!

Avatar
Bright Eyes David

Community Member, 26 Posts

20 February 2011 at 6:20am

Edited: 20/02/2011 6:22am

I've now transferred the Disco example you've given to my needs (Part and PartCategory), and it's working perfectly. What has had me stumped all day is creating bundles of these same parts.

It's a many many relationship. Bundles can have many parts, and parts can belong to many bundles. For the life of me I can't figure out how to enable this as well as having the categories which are already in place. :( I've been trying to use ManyManyComplexTableField but am well and truly stuck.

@tmkp / Andi: I don't expect you to endlessly spend your time on my problems for nothing - if you would be able to take a look at this I would gladly pay for your time as it's for a client project. Many thanks.

Avatar
tmkp

Community Member, 42 Posts

20 February 2011 at 11:37am

Edited: 20/02/2011 11:41am

Hi again David,

I'm lacking the time right now to code this up from scratch, but as a quick example i'll post another snippet i've used in a project a while ago. It should be fairly easy to merge this into your project, otherwise i'd be glad to go a bit more into detail some time next week.

This a basic setup for a DOM-based news system. A news post can have multiple categories, and a category can have multiple news articles. When you create a new article you can multiselect the categories via Checkboxes, the checkboxes will be created dynamically.

Again, I stripped out all the extra functions and db fields except for the title to make it a little more readable.

<?php

class NewsCategory extends DataObject {

   static $db = array(
      "Title" => "Text",
   );

   static $has_one = array (
      'Holder' => 'NewsHolder',
   );

   // an Category can have many NewsPost objects associated with it.
   // calling $this->getManyManyComponents('Posts') retrieves the associated NewsPost objects.
   static $many_many = array(
      'Posts' => 'NewsPost'
   );

   function getCMSFields() {
      $f = new FieldSet();
      $f->push( new TextField( 'Title', 'Title') );
      return $f;
   }

}

<?php

class NewsPost extends DataObject {

   static $db = array(
      "Title" => "Text",
   );

   static $has_one = array (
      'Holder' => 'NewsHolder',
   );

   // Defines the join in the referenced class as $belongs_many_many.
   // a new table, (this-class)_(relationship-name), will be created with a pair of ID fields.
   static $belongs_many_many = array(
      'Categories' => 'NewsCategory'
   );

   function getCMSFields() {
      $f = new FieldSet();

      // add fields
   $f->push( new TextField('Title','Title') );

      // retrieve all the category objects in the database
      $cat_list = DataObject::get('NewsCategory');
      $cat_list->sort('Title', 'ASC');
      // add a checkbox set field to the Main tab in the CMS, the checkboxes use all the category
      // objects in the system, so if you created 5 categories on the NewsHolder class, then
      // it shows 5 checkboxes here to select.
      $f->push( new CheckboxSetField('Categories', 'Categories', $cat_list) );

      // return fieldset
      return $f;
   }

}

For your project, the NewsCategory could serve as the template for "Bundle", and you could just merge the necessary parts from NewsPost into your Parts class.

Also, if you're handling a lot of Bundles/Parts Relationships and you're getting too many checkboxes, you could try using a listbox field instead..

Hope this helps for now, Grüße nach Köln : )

Andi

Avatar
Bright Eyes David

Community Member, 26 Posts

20 February 2011 at 12:52pm

Ah, that's working. I can see I need to get used to using DataObject::get. Vielen Dank, Andi. :)

I'm pretty sure there will be another couple of little things that need sorting on this site (and maybe on other SS sites) - if you'd be interested in helping out and being paid for your time it would be great if you could let me know as I'd be much happier if I knew there was someone available who could cover these sorts of things. If you fancy it, maybe send me a message via http://doliver.co.uk/#message

Thanks!

Avatar
DanStephenson

Community Member, 115 Posts

12 October 2011 at 6:47pm

Really great work here gentlemen. Thank you for posting the completed code, as this has come in VERY handy for me on a project I am currently working on, and was confused how to do a dynamic dropdown inside a DOM.

If I wanted to be able to sort by Category on my DiscoHolder, so I had the following layout:

CATEGORY 1 NAME
- Disco Record 1
- Disco Record 2
- Disco Record 3

CATEGORY 2 NAME
- Disco Record 4
- Disco Record 5
- Disco Record 6

How could I go about that?

Avatar
tmkp

Community Member, 42 Posts

16 October 2011 at 5:11am

Edited: 16/10/2011 5:25am

Hey Dan,
from the top of my head, you should be able to put something like this in your DiscoHolder.ss

<% control Categories %>
   <% if Records %>
      <h4>$Title:</h4>
      <% control Records %>
         <p>$Title</p>
      <% end_control %>
   <% end_if %>
<% end_control %>

The if clause will make sure the category only shows if there are any records in it.

Hope that does it!

Avatar
k_k

Community Member, 2 Posts

6 March 2012 at 5:22am

OH! MY DOG! XDtmkp you just save my life! thanks you so much!!!

Avatar
Bronwyn

Community Member, 20 Posts

22 March 2012 at 1:25pm

Thanks HEAPS for this. I've been pulling my hair out trying to do just this very thing.

My version has an "Award" object, all of which belong to one "Awards" page, and with a many to many relationship to a "laboratory" page. I.e. some people who get awards are associated with more than one lab, and of course labs can have more than one award. The Award object contains the name of the person getting it, as the only actual "People" in our database are senior staff members, and many awards are given to students, who are here only fleetingly. I have, thanks to the help in this thread, got an awards page with a list of all awards, and on each senior staff member's page a list of the awards given to members of that group. I would like to have a link from each item on the awards page to the staff member's page (or members' pages) that the awardee is associated with - is this possible? And how would you do it?

Go to Top