Jump to:

10930 Posts in 2613 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » ModelAdmin & CSV Export issue?

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 4349 Views
  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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;

       }
    }

  • James Bolitho
    Avatar
    Community Member
    33 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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

  • RobboDev
    Avatar
    Community Member
    20 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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.

  • Phill
    Avatar
    Community Member
    75 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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') {

    }

  • svandragt
    Avatar
    Community Member
    44 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

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

  • RobboDev
    Avatar
    Community Member
    20 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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

  • Terrsance
    Avatar
    Community Member
    1 Post

    Re: ModelAdmin & CSV Export issue? Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: ModelAdmin & CSV Export issue? Link to this post

    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?

    4349 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.