10387 Posts in 2199 Topics by 1712 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2256 Views |
-
ModelAdmin $searchable_fields $has_one as dropdown

29 January 2010 at 6:02pm
Suppose we are following this tutorial on ModelAdmin and we changed the following to:
Product.php
static $searchable_fields = array(
'Name' => array('title'=>'Name'),
'ProductCode' => array('title'=>'Product Code'),
'Category.Name' => array('title'=>'Product Group') // add category name to searchable fields
);Great we can search the category names with a text field, seeing as this is a $has_one relationship wouldn't it make sense to have a dropdown instead so you can simply select a Category from the list?
With that being said it really doesn't look like ModelAdmin supports this without overriding ModelAdmin's SearchForm method is that correct?
Has anyone done this if please share, or if there an easier way please tell
-
Re: ModelAdmin $searchable_fields $has_one as dropdown

2 February 2010 at 4:21pm
Okay another nasty jquery hack but way faster than overloading/subclassing ModelAdmin:
ProductAdmin.php
class ProductAdmin extends ModelAdmin {
...
function groups()
{
$groups = DataObject::get('ProductGroup');
$out = array();
foreach($groups as $group) {
array_push($out, $group->toMap());
}
return json_encode($out);
}
}ProductAdmin.js
// groups dropdown
$.getJSON("admin/products/groups",
function(data){
var Options = '<option></option>';
$.each(data, function(){
Options = Options + '<option value="'+this.ProductGroup+'">'+this.ProductGroup+'</option>';
});
$('#Form_SearchForm_Product_ProductGroups__ProductGroup').replaceWith('<select name="ProductGroups__ProductGroup" id="Form_SearchForm_Product_ProductGroups__ProductGroup" class="text">'+Options+'</select>');
});Just remember the above dropdown needs the correct ID and NAME attributes from your ModelAdmin generated search forms.
-
Re: ModelAdmin $searchable_fields $has_one as dropdown

26 May 2010 at 2:00am
Thank you very much, this helped me too.
Btw, before searching through the forums, I tried to just add the ID of a many_many associated class to the $searchable_fields variable, e.g.:
class Foo extends DataObject {
$many_many = array(
"Channels" => "ChannelPage"
);static $searchable_fields = array(
"Channels.ID"
);}
Using this code, a dropdown box with valid options was displayed in ModelAdmin. Hurray! Seems like it's designed to work that way.
Unfortunately, an error occured after clicking on the search button: The generated SQL was invalid ("Unknown column 'ChannelPage.ID' in 'where clause').
IMHO, it seems like there's a bug in the SQL generation. The searchable_fields are not translated to a where clause correctly. Actually, the "Channels" role should be translated to "Foo_Channels" instead of "ChannelPage", and the ID property should be translated to "ChannelPageID" column. So the column identifier in the where clause should read 'Foo_Channels.ChannelPageID' instead of 'ChannelPage.ID'.
Can anyone confirm that?
-
Re: ModelAdmin $searchable_fields $has_one as dropdown

4 February 2011 at 1:45am
I am using version 2.4.3 and
static $searchable_fields = array(
'ArticleTypeID' =>array( 'title' => 'Article Type' )
);seems to create a drop down as expected but the first option is a blank option. I can set a default value to pre-select and option. I am trying to remove the first blank entry or change the text to say 'Please select'
| 2256 Views | ||
|
Page:
1
|
Go to Top |


