10375 Posts in 2190 Topics by 1707 members
| Go to End | ||
| Author | Topic: | 2906 Views |
-
Re: ModelAdmin & CSV Export issue?

27 October 2011 at 9:00am Last edited: 27 October 2011 9:03am
To do that you need a hook on the ModelAdmin 'Search' TableListField, so you can use its setFieldListCsv() method. One way to accomplish that is by extending the ModelAdmin_CollectionController class and subclassing its getResultsTable() method, that returns the TableListField.
So suppose you have a Product class like this:
class Product extends DataObject{
static $db = array (
'Name' => 'Varchar(255)',
'Price' => 'Varchar(255)',
'Code' => 'Varchar(255)'
);
static $summary_fields = array(
'Name'
);
...
}Now your ModelAdmin might look like this:
class ProductAdmin extends ModelAdmin {
public static $managed_models = array(
'Product'
);
static $url_segment = 'products';
static $menu_title = 'Products';// tell ModelAdmin to use your custom CollectionController
public static $collection_controller_class = "ProductAdmin_CollectionController";
}Next create the CollectionController like this:
class ProductAdmin_CollectionController extends ModelAdmin_CollectionController {
function getResultsTable($searchCriteria) {
$tf = parent::getResultsTable($searchCriteria);$tf->setFieldListCsv(array(
'Name' => 'Name',
'Price' => 'Price',
'Code' => 'code'
));
return $tf;}
} -
Re: ModelAdmin & CSV Export issue?

27 October 2011 at 9:13am
Hi Martimiz,
Great that has worked a treat. I wasn't sure how to implement the setFieldListCsv() method correctly in this case. Thanks for pointing me in correct direction.
Cheers,
Jim
-
Re: ModelAdmin & CSV Export issue?

23 January 2012 at 5:30pm
Had the same problem as you guys, found the solution here: http://www.balbuss.com/modeladmin-some-simple-snippets/
CSV Export for all fields - not just the summaryfields
By default the CSV export button in a TableListFieldwill only export the summaryfields. To change that behaviour you need to use its setFieldListCsv() method to set an array of the fields you want to export. Just like in the formatting/casting example above, you can extend the ModelAdmin's CollectionController class to get a hold of the TableListField:
class Product_CollectionController extends ModelAdmin_CollectionController {function getResultsTable($searchCriteria) {
$tf = parent::getResultsTable($searchCriteria);$tf->setFieldListCsv(array(
'Name' => 'Name',
'Price' => 'Price',
'Code' => 'code'
));return $tf;
}
}This will make ModelAdmin export the Product's fields Name, Price and Code to CSV.
-
Re: ModelAdmin & CSV Export issue?

3 February 2012 at 6:38am Last edited: 3 February 2012 6:38am
Thanks
The following was helpful
class Product_CollectionController extends ModelAdmin_CollectionController {
function getResultsTable($searchCriteria) {
$tf = parent::getResultsTable($searchCriteria);$tf->setFieldListCsv(array(
'Name' => 'Name',
'Price' => 'Price',
'Code' => 'code'
));return $tf;
}
}but i found that if you manage more than 1 dataobject in the admin you need to add a check like this to limit which dataobject it effects
if ($this->modelClass == 'Product') {
}
-
Re: ModelAdmin & CSV Export issue?

22 July 2012 at 8:35pm Last edited: 22 July 2012 8:52pm
In case anyone is looking for this still, see: ModelAdmin's getExportFields method which accepts an array of fields.
http://api.silverstripe.org/trunk/cms/core/ModelAdmin.html#methodgetExportFieldsexample:
class MyAdmin extends ModelAdmin {...
public function getExportFields() {
$parent_fields = parent::getExportFields();
$add_fields = array (
'Content' => 'Content',
'SourceURL' => 'SourceURL',
'URLSegment' => 'URLSegment',
);
$fields = array_merge ( $parent_fields, $add_fields);
return $fields;
}
} -
Re: ModelAdmin & CSV Export issue?

20 March 2013 at 5:10pm
Hi Guys,
What you need is on this page
http://www.balbuss.com/modeladmin-some-simple-snippets/go down to "CSV Export for all fields - not just the summaryfields"
Cheers
Rob -
Re: ModelAdmin & CSV Export issue?

12 April 2013 at 6:52pm Last edited: 12 April 2013 6:54pm
This generates the error "'ascii' codec can't encode characters in position ...". How can this be extended to handle UTF-8 encoding?
-
Re: ModelAdmin & CSV Export issue?

12 April 2013 at 8:42pm
Weird - I would have thought CSV export aready supports utf-8. What version of Silverstripe are you using? Could you post the complete error message?
| 2906 Views | ||
| Go to Top |




