-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |