5098 Posts in 1518 Topics by 1115 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 802 Views |
-
Filling a DropdownField According to values from another dropdownfield

13 March 2011 at 10:06pm
Here is my example:
I have car 'brand' dropdown and car 'Make' dropdown.
When you select BMW from brand, Make should be filled with values relative to BMW only like: 325, 325i, 330, ...I tried searching the forum and found an old topic (2008) http://silverstripe.org/customising-the-cms/show/4493
which shows the attempt was unsuccessful, I think it is an important feature, to dynamically be able to switch the values
of a dropdownfield as many site use it. -
Re: Filling a DropdownField According to values from another dropdownfield

14 March 2011 at 6:09am
So, I followed a tutorial: http://chillburn.com.au/blog/using-one-select-to-control-another-select-using-silverstripe-and-jquery/
And now I have the Car Model and Car Make fillable from the CMS.
The problem remains, when I try to add a new Car Page from the CMS, where I have to select the Car Make from a dropdown then the Car Model should be automatically populated.
IS there a way to do so? -
Re: Filling a DropdownField According to values from another dropdownfield

15 March 2011 at 10:30pm
Seems no one is interested enough to reply.
How are relations (one to one and one to many) stored with silverstripe?
Car Brand has Many Car Models.
Car Model has one Car Brand.CarPage encapsulates both and CarPage can have many Car Brands and many Car Models.
The trick is that you can have many CarPages sharing the same Car Models and Car Brands.
I tried CarModels and CarBrands has_many CarPages but it did not work.
as I am not sure where the IDs of the CarPages are stored so I can filter with them (they are not in the Tables). -
Re: Filling a DropdownField According to values from another dropdownfield

15 March 2011 at 11:38pm
Solved it
<?php
class CarModel extends DataObject {
static $db = array(
'Title' => 'Varchar(255)'
);
static $has_one = array(
'Brand' => 'CarBrand'/*,
'Holder' => 'CarHolder',*/
);
static $has_many = array(
'Holders' => 'CarHolder'
);
function getCMSFields_forPopup() {$f = new FieldSet();
$myholder = $this->Brand()->ID;
$oData = DataObject::get("CarBrand", "ID = '$myholder'");
if ($oData) {
$CategoriesSource = $oData->toDropDownMap('ID','Title');
$dropdown = new DropdownField('BrandID', 'Brand', $CategoriesSource, $this->BrandID);
$f->push ( $dropdown );
}
// record detail fields
$f->push( new TextField('Title','Title') );return $f;
}
}
?><?php
class CarBrand extends DataObject {
static $db = array(
'Title' => 'Varchar(255)'
);
static $has_many = array(
'CarModels' => 'CarModel',
'Holders' => 'CarHolder'
);
/*static $belongs_many_many = array (
'Holders' => 'CarHolder'
); */
function getCMSFields_forPopup() {
$f = new FieldSet();
$f->push( new TextField( 'Title', 'Title') );
return $f;
}function getDropdownSummary() {
$String = $this->Title . " (" . $this->CarModels()->TotalItems() . ")";
return $String;
}
}
?><?php
class CarHolder extends Page {
static $db = array(
//car properties:
"Car_Vitesse" =>"Enum('Manual,Automatic')",
"Car_Category"=>"Enum('Sports/Luxury,Berling/Brake,Cabriolet,4x4/Off Roaders,Multi-purpose,Fun/2 Wheels')",
"Car_Version"=>"Text",
"Car_Year"=>"Text",
"Car_Mileage"=>"Text",
"Car_Price"=>"Text",
"Car_Description"=>"Text",
"Car_Contact_Name"=>"Text",
"Car_Contact_Email"=>"Text",
"Car_Contact_Phone"=>"Text",
"Car_Agent"=>"Text",
"Car_DateIn"=>"Date",
"Car_DateOut"=>"Date"
);
static $has_many= array(
'Photos' => 'CarImageResource'
);
static $has_one = array (
'Model' => 'CarModel',
'Brand' => 'CarBrand'
);public function getCMSFields() {
$f = parent::getCMSFields();
$mng_records = new HasOneComplexTableField(
$this,
'Brand',
'CarBrand',
array(
'Title' => 'Title',
),
'getCMSFields_forPopup'
);$f->addFieldToTab("Root.Content.Brand",$mng_records);
if($this->BrandID)
{
$bid = $this->BrandID;
$mng_records = new HasOneComplexTableField(
$this,
'Model',
'CarModel',
array(
'Title' => 'Title',
'Brand.Title' => 'Brand',
),
'getCMSFields_forPopup',
array("BrandID='$bid'")
);
$f->addFieldToTab("Root.Content.Model",$mng_records);
}
$vitess = new DropdownField('Vitesse','Vitesse',singleton('CarHolder')->dbObject('Car_Vitesse')->enumValues());
$category = new DropdownField('Category','Category',singleton('CarHolder')->dbObject('Car_Category')->enumValues());
$f->addFieldsToTab('Root.Content.Main',array($vitess,$category));
$manager = new ImageDataObjectManager(
$this, // Controller
'Photos', // Source name
'CarImageResource', // Source class
'Attachment', // File name on DataObject
array(
//'http_url' => 'http_url',
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Photos",$manager);
return $f;
}}
class CarHolder_Controller extends Page_Controller {
}
?>
To manage the models and brands from a single interface (instead from doing it when you add a new CarHolder Page)
<?php
class CarModelAdmin extends ModelAdmin {
static $managed_models = array(
'CarBrand',
'CarModel'
);static $url_segment = 'cars';
static $menu_title = 'Cars';
}
?>
| 802 Views | ||
|
Page:
1
|
Go to Top |

