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.

All other Modules

Discuss all other Modules here.

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

ModelAdmin & CSV Export issue?


Reply

16 Posts   4511 Views

Avatar
martimiz

27 October 2011 at 9:00am (Last edited: 27 October 2011 9:03am), Forum Moderator, 1091 Posts

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

27 October 2011 at 9:13am Community Member, 33 Posts

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
RobboDev

23 January 2012 at 5:30pm Community Member, 21 Posts

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

3 February 2012 at 6:38am (Last edited: 3 February 2012 6:38am), Community Member, 75 Posts

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

22 July 2012 at 8:35pm (Last edited: 22 July 2012 8:52pm), Community Member, 44 Posts

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
RobboDev

20 March 2013 at 5:10pm Community Member, 21 Posts

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

12 April 2013 at 6:52pm (Last edited: 12 April 2013 6:54pm), Community Member, 1 Post

This generates the error "'ascii' codec can't encode characters in position ...". How can this be extended to handle UTF-8 encoding?

Avatar
martimiz

12 April 2013 at 8:42pm Forum Moderator, 1091 Posts

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?