3070 Posts in 869 Topics by 651 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1503 Views |
-
ModelAdmin, Dropdown

19 November 2009 at 9:12am
Hi,
Given the example at the following ModelAdmin, then add another field, e.g. description, into the Category class. How do I then choose which of the fields (title or description) to show in the Dropdown (as displayed at point 3 in Usage on the linked page)?
Further to that, is it possible to concatenate them? In my application i have 3 fields, Title, Forename and Surname which I would like to concatenate into the dropdown on my page - currently it just shows Title, which isn't too helpful!!
Regards
-
Re: ModelAdmin, Dropdown

24 November 2009 at 12:17am
Just bumping this - has no-one else struggled with this? I was thinking it was something other people had wanted to do!
-
Re: ModelAdmin, Dropdown

4 December 2009 at 10:08am
Good one !
I've bumped into this as well , searched a bit through the docs but in the end resorted to deleting the field with
removeByName() and then adding it again by creating the DropDownField by hand ...There's probably an easier way (maybe some custom param or method on the dataobject to set the default "label") but I cannot find it..
;-)
-
Re: ModelAdmin, Dropdown

11 December 2009 at 5:07am
Thanks Fuzz10,
I've had a look at how to remove fields and add them again, but I am struggling to get it working. I wonder if you have an example I could see?
Assuming I have the code below, and I would like to achieve having dropdowns which show "$title - $composer" (as currently i have pieces called the same thing by different people!); Would i have to put the getCMSFields() call in Service.php and remove each of the fields relating to Repertoire so they can be re-added as dropdowns - or have one entry in Repertoire.php? Also, I don't really understand what I am supposed to add the field to as it appears that I have to attach it to a tab? Sorry - I am probably getting it all wrong!
Repertoire.php
class Repertoire {
static $db = new array (
'title' = > 'varchar',
'composer' => 'varchar'
);
}Service.php
class Service {
static $db = new array (
'type' = > 'enum('Matins, Evensong', 'Evensong')',
'location' => 'varchar'
);static $has_one = new array(
'Introit' => 'Repertoire',
'Anthem' => 'Repertoire',
'Setting' => 'Repertoire'
);
} -
Re: ModelAdmin, Dropdown

11 December 2009 at 10:38pm
I suppose the code below is just an example , but if not : you need to extend DataObject ... ;-)
Untested code of course :
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(new DropdownField('IntroitID','Repertoire',Dataobject::get("Repertoire")->toDropdownMap("ID", "title")));
return $fields;
}
Change title to your own combination of composer+title .....
-
Re: ModelAdmin, Dropdown

12 December 2009 at 7:34am
Thanks again for the reply (and yes, it was an example on which i forgot to put the 'extends DataObject'),
Unfortunately I cannot work out the very last step:
Change title to your own combination of composer+title .....
All is well if the value is 'Title', or 'Composer', but if it is a mix, ie 'Title - Composer', then the field is empty. I can understand that as I assume it uses it as a key to get the value. I have looked up about the toDropdownMap and it can take a field or a method to put the value in. Unfortunately the method seems to need to be part of the Service object rather than Repertoire, so i dont know how i can get that to return Title - Composer.
Stuck again!!
Hope more help is on its way!
Regards
-
Re: ModelAdmin, Dropdown

12 December 2009 at 8:30am Last edited: 12 December 2009 8:31am
Yeah sorry, I was a bit incomplete there..
You can add a method to your dataobject class to create the value you want , and then call it in your dropdownField...
So:
e.g.
function getTitleComposer(){
return $this->Title.",".$this->Composer;
}and then :
new DropdownField('IntroitID','Repertoire',Dataobject::get("Repertoire")->toDropdownMap("ID", "getTitleComposer")));
| 1503 Views | ||
|
Page:
1
|
Go to Top |


