Detailed explanation of the method of staying on the current page after adding, deleting, modifying and checking in Yii2

韦小宝
Release: 2023-03-17 16:58:01
Original
1665 people have browsed it

ImplementationAdd, Delete, Modify and CheckIt will remain on the current page after the operation is successful, which can give the user a good experience. However, the Yii2 framework itself does not have the effect of remaining on the current page after theadd, delete, modify,operation is successful. To achieve such an effect, you have to write it yourself. My principle is not to leave the core code alone and always stick to my own principles. Now that I have achieved it, I will share it. Different paths lead to the same goal. If there is a better way to implementAdd, Delete, Modify and Check, please feel free to communicate.

Encapsulation code

There are two files ActionColumn.php and Helper.php

1 , ActionColumn.php file

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 .= '
'; return $data; } }
Copy after login

2, Helper.php file

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; } }
Copy after login

View call

1. Call it directly and replace the ['class' => 'yiigridActionColumn'] that comes with Yii2 with the new one we wrote ['class' => 'common\components\ActionColumn'].

2. If the direct call cannot meet your requirements, you can customize the link. The custom link is written as follows:

[ '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 ], ], ],
Copy after login

3. If you add a new one, quote it like this:

 Helper::getReturnUrl()], ['class' => 'btn btn-success']) ?> 。
Copy after login

ControllerLogic

1. Use get to get returnUrl, code: $returnUrl = Yii::$ app->request->get('returnUrl'); .

2. The URL to jump to: return $this->redirect($returnUrl);.

Analysis Summary

1. The advantage of this method is that it does not leave the core code, and the calling method retains the Yii2 built-in method.

2. The disadvantage is that when customizing the link, you need to write out each operation update, view, and delete. You cannot use this kind of 'template' => '{view}{ update}{delete}' is simple and comfortable to look at. You can write it according to your needs.

Okay, that’s the entire content of this article. I hope the content of this article can be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate.

The above is a detailed explanation of the method of leaving the current page after adding, deleting, modifying and checking in Yii2. Welcome to discuss and exchange issues inPHP Chinese Community!

Related recommendations:

Detailed explanation of the registration and creation methods of components in Yii2

How Yii2 uses camel case naming to access controller instances

Implementation code for the QR code generation function of Yii2.0 framework

How to implement the automatic login and login and exit functions of Yii2 framework

The above is the detailed content of Detailed explanation of the method of staying on the current page after adding, deleting, modifying and checking in Yii2. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!