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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

ModelAdmin & CSV Export issue?


Go to End


16 Posts   9021 Views

Avatar
martimiz

Forum Moderator, 1391 Posts

27 October 2011 at 9:00am

Edited: 27/10/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;

	}
}

Avatar
James Bolitho

Community Member, 33 Posts

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

Avatar
Rob Clarkson

Community Member, 26 Posts

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.

Avatar
Phill

Community Member, 81 Posts

3 February 2012 at 6:38am

Edited: 03/02/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') {

}

Avatar
svandragt

Community Member, 44 Posts

22 July 2012 at 8:35pm

Edited: 22/07/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#methodgetExportFields

example:
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;
}
}

Avatar
Rob Clarkson

Community Member, 26 Posts

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

Avatar
Terrsance

Community Member, 1 Post

12 April 2013 at 6:52pm

Edited: 12/04/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?

Avatar
martimiz

Forum Moderator, 1391 Posts

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?

Go to Top