Skip to content

Commit

Permalink
feat: 增加linkColumn和DetailViewAction
Browse files Browse the repository at this point in the history
  • Loading branch information
krissss committed Sep 27, 2018
1 parent 4d9c202 commit c985ea6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
72 changes: 72 additions & 0 deletions actions/web/crud/DetailViewAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace kriss\actions\web\crud;

use kriss\widgets\SimpleAjaxView;
use kriss\widgets\SimpleBoxView;
use yii\base\InvalidConfigException;
use yii\widgets\DetailView;

/**
* $actions['detail'] = [
* 'class' => DetailViewAction::class,
* 'modelClass' => Store::class,
* 'attributes' => ['id', 'name'],
* ];
*
* @since 2.1.3
* @see LinkColumn
*/
class DetailViewAction extends AbstractModelAction
{
/**
* attributes in DetailView
* 不设置将显示全部
* @see DetailView::$attributes
* @var array
*/
public $attributes = [];
/**
* 当传递参数 type 时,该参数起作用,attributes 将使用此处的配置
* [[type => attributes]]
* @var array
*/
public $mapAttributes;
/**
* @var DetailView|string
*/
public $detailViewClass = DetailView::class;
/**
* @see SimpleAjaxView
* @see SimpleBoxView
* @var array
*/
public $wrapConfig = [];

public function run($id, $type = null)
{
if ($type !== null) {
if (isset($this->mapAttributes[$type])) {
$attributes = $this->mapAttributes[$type];
} else {
throw new InvalidConfigException('mapAttributes has no index named: ' . $type);
}
} else {
$attributes = $this->attributes;
}
$content = ($this->detailViewClass)::widget([
'model' => $this->findModel($id, $this->controller),
'attributes' => $attributes ?: null,
]);

if ($this->isAjax) {
SimpleAjaxView::begin($this->wrapConfig);
echo $content;
SimpleAjaxView::end();
} else {
SimpleBoxView::begin($this->wrapConfig);
echo $content;
SimpleBoxView::end();
}
}
}
2 changes: 2 additions & 0 deletions widgets/ExportMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* 'dataProvider' => $dataProvider,
* 'columns' => ExportMenuHelper::transColumns($columns),
* ]);
*
* @since 2.1.2
*/
class ExportMenu extends Widget
{
Expand Down
3 changes: 3 additions & 0 deletions widgets/ExportMenuDataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace kriss\widgets;

/**
* @since 2.1.2
*/
class ExportMenuDataColumn extends \yii2tech\csvgrid\DataColumn
{
public function renderDataCellContent($model, $key, $index)
Expand Down
5 changes: 5 additions & 0 deletions widgets/ExportMenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use yii\grid\DataColumn;
use yii\grid\SerialColumn;

/**
* @since 2.1.2
*/
class ExportMenuHelper
{
public static function transColumns($columns)
Expand All @@ -30,6 +33,8 @@ public static function transColumns($columns)
$result[] = static::transSimpleColumn($column);
} elseif (static::isMatchClass($column['class'], SerialColumn::class)) {
$result[] = static::transSerialColumn($column);
} elseif (static::isMatchClass($column['class'], LinkColumn::class)) {
$result[] = static::transSimpleColumn($column);
} elseif (static::isMatchClass($column['class'], DataColumn::class)) {
// 此项必须放在最后
$result[] = static::transSimpleColumn($column);
Expand Down
44 changes: 44 additions & 0 deletions widgets/LinkColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace kriss\widgets;

use yii\base\InvalidConfigException;
use yii\helpers\Html;

/**
* [
* 'class' => LinkColumn::class,
* 'label' => '所属门店',
* 'attribute' => 'store.name',
* 'linkUrl' => function (Person $model) {
* return ['store/detail', 'id' => $model->store_id];
* },
* 'linkOptions' => ['class' => 'show_ajax_modal'],
* ],
*
* @since 2.1.3
* @see DetailViewAction
*/
class LinkColumn extends DataColumn
{
public $linkUrl;

public $linkOptions = [];

public function init()
{
parent::init();
if (!$this->linkUrl) {
throw new InvalidConfigException('must config linkUrl');
}
}

protected function renderDataCellContent($model, $key, $index)
{
$value = parent::renderDataCellContent($model, $key, $index);

$linkUrl = is_callable($this->linkUrl) ? call_user_func($this->linkUrl, $model, $key, $index, $this) : $this->linkUrl;

return Html::a($value, $linkUrl, $this->linkOptions);
}
}

0 comments on commit c985ea6

Please sign in to comment.