mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 13:58:24 +08:00
Merge branch '9198-buttons-visible-in-action-column' of https://github.com/fornit1917/yii2 into fornit1917-9198-buttons-visible-in-action-column
This commit is contained in:
@@ -180,6 +180,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #9072: `yii\web\ErrorAction` displays 404 error instead of blank page on direct access (klimov-paul)
|
- Enh #9072: `yii\web\ErrorAction` displays 404 error instead of blank page on direct access (klimov-paul)
|
||||||
- Enh #9149: Print directory migrationPath in a `yii migrate` command error. (RusAlex)
|
- Enh #9149: Print directory migrationPath in a `yii migrate` command error. (RusAlex)
|
||||||
- Enh #9177: Added password hash cost setting to Security component (freezy-sk)
|
- Enh #9177: Added password hash cost setting to Security component (freezy-sk)
|
||||||
|
- Enh #9198: Added parameter visibleButtons for `yii\grid\ActionColimn` (fornit1917)
|
||||||
- Enh #9239: Better handling of `Json` errors (grzegorzkurtyka, samdark)
|
- Enh #9239: Better handling of `Json` errors (grzegorzkurtyka, samdark)
|
||||||
- Enh #9246: Added `yii\web\UrlRule::getParamRules()` (df2)
|
- Enh #9246: Added `yii\web\UrlRule::getParamRules()` (df2)
|
||||||
- Enh #9249: Added `hashCallback` in `yii\web\AssetManager` to allow custom hash generation for asset directory (petrabarus)
|
- Enh #9249: Added `hashCallback` in `yii\web\AssetManager` to allow custom hash generation for asset directory (petrabarus)
|
||||||
|
|||||||
@@ -85,6 +85,28 @@ class ActionColumn extends Column
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
public $buttons = [];
|
public $buttons = [];
|
||||||
|
/** @var array visibility conditions for each button. The array keys are the button names (without curly brackets),
|
||||||
|
* and the values are are the boolean true/false or the anonymous function. The button will be shown,
|
||||||
|
* when its name is not specified in this array.
|
||||||
|
* The callbacks must use the following signature:
|
||||||
|
*
|
||||||
|
* ```php
|
||||||
|
* [
|
||||||
|
* 'update' => function ($model, $key, $index) {
|
||||||
|
* return $model->status === 'editable';
|
||||||
|
* },
|
||||||
|
* ],
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Or you can pass a boolean expression:
|
||||||
|
*
|
||||||
|
* ```php
|
||||||
|
* [
|
||||||
|
* 'update' => \Yii::$app->user->can('update'),
|
||||||
|
* ],
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
public $visibleButtons = [];
|
||||||
/**
|
/**
|
||||||
* @var callable a callback that creates a button URL using the specified model information.
|
* @var callable a callback that creates a button URL using the specified model information.
|
||||||
* The signature of the callback should be the same as that of [[createUrl()]].
|
* The signature of the callback should be the same as that of [[createUrl()]].
|
||||||
@@ -174,9 +196,17 @@ class ActionColumn extends Column
|
|||||||
{
|
{
|
||||||
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
|
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
|
||||||
$name = $matches[1];
|
$name = $matches[1];
|
||||||
if (isset($this->buttons[$name])) {
|
|
||||||
$url = $this->createUrl($name, $model, $key, $index);
|
|
||||||
|
|
||||||
|
if (isset($this->visibleButtons[$name])) {
|
||||||
|
$isVisible = $this->visibleButtons[$name] instanceof \Closure
|
||||||
|
? call_user_func($this->visibleButtons[$name], $model, $key, $index)
|
||||||
|
: $this->visibleButtons[$name];
|
||||||
|
} else {
|
||||||
|
$isVisible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isVisible && isset($this->buttons[$name])) {
|
||||||
|
$url = $this->createUrl($name, $model, $key, $index);
|
||||||
return call_user_func($this->buttons[$name], $url, $model, $key);
|
return call_user_func($this->buttons[$name], $url, $model, $key);
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
|
|||||||
63
tests/framework/grid/ActionColumnTest.php
Normal file
63
tests/framework/grid/ActionColumnTest.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace yiiunit\framework\grid;
|
||||||
|
|
||||||
|
|
||||||
|
use yii\base\Model;
|
||||||
|
use yii\grid\ActionColumn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vitaly S. <fornit1917@gmail.com>
|
||||||
|
*
|
||||||
|
* @group grid
|
||||||
|
*/
|
||||||
|
class ActionColumnTest extends \yiiunit\TestCase
|
||||||
|
{
|
||||||
|
public function testRenderDataCell()
|
||||||
|
{
|
||||||
|
$column = new ActionColumn();
|
||||||
|
$column->urlCreator = function($model, $key, $index) {
|
||||||
|
return 'http://test.com';
|
||||||
|
};
|
||||||
|
$column->template = '{update}';
|
||||||
|
$column->buttons = [
|
||||||
|
'update' => function($url, $model, $key) {
|
||||||
|
return 'update_button';
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
//test default visible button
|
||||||
|
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
|
||||||
|
$this->assertContains('update_button', $columnContents);
|
||||||
|
|
||||||
|
//test visible button
|
||||||
|
$column->visibleButtons = [
|
||||||
|
'update' => true
|
||||||
|
];
|
||||||
|
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
|
||||||
|
$this->assertContains('update_button', $columnContents);
|
||||||
|
|
||||||
|
//test visible button (condition is callback)
|
||||||
|
$column->visibleButtons = [
|
||||||
|
'update' => function($model, $key, $index){return $model['id'] == 1;}
|
||||||
|
];
|
||||||
|
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
|
||||||
|
$this->assertContains('update_button', $columnContents);
|
||||||
|
|
||||||
|
//test invisible button
|
||||||
|
$column->visibleButtons = [
|
||||||
|
'update' => false
|
||||||
|
];
|
||||||
|
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
|
||||||
|
$this->assertNotContains('update_button', $columnContents);
|
||||||
|
|
||||||
|
//test invisible button (condition is callback)
|
||||||
|
$column->visibleButtons = [
|
||||||
|
'update' => function($model, $key, $index){return $model['id'] != 1;}
|
||||||
|
];
|
||||||
|
$columnContents = $column->renderDataCell(['id' => 1], 1, 0);
|
||||||
|
$this->assertNotContains('update_button', $columnContents);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user