Phalon框架:将复选框所有选择的值从View视图层(.volt)传输到控制层(.php)

来源:互联网 发布:js调用手机摄像头 编辑:程序博客网 时间:2024/04/19 04:57

适用条件


1.Phalcon框架(MVC)

2.控制层采用PHP,视图采用Volt(使用了kendo grid)

3.目标:点击视图中的按钮,将按钮获得的数据,以url的形式传递给控制层。

我自身的情况是:1.在视图层中要在添加一列复选框,表头的复选框要求通过点击实现全选全不选,各个项对应的框表示自身是否被选择

                                2.在视图层中需要获取复选框选择的值,存储为字符串格式,加于url的字段中  XXXX/whitelist/bulkOnline?id=1,2,3,4

                    

                               例如  whitelist(views下文件夹)/bulkOnline(将跳转到控制层的某个函数)                

                                3.在控制层通过 $str = $_GET["id"]  从而获取到视图层 复选框传递的值    

                              注意:get[ ]中的内容应该 与 url 中的标识一致  id与id相对应


以下上代码:

一、视图层  

1. kendogrid下添加复选框这一列 并设置value为同行的id值

var grid = $("#grid").kendoGrid({dataSource : dataSource,pageable : {refresh : true,pageSizes : [ 10, 20, 50 ],buttonCount : 5,input : true,messages : {display : "显示第 {0}-{1} 条记录,共 {2} 条记录",empty : "无数据",page : "第",of : "页",itemsPerPage : "条/页"}},height : 500,toolbar: [                ],    persistSelection: true,    sortable:true,   // change: onChange,    selectable: "multiple row",columns: [                              //添加一列复选框 title表示全选全不选的复选框,template表示每一行对应的复选框,其中value="#id#"将复选框与id相对应    {editable: true,sortable:false,title:"<input id='testIds',name='testIds', type='checkbox', class='k-checkbox' value='viewContent'/>" ,template: '<input type="checkbox" name="testId" class="k-checkbox"value="#=id#" /> ',width: 12},     {editable: true, sortable: false, field:"id", title: "序号", width: "40px" },    {sortable: false, type:"text", field:"grouptxt", title: "名称", width: "80px" },     {sortable: false, type:"text", field:"user_id", title: "用户ID", width: "80px" },     {sortable: false, type:"text", field:"sid", title: "手机ID", width: "80px" },         {sortable: false, type:"text", field:"remoter_id", title: "相机ID", width: "80px" }, {editable: true, sortable: false, field:"statustxt", title: "状态", width: "45px" }, {sortable: false, field:"typetxt", title: "类型", width: "45px" }, { command: [{ text: "上线", click: agree }, { text: "下线", click: reject }, { text: "修改", click: editItem }, { text: "删除", click: deleteItem }, ], title: " ", width: "70px" } ],editable : { mode: "popup", window: { title: "编辑", animation: false, }}});



2 .实现点击第一行的复选框达到全选全不选的效果 id = testIds的复选框  对应的事件swapCheck()

    //checkbox 全选全不选功能的实现        var isCheckAll = false;//默认不选中        function swapCheck(){            if (isCheckAll) {                $("input[type='checkbox']").each(function() {                    this.checked = false;                });                isCheckAll = false;            } else {                $("input[type='checkbox']").each(function() {                    this.checked = true;                });                isCheckAll = true;            }        }
复选框一通乱选之后,点击页面上的一个“批量上线按钮” ,(按钮click事件)--->这时将所有被选择复选框的id加到一个字符串中,形如1,2,6,

3. “批量上线按钮”对应的事件
获取所选复选框value值 ->存入url->getjson通过url跳转到控制层->经过控制层一番处理后->function(o)返回处理结果
 //批量上线     function checkOnItem(e) {       //获取被选择的复选款的所有balue值到字符串str       var obj = document.getElementsByName("testId");        var str = "";        for(k in obj){            if(obj[k].checked)            str += obj[k].value + ",";        }        var str1=str.substring(0,str.length-1);//去掉最后一个多余的逗号        //通过url找到控制层的函数        var url = crudServiceBaseUrl+"/whitelist/bulkOnline?";//这里应保证写法正确        url += "id=" + str1;                                  //控制层通过id来获取str1                        $.getJSON(url,null,function(o){//第一个参数表示跳转的控制器的地址                 if(o.errorcode == 0){                  popupNotification.show("批量上线成功!", "info");//返回的提示信息                  $(".k-i-refresh","#grid").trigger("click");        //这时完成了一次批量上线操作了,我们希望全选复选框testids 不具备记忆性,也就是testids应该在每次操作完成后,默认状态为不被选中                               document.getElementById("testIds").checked=false;                                isCheckAll = false;                 }else{                  popupNotification.show(o.errormsg, "error");                 }                });        }



4.控制层的处理过程
    public function bulkOnlineAction()    {        //获取视图层传输的字符串 通过id获取       $str = $_GET['id'];         //PHPexplode函数将字符串按照逗号分割成数组        $ids = explode(",",$str);        //echo "<pre>";        //var_dump($check);        //echo count($ids);exit;        $errorcode = 0;        $errormsg  = "成功";
        for($i=0;$i<count($ids);$i++)        {            $temp = $ids[$i];            $publishuser = PublishUser::findFirstById($temp);            $publishuser->status = 3;            if(!$publishuser->save()){                $errorcode = 1;                $errormsg  = "失败:";                foreach ($publishuser->getMessages() as $msg) {                    $errormsg.= $msg;                }            }        }       //返回一个数组到视图层 视图层通过function(o)来显示处理结果      $jsonarray = array(            "errorcode"  =>  $errorcode,            "errormsg"   =>  $errormsg        );        echo json_encode($jsonarray);        exit;
    }
复选框全选效果参考:http://blog.csdn.net/graceup/article/details/46650781

kendogrid复选框的写法参考:http://blog.csdn.net/jbgtwang/article/details/52062278



 
原创粉丝点击