Skip to content

Commit

Permalink
fix: fixed export data to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
killer67 authored Aug 4, 2022
1 parent f3a4cfc commit 8afe29f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
44 changes: 24 additions & 20 deletions src/behaviors/ExportTableBehavior.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace kak\widgets\grid\behaviors;

use kak\widgets\grid\interfaces\ExportType;
use yii\base\Behavior;
use yii\bootstrap\ButtonDropdown;
Expand All @@ -10,29 +12,31 @@
* @package kak\widgets\grid\behaviors
*
* ```php
'behaviors' => [
[
'class' => \kak\widgets\grid\behaviors\ToolBarBehavior::className(),
'toolbar' => [
[
// attach ExportTableBehavior placeholder
'content' => '<div class="form-group">'
. '<div class="col-md-2 col-sm-4">{pagesize}</div>'
. '<div class="col-md-2">{exporttable}</div>'
. '</div>'
]
]
]
],[
'class' => \kak\widgets\grid\behaviors\ExportTableBehavior::className(),
'behaviors' => [
[
'class' => \kak\widgets\grid\behaviors\ToolBarBehavior::className(),
'toolbar' => [
[
// attach ExportTableBehavior placeholder
'content' => '<div class="form-group">'
. '<div class="col-md-2 col-sm-4">{pagesize}</div>'
. '<div class="col-md-2">{exporttable}</div>'
. '</div>'
]
]
]
],[
'class' => \kak\widgets\grid\behaviors\ExportTableBehavior::className(),
]
],
]
],
* ```
*/
class ExportTableBehavior extends Behavior
{
public $dropDownOptions = [];
public $dropDownOptions = [];

public $columnHeader = true;

/**
* @var array is empty list then export all columns
Expand All @@ -52,8 +56,7 @@ class ExportTableBehavior extends Behavior
*/
public function renderExportTable()
{
$output = $this->initButtonDropdown();
return $output;
return $this->initButtonDropdown();
}

/**
Expand Down Expand Up @@ -86,6 +89,7 @@ protected function process()
$service->type = $type;
$service->limit = $this->limit;
$service->exportColumns = $this->exportColumns;
$service->columnHeader = $this->columnHeader;
$service->run();
}
}
Expand Down
32 changes: 18 additions & 14 deletions src/mappers/ColumnMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class ColumnMapper
{

private $columns = [];
private $exportColumns = [];
private $removeHtml = true;
Expand All @@ -26,20 +25,21 @@ public function __construct($columns, $exportColumns, $removeHtml, $columnHeader
$this->columnHeader = $columnHeader;
}


public function getHeaders()
{
$headers = [];
/** @var DataColumn $column */
foreach ($this->columns as $column) {
foreach ($this->columns as $key => $column) {
if ($this->isColumnExportable($column)) {
$headers[] = $header = $this->columnHeader ? $this->getColumnHeader($column): $column->attribute;
$attributeKey = $column->attribute ?? $key;
$value = $this->columnHeader ? $this->getColumnHeader($column) : $attributeKey;
$headers[] = $value;
}
}

return $headers;
}


/**
* @param $model
* @param $index
Expand All @@ -48,19 +48,18 @@ public function getHeaders()
public function map($model, $index)
{
$row = [];
foreach ($this->columns as $column) {
foreach ($this->columns as $key => $column) {
if ($this->isColumnExportable($column)) {
/** @var DataColumn $column */
$key = $model instanceof ActiveRecordInterface
$modelKey = $model instanceof ActiveRecordInterface
? $model->getPrimaryKey()
: isset($model[$column->attribute]) ? $model[$column->attribute]: null;
: $model[$column->attribute] ?? $model[$column->attribute] ?? $key ?? null;

$value = $this->getColumnValue($column, $model, $key, $index);
$header = $this->columnHeader ? $this->getColumnHeader($column): $column->attribute;
$value = $this->getColumnValue($column, $model, $modelKey, $index);
$header = $column->attribute ?? $key;
$row[$header] = $value;
}
}

return $row;
}

Expand Down Expand Up @@ -93,14 +92,19 @@ protected function getColumnHeader($column)
*/
protected function isColumnExportable($column)
{
if ($column instanceof ActionColumn || $column instanceof CheckboxColumn || ($column instanceof DataColumn && $column->export === false)) {
if (
$column instanceof ActionColumn ||
$column instanceof CheckboxColumn ||
($column instanceof DataColumn && $column->export === false)
) {
return false;
}

if (!empty($this->exportColumns)) {
return in_array($column->attribute, $this->exportColumns);
}

return true;
}


}
}
2 changes: 1 addition & 1 deletion src/services/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function run()
/** @var BaseDataProvider $dataProvider */
$dataProvider = $this->grid->dataProvider;

$mapper = new ColumnMapper($this->grid->columns, $this->exportColumns, $this->columnRemoveHtml, $this->columnHeader);
$mapper = new ColumnMapper($this->grid->columns, $this->exportColumns, $this->columnRemoveHtml, $this->columnHeader, $this->type);
$source = new SourceIterator(new DataProviderBatchIterator($dataProvider, $mapper, $this->limit));

$writer->openToBrowser($this->getFileName());
Expand Down

0 comments on commit 8afe29f

Please sign in to comment.