php保留在当前页面,Yii2实现增删改查后留在当前页的方法详解

前言

实现增删改查操作成功后仍留在当前页,能给用户一个良好的体验。但是 Yii2 框架本身是没有在增删改查操作成功后仍留在当前页的效果的,要实现这样的一个效果得自己写。我的原则是不动核心代码,始终坚持自己的原则,现实现了我把它分享出来。殊途同归,如有更好的实现方法,欢迎交流。

需求分析

一、实现增删改查后操作成功后仍然留在当前页面。

1、链接的效果图

7d097ba6c28c1b521c32d773c9e2d676.png

封装代码

共有两个文件ActionColumn.php和Helper.php1、ActionColumn.php文件

use Closure;

use kartik\icons\Icon;

use Yii;

use yii\grid\Column;

use yii\helpers\ArrayHelper;

use yii\helpers\Html;

use yii\helpers\Url;

use common\components\Helper;

/*

*重写ActionColumn

*/

class ActionColumn extends Column

{

public $buttons;

private $defaultButtons = [];

private $callbackButtons;

public $controller;

public $urlCreator;

public $url_append = '';

public $appendReturnUrl = true; //默认为true,返回当前链接

public function init()

{

parent::init();

$this->defaultButtons = [

[

'url' => 'view',

'icon' => 'eye',

'class' => 'btn btn-success btn-xs',

'label' => Yii::t('yii', 'View'),

'appendReturnUrl' => false,

'url_append' => '',

'keyParam' => 'id',//是否传id,不传设置null

],

[

'url' => 'update',

'icon' => 'pencil',

'class' => 'btn btn-primary btn-xs',

'label' => Yii::t('yii', 'Update'),

],

[

'url' => 'delete',

'icon' => 'trash-o',

'class' => 'btn btn-danger btn-xs',

'label' => Yii::t('yii', 'Delete'),

'options' => [

'data-action' => 'delete',

],

]

];

if (null === $this->buttons) {

$this->buttons = $this->defaultButtons;

} elseif ($this->buttons instanceof Closure) {

$this->callbackButtons = $this->buttons;

}

}

public function createUrl(

$action,

$model,

$key,

$index,

$appendReturnUrl = null,

$url_append = null,

$keyParam = 'id',

$attrs = []

) {

if ($this->urlCreator instanceof Closure) {

return call_user_func($this->urlCreator, $action, $model, $key, $index);

} else {

$params = [];

if (is_array($key)) {

$params = $key;

} else {

if (is_null($keyParam) === false) {

$params = [$keyParam => (string)$key];

}

}

$params[0] = $this->controller ? $this->controller . '/' . $action : $action;

foreach ($attrs as $attrName) {

if ($attrName === 'model') {

$params['model'] = $model;

} elseif ($attrName === 'mainCategory.category_group_id' && $model->getMainCategory()) {

$params['category_group_id'] = $model->getMainCategory()->category_group_id;

} else {

$params[$attrName] = $model->getAttribute($attrName);

}

}

if (is_null($appendReturnUrl) === true) {

$appendReturnUrl = $this->appendReturnUrl;

}

if (is_null($url_append) === true) {

$url_append = $this->url_append;

}

if ($appendReturnUrl) {

$params['returnUrl'] = Helper::getReturnUrl();

}

return Url::toRoute($params) . $url_append;

}

}

protected function renderDataCellContent($model, $key, $index)

{

if ($this->callbackButtons instanceof Closure) {

$btns = call_user_func($this->callbackButtons, $model, $key, $index, $this);

if (null === $btns) {

$this->buttons = $this->defaultButtons;

} else {

$this->buttons = $btns;

}

}

$min_width = count($this->buttons) * 34; //34 is button-width

$data = Html::beginTag('div', ['class' => 'btn-group', 'style' => 'min-width: ' . $min_width . 'px']);

foreach ($this->buttons as $button) {

$appendReturnUrl = ArrayHelper::getValue($button, 'appendReturnUrl', $this->appendReturnUrl);

$url_append = ArrayHelper::getValue($button, 'url_append', $this->url_append);

$keyParam = ArrayHelper::getValue($button, 'keyParam', 'id');

$attrs = ArrayHelper::getValue($button, 'attrs', []);

Html::addCssClass($button, 'btn');

Html::addCssClass($button, 'btn-sm');

$buttonText = isset($button['text']) ? ' ' . $button['text'] : '';

$data .= Html::a(

$button['label'] . $buttonText,

$url = $this->createUrl(

$button['url'],

$model,

$key,

$index,

$appendReturnUrl,

$url_append,

$keyParam,

$attrs

),

ArrayHelper::merge(

isset($button['options']) ? $button['options'] : [],

[

//'data-pjax' => 0,

// 'data-action' => $button['url'],

'class' => $button['class'],

'title' => $button['label'],

]

)

) . ' ';

}

$data .= '

';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值