Yii2实现增删改查后留在当前页的方法详解
前言
实现增删改查操作成功后仍留在当前页,能给用户一个良好的体验。但是 yii2 框架本身是没有在增删改查操作成功后仍留在当前页的效果的,要实现这样的一个效果得自己写。我的原则是不动核心代码,始终坚持自己的原则,现实现了我把它分享出来。殊途同归,如有更好的实现方法,欢迎交流。
需求分析
一、实现增删改查后操作成功后仍然留在当前页面。
1、链接的效果图
封装代码
共有两个文件actioncolumn.php和helper.php1、actioncolumn.php文件
<?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 .= '</div>'; return $data; } }
2、helper.php文件
<?php use yii; class helper { private static $returnurl; public static $returnurlwithouthistory = false; /** * @param int $depth * @return string */ public static function getreturnurl() { if (is_null(self::$returnurl)) { $url = parse_url(yii::$app->request->url); $returnurlparams = []; if (isset($url['query'])) { $parts = explode('&', $url['query']); foreach ($parts as $part) { $pieces = explode('=', $part); if (static::$returnurlwithouthistory && count($pieces) == 2 && $pieces[0] === 'returnurl') { continue; } if (count($pieces) == 2 && strlen($pieces[1]) > 0) { $returnurlparams[] = $part; } } } if (count($returnurlparams) > 0) { self::$returnurl = $url['path'] . '?' . implode('&', $returnurlparams); } else { self::$returnurl = $url['path']; } } return self::$returnurl; } }
视图调用
1、直接调用,把yii2自带的['class' => 'yiigridactioncolumn']
换成我们新写的['class' => 'common\components\actioncolumn']
。
2、如直接调用不能满足你的要求可以自定义链接,自定义链接的写法如下:
[ 'class' => 'common\components\actioncolumn', 'urlcreator' => function($action, $model, $key, $index) use ($id) { //自定义链接传的参数 $params = [ $action, 'option_id' => $model->option_id, 'id' => $id, ]; $params['returnurl'] = common\components\helper::getreturnurl(); return yii\helpers\url::toroute($params); }, 'buttons' => [ [ 'url' =>'view', 'class' => 'btn btn-success btn-xs', 'label' => yii::t('yii', 'view'), 'appendreturnurl' => false,//是否保留当前url,默认为true 'url_append' => '', 'keyparam' => 'id', //是否传id,不传设置null ], [ 'url' => 'update', 'class' => 'btn btn-primary btn-xs btn-sm', 'label' => yii::t('yii', 'update'), 'appendreturnurl' => true,//是否保留当前url,默认为true 'url_append' => '', 'keyparam' => 'id', //是否传id,不传设置null ], [ 'url' => 'delete', 'class' => 'btn btn-danger btn-xs btn-sm', 'label' => yii::t('yii', 'delete'), 'options' => [ 'data-action' => 'delete', ], 'appendreturnurl' => true,//是否保留当前url,默认为true 'url_append' => '', 'keyparam' => 'id', //是否传id,不传设置null ], ], ],
3、新增的话,这么引用<?= html::a(yii::t('yii', 'create'), ['create','returnurl' => helper::getreturnurl()], ['class' => 'btn btn-success']) ?>
。
控制器逻辑
1、用get获取returnurl,代码:$returnurl = yii::$app->request->get('returnurl');
。
2、跳转到的url:return $this->redirect($returnurl);
。
分析总结
1、此方法的好处是不动核心代码,调用方式保留了yii2自带的方式。
2、不足之处是在自定义链接的时候需要把每一个操作update、view、delete都写出来,不能用这种'template' => '{view}{update}{delete}
'简单且看着舒服能根据需求去写了。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。