序
新增、删除、修改查询操作成功后,保留在当前页面,可以给用户很好的体验。但是Yii2框架本身并没有添加、删除、修改、检查等操作成功后留在当前页面的效果,所以要达到这样的效果,还得自己写。我的原则不是移动核心代码,而是始终坚持自己的原则。现在我分享一下。殊途同归。如果有更好的实现方法,欢迎交流。
需求分析
1.新增、删除、修改、检查后,操作成功后仍保留在当前页面。
1.链接的渲染
封装代码
有两个文件:ActionColumn.php、Helper.php1和ActionColumn.php文件
?关闭;使用kartik 图标图标;使用Yii使用yii 网格列;使用yii helpers助手 ArrayHelper使用yii 助手 Html使用yii 助手 Url使用公共组件助手;/**重写行动栏*/集体行动栏扩展了列{ public $ buttonsprivate $ DefaultButtons=[];私人$ callbackButtons按钮公共$控制器;public $ URL creatorpublic $ URL _ append=public $ appendReturnUrl=true//默认为没错,返回当前链接公共函数init(){ parent : init();$ this-默认按钮=[[' URL '=' view ',' icon'='eye ',' class'='btn btn-success btn-xs ',' label '=yii 3360:t(' yii ',' view '),' appendReturnUrl'=false,' url_append'=',' keyParam'='id ',//是否传id,不传设置null ],[ 'url'='update ',' icon'='pencil ',' class'='btn btn-primary btn-xs ',' label '=yii 3360:t(' yii ',' update '),],[ 'url'='delete ',' icon'='trash-o ',' class'='btn btn-danger btn-xs ',' label '=yii 33603:t(' yii ',' delete '),' options '=[' data-action '=' Delete ' if(null==$ this-buttons){ $ this-buttons=$ this-default buttons; } else if($ this-buttons Closure的实例){ $ this-callback button=$ this-buttons;} }公共函数createUrl($action,$model,$key,$index,$appendReturnUrl=null,$url_append=null,$keyParam='id ',$ attrs=[]){ if($ this-URL creator Closure的实例){ return call _ user _ func($ this-URL creator,$action,$model,$key,$ index);} else { $ params=[];if(is _ array($ key)){ $ params=$ key;} else { if(is _ null($ KeyParam)==false){ $ params=[$ KeyParam=(字符串)$ key];} } $params[0]=$this-controller?这个控制器。'/' .$ action : $ actionforeach($ attr as $ attrName){ if($ attrName==' model '){ $ params[' model ']=$ model;} else if($ Attname=='主类别。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 ']=help : getreturnurl();}返回Url:toRoute路径字符串($params).$ url _ append} }受保护函数renderDataCellContent($model,$key,$index){ if($ this-callbackbutton的Closure实例){ $ btns=call _ user _ func($ this-callbackbutton,$model,$key,$ index,$ this);if(null===$ btns){ $ this-buttons=$ this-DefaultButtons;} else { $ this-buttons=$ btns;} } $ min _ width=count($ this-buttons)* 34;//34是按钮宽度$ data=html : begintag(' div ',['class'='btn-group ',' style'='min-width: ' .$最小_宽度. px ']);foreach($ this-buttons as $ button){ $ appendReturnUrl=arrayhelper :3360 getvalue($ button,$ appendReturnUrl ',$ this-appendReturnUrl);$ url_append=arrayhelper : getvalue($ button,' URL _ append ',$ this-URL _ append);$ keyParam=ArrayHelper :3360 GetValue($ button,' KeyParam ',' id ');$ attrs=arrayhelper : get value($ button,' attrs ',[]);html : addcss类($ button,' BTN ');html : addcss类($ button,' BTN-sm ');$ button text=isset($ button[' text '])?' ' .$ button[' text ']: " ";$数据=HTMl :3360 a($ button[' label ']).$buttonText,$ URL=$ this-createUrl($ button[' URL '],$model,$key,$index,$appendReturnUrl,$url_append,$keyParam,$attrs),array helper 3360: merge(isset($ button[' options '])?$button['options'] : [],[ //'data-pjax'=0,////' data-action '=$ button[' URL '],' class'=$button['class'],' title'=$button['label'],]).' ';} $data .='/div ';返回$数据;}}2、Helper.php文件
?phpuse YiiHelper类{ private static $ return URL thistory内的public static $ return URL=false;/* * * @ param int $ depth * @ return string */public static函数getreturn URL(){ if(is _ null(self : $ return URL)){ $ URL=parse _ URL(yii : $ app-request-URL);$ return URL params=[];if(isset($ URL[' query ']){ $ parts=explode(',$ URL[' query ']);foreach($ parts as $ part){ $ pieces=explode('=',$ part);if(静态: $ returnUrl在此历史计数内($ pieces)==2 $ pieces[0]===' return URl '){ continue;} if(count($ pieces)=2 strlen($ pieces[1])0){ $ return URL params[]=$ part;} } } if(count($ return URL params)0){ self : $ return URL=$ URL[' path '].内爆(',$ returnurl参数);} else { self : $ return URl=$ URl[' path '];} } return self : $ return URl;}}视图调用
1、直接调用,把Yii2自带的[' class '=' yiigridActionColumn ']换成我们新写的[' class '=' common components action column ']。
2、如直接调用不能满足你的要求可以自定义链接,自定义链接的写法如下:
[' class '=' common components action column ',' urlCreator'=function($action,$model,$key,$index) use ($id) { //自定义链接传的参数$params=[ $action,' option_id'=$model-option_id,' id'=$id,];$ params[' return URL ']=common components help 3360: getreturn URL();返回yii helper URL : torut($ params);},' buttons'=[ [ 'url'='view ',' class'='btn btn-success btn-xs ',' label '=yii 3360:t(' yii ',' view '),' appendReturnUrl'=false,//是否保留当前网址,默认为true 'url_append'=' ',' keyParam'='id ',//是否传id,不传设置null ],[ 'url'='update ',' class '=' BTN BTN-主BTN-xs BTN-sm ',' label '=yii 3360:t(' yii ',' update '),' appendReturnUrl'=true,//是否保留当前网址,默认为true 'url_append'=' ',' keyParam'='id ',//是否传id,不传设置null ],[ 'url'='delete ',' class '=' BTN BTN-危险BTN-xs BTN-sm ,' label '=yii 3360:t(' yii ',' delete '),' options '=[' data-action '=' Delete ',],' appendReturnUrl'=true,//是否保留当前网址,默认为true 'url_append'=' ',' keyParam'='id ',//是否传id,不传设置null ],],],3,新增的话,这么引用?=html :3360 a(yii :3360t(' yii ',' Create '),['create ',' return URL '=helper :3360 getreturn URL()],['class'='btn btn-success'])?
控制器逻辑
1、用得到获取返回Url,代码:$ return URL=yii : $ app-request-get(' return URL ');
2、跳转到的网址:返回$ this-重定向($ return URl);
分析总结
1、此方法的好处是不动核心代码,调用方式保留了Yii2自带的方式。
2、不足之处是在自定义链接的时候需要把每一个操作更新、查看、删除都写出来,不能用这种模板=“{ view } { update } { delete }”简单且看着舒服能根据需求去写了。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。