How to add, delete, modify, and query data on the current page of Yii2: Current page of yii2
Preface
The addition, deletion, modification, and query operations are still on the current page. This gives you a good experience. However, the Yii2 framework itself does not remain on the current page after the addition, deletion, modification, and query operations are successful. To achieve this effect, you must write it yourself. My principle is to keep the core code unchanged and stick to my own principles. Now I have shared it. We share the same things with each other. If you have better implementation methods, please contact us.
Requirement Analysis
1. After adding, deleting, modifying, and querying, the Operation will remain on the current page.
1. linked
Encapsulation code
There are two files: ActionColumn. php, Helper. php1, and ActionColumn. php.
<? Phpuse 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;/** override ActionColumn */class ActionColumn extends Column {public $ buttons; private $ defaultButtons = []; private $ callbackButtons; public $ controller; public $ urlCreator; public $ url_append = ''; public $ appendReturnUrl = true; // The default value is 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', // whether to upload id, if the parameter is not set to 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_app End = 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-> callbackBu Ttons, $ 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, 'appendretu Rnurl', $ 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-pja' => 0, // 'data-action' => $ button ['url'], 'class' => $ button ['class'], 'title' => $ button ['label'],]). '';} $ data. = '</div>'; return $ data ;}}
2. Helper. php file
<?phpuse 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; }}
View call
1. directly call Yii2['class' => 'yiigridActionColumn']Replace it with our new['class' => 'common\components\ActionColumn'].
2. If direct calls 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) {// custom parameter $ 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, // whether to retain the current URL. The default value is true 'url _ append' => '', 'keyparam' => 'id', // whether to upload id. If not, set null.], ['url' => 'update ', 'class' => 'btn btn-primary btn-xs btn-sm ', 'label' => Yii: t ('yii', 'update '), 'appendreturnurl' => true, // whether to retain the current URL. The default value is true 'url _ append' => '', 'keyparam' => 'id ', // whether to pass the id. If not, set null.], ['url' => 'delete', 'class' => 'btn btn-danger btn-xs btn-sm ', 'label' => Yii: t ('yii', 'delete'), 'options' => ['data-action' => 'delete',], 'appendreturnurl' => true, // whether to retain the current URL. The default value is true 'url _ append' => '', 'keyparam' => 'id ', // whether to pass the id. If not, set null],
3. If it is added, reference it like this.<?= Html::a(Yii::t('yii', 'Create'), ['create','returnUrl' => Helper::getReturnUrl()], ['class' => 'btn btn-success']) ?>.
Controller Logic
1. get the returnUrl with get. Code:$returnUrl = Yii::$app->request->get('returnUrl');.
2. Jump URL:return $this->redirect($returnUrl);.
Analysis Summary
1. The advantage of this method is that it does not move the core code. The call method retains the Yii2 built-in method.
2. The disadvantage is that every update, view, and delete operation must be written during custom links.'template' => '{view}{update}{delete}'Simple and comfortable to write as needed.
Well, the above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.