mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #257 from halkyon/gridfield_export
GridFieldExportButton changes to support customisation of which fields to export
This commit is contained in:
commit
3f78aab8d8
@ -115,12 +115,14 @@ abstract class ModelAdmin extends LeftAndMain {
|
||||
|
||||
function getEditForm($id = null) {
|
||||
$list = $this->getList();
|
||||
$exportButton = new GridFieldExportButton();
|
||||
$exportButton->setExportColumns($this->getExportFields());
|
||||
$listField = Object::create('GridField',
|
||||
$this->modelClass,
|
||||
false,
|
||||
$list,
|
||||
$fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
|
||||
->addComponent(new GridFieldExportButton())
|
||||
->addComponent($exportButton)
|
||||
->removeComponentsByType('GridFieldFilterHeader')
|
||||
);
|
||||
|
||||
@ -145,6 +147,16 @@ abstract class ModelAdmin extends LeftAndMain {
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define which fields are used in the {@link getEditForm} GridField export.
|
||||
* By default, it uses the summary fields from the model definition.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExportFields() {
|
||||
return singleton($this->modelClass)->summaryFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SearchContext
|
||||
*/
|
||||
|
@ -6,6 +6,7 @@ class ModelAdminTest extends FunctionalTest {
|
||||
protected $extraDataObjects = array(
|
||||
'ModelAdminTest_Admin',
|
||||
'ModelAdminTest_Contact',
|
||||
'ModelAdminTest_Player'
|
||||
);
|
||||
|
||||
function testModelAdminOpens() {
|
||||
@ -14,6 +15,22 @@ class ModelAdminTest extends FunctionalTest {
|
||||
$this->assertTrue((bool)Permission::check("ADMIN"));
|
||||
$this->assertEquals(200, $this->get('ModelAdminTest_Admin')->getStatusCode());
|
||||
}
|
||||
|
||||
function testExportFieldsDefaultIsSummaryFields() {
|
||||
$admin = new ModelAdminTest_Admin();
|
||||
$admin->modelClass = 'ModelAdminTest_Contact';
|
||||
$this->assertEquals($admin->getExportFields(), singleton('ModelAdminTest_Contact')->summaryFields());
|
||||
}
|
||||
|
||||
function testExportFieldsOverloadedMethod() {
|
||||
$admin = new ModelAdminTest_PlayerAdmin();
|
||||
$admin->modelClass = 'ModelAdminTest_Player';
|
||||
$this->assertEquals($admin->getExportFields(), array(
|
||||
'Name' => 'Name',
|
||||
'Position' => 'Position'
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ModelAdminTest_Admin extends ModelAdmin implements TestOnly {
|
||||
@ -23,10 +40,36 @@ class ModelAdminTest_Admin extends ModelAdmin implements TestOnly {
|
||||
'ModelAdminTest_Contact',
|
||||
);
|
||||
}
|
||||
class ModelAdminTest_PlayerAdmin extends ModelAdmin implements TestOnly {
|
||||
static $url_segment = 'testadmin';
|
||||
|
||||
public static $managed_models = array(
|
||||
'ModelAdminTest_Player'
|
||||
);
|
||||
|
||||
public function getExportFields() {
|
||||
return array(
|
||||
'Name' => 'Name',
|
||||
'Position' => 'Position'
|
||||
);
|
||||
}
|
||||
}
|
||||
class ModelAdminTest_Contact extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
"Name" => "Varchar",
|
||||
"Phone" => "Varchar",
|
||||
'Name' => 'Varchar',
|
||||
'Phone' => 'Varchar',
|
||||
);
|
||||
static $summary_fields = array(
|
||||
'Name' => 'Name',
|
||||
'Phone' => 'Phone'
|
||||
);
|
||||
}
|
||||
class ModelAdminTest_Player extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
'Name' => 'Varchar',
|
||||
'Position' => 'Varchar',
|
||||
);
|
||||
static $has_one = array(
|
||||
'Contact' => 'ModelAdminTest_Contact'
|
||||
);
|
||||
}
|
@ -167,26 +167,22 @@ Export is also available, although at the moment only to the CSV format,
|
||||
through a button at the end of a results list. You can also export search results.
|
||||
It is handled through the `[api:GridFieldExportButton]` component.
|
||||
|
||||
To customize the exported columns, override the edit form implementation in your `ModelAdmin`:
|
||||
To customize the exported columns, create a new method called `getExportFields` in your `ModelAdmin`:
|
||||
|
||||
:::php
|
||||
class MyAdmin extends ModelAdmin {
|
||||
// ...
|
||||
function getEditForm($id = null) {
|
||||
$form = parent::getEditForm($id);
|
||||
if($this->modelClass == 'Product') {
|
||||
$grid = $form->Fields()->fieldByName('Product');
|
||||
$grid->getConfig()->getComponentByType('GridFieldExportButton')
|
||||
->setExportColumns(array(
|
||||
// Excludes 'ProductCode' from the export
|
||||
function getExportFields() {
|
||||
return array(
|
||||
'Name' => 'Name',
|
||||
'Price' => 'Price'
|
||||
));
|
||||
}
|
||||
return $form;
|
||||
'ProductCode' => 'Product Code',
|
||||
'Category.Title' => 'Category'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Dot syntax support allows you to select a field on a related `has_one` object.
|
||||
|
||||
## Extending existing ModelAdmins
|
||||
|
||||
Sometimes you'll work with ModelAdmins from other modules, e.g. the product management
|
||||
@ -195,6 +191,7 @@ also another tool at your disposal: The `[api:Extension]` API.
|
||||
|
||||
:::php
|
||||
class MyAdminExtension extends Extension {
|
||||
// ...
|
||||
function updateEditForm(&$form) {
|
||||
$form->Fields()->push(/* ... */)
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
|
||||
foreach($items as $item) {
|
||||
$columnData = array();
|
||||
foreach($csvColumns as $columnSource => $columnHeader) {
|
||||
$value = $item->$columnSource;
|
||||
$value = $item->relField($columnSource);
|
||||
$value = str_replace(array("\r", "\n"), "\n", $value);
|
||||
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user