Bläddra i källkod

下拉部门选人

lizhen_gitee 8 månader sedan
förälder
incheckning
02f387c989

+ 45 - 1
application/admin/controller/Trainactive.php

@@ -17,7 +17,7 @@ class Trainactive extends Backend
      * @var \app\admin\model\Trainactive
      */
     protected $model = null;
-    protected $noNeedLogin = ['qrcode','showinfo','nograde'];
+    protected $noNeedLogin = ['qrcode','showinfo','nograde','transfer','selectuser'];
 
     public function _initialize()
     {
@@ -228,4 +228,48 @@ class Trainactive extends Backend
         $this->assign('lists',$lists);
         return $this->view->fetch();
     }
+
+    /**
+     * 穿梭
+     */
+    public function transfer(){
+        //
+        $this->view->engine->layout(false);
+        return $this->view->fetch();
+    }
+
+    /**
+     * 选人
+     */
+    public function selectuser($ids = null){
+
+        if($this->request->isPost()){
+            $params = $this->request->post('row/a');
+            $user_rule = explode(',',$params['user_rule']);
+
+            $user_ids = [];
+            foreach($user_rule as $key => $val){
+                if(strpos($val,'u_') !== false){
+                    $user_ids[] = substr($val,2);
+                }
+            }
+            $user_ids = implode(',',$user_ids);
+
+            $update = [
+                'user_ids' => $user_ids,
+                'user_rule' => $params['user_rule'],
+            ];
+            Db::name('train_active')->where('id',$ids)->update($update);
+            $this->success('设置成功');
+        }
+
+        $row = Db::name('train_active')->where('id',$ids)->find();
+
+        $user_rule = explode(',', $row['user_rule']);
+        $nodeList = \app\admin\model\User::getTreeList($user_rule);
+        $this->assign("nodeList", $nodeList);
+
+        $this->assign('row',$row);
+        return $this->view->fetch();
+    }
 }

+ 48 - 2
application/admin/model/User.php

@@ -3,8 +3,8 @@
 namespace app\admin\model;
 
 use think\Model;
-
-
+use think\Db;
+use fast\Tree;
 class User extends Model
 {
 
@@ -121,4 +121,50 @@ class User extends Model
     {
         return $this->belongsTo('app\admin\model\user\Jigou', 'jigou_id', 'id', [], 'LEFT')->setEagerlyType(0);
     }
+
+    public static function getTreeList($selected = [])
+    {
+        //用户列表,重置id,防止和岗位表的id重复
+        $userlist = Db::name('user')->field('id,gangwei_id as pid,nickname as name')->select();
+        foreach($userlist as $key => $user){
+            $userlist[$key]['id'] = 'u_'.$user['id'];
+            $userlist[$key]['spacer'] = '';
+            $userlist[$key]['haschild'] = 0;
+        }
+
+        //岗位树
+        $ruleList = Db::name('user_gangwei')->field('id,pid,name')->select();
+        Tree::instance()->init($ruleList);
+        Tree::instance()->icon = ['','',''];
+        Tree::instance()->nbsp = '';
+        $ruleList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
+
+        //修改haschild
+        //拿到hasChildrens
+        $hasChildrens = [];
+        foreach ($ruleList as $k => $v)
+        {
+            foreach($userlist as $key => $user){
+                if($user['pid'] == $v['id']){
+                    $v['haschild'] = 1;
+                }
+            }
+            $ruleList[$k] = $v;
+
+            if ($v['haschild']){
+                $hasChildrens[] = $v['id'];
+            }
+        }
+
+        //合并
+        $ruleList = array_merge($ruleList,$userlist);
+
+        //最终数据
+        $nodeList = [];
+        foreach ($ruleList as $k => $v) {
+            $state = array('selected' => in_array($v['id'], $selected) && !in_array($v['id'], $hasChildrens));
+            $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => $v['name'], 'type' => 'menu', 'state' => $state);
+        }
+        return $nodeList;
+    }
 }

+ 24 - 0
application/admin/view/trainactive/selectuser.html

@@ -0,0 +1,24 @@
+<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
+
+    <input type="hidden" name="row[user_rule]" value="{$row.user_rule}" />
+
+    <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2">{:__('User_ids')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            <span class="text-muted"><input type="checkbox" name="" id="checkall" /> <label for="checkall"><small>{:__('Check all')}</small></label></span>
+            <span class="text-muted"><input type="checkbox" name="" id="expandall" /> <label for="expandall"><small>{:__('Expand all')}</small></label></span>
+
+            <div id="treeview"></div>
+        </div>
+    </div>
+
+    <div class="form-group layer-footer">
+        <label class="control-label col-xs-12 col-sm-2"></label>
+        <div class="col-xs-12 col-sm-8">
+            <button type="submit" class="btn btn-primary btn-embossed disabled">{:__('OK')}</button>
+        </div>
+    </div>
+</form>
+<script>
+    var nodeData = {:json_encode($nodeList); };
+</script>

+ 79 - 3
public/assets/js/backend/trainactive.js

@@ -1,5 +1,20 @@
-define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
-
+define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form) {
+    //读取选中的条目
+    $.jstree.core.prototype.get_all_checked = function (full) {
+        var obj = this.get_selected(), i, j;
+        for (i = 0, j = obj.length; i < j; i++) {
+            obj = obj.concat(this.get_node(obj[i]).parents);
+        }
+        obj = $.grep(obj, function (v, i, a) {
+            return v != '#';
+        });
+        obj = obj.filter(function (itm, i, a) {
+            return i == a.indexOf(itm);
+        });
+        return full ? $.map(obj, $.proxy(function (i) {
+            return this.get_node(i);
+        }, this)) : obj;
+    };
     var Controller = {
         index: function () {
             // 初始化表格参数配置
@@ -67,6 +82,15 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'operate', title: __('Operate'), table: table,
                             buttons:[
                                 {
+                                    name:'selectuser',
+                                    text:'选择用户',
+                                    title:'选择用户',
+                                    icon:'fa fa-exclamation-circle',
+                                    classname:'btn btn-xs btn-info btn-dialog',
+                                    url:'trainactive/selectuser/id/{ids}?dialog=1',
+                                    target:'_self',
+                                },
+                                {
                                     name:'qrcode',
                                     text:'二维码',
                                     title:'二维码',
@@ -166,6 +190,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
         add: function () {
             Controller.api.bindevent();
         },
+        selectuser: function () {
+            Controller.api.bindevent();
+        },
         qrcode: function () {
             Controller.api.bindevent();
         },
@@ -176,8 +203,57 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             Controller.api.bindevent();
         },
         api: {
-            bindevent: function () {
+            /*bindevent: function () {
                 Form.api.bindevent($("form[role=form]"));
+            }*/
+            bindevent: function () {
+                Form.api.bindevent($("form[role=form]"), null, null, function () {
+                    if ($("#treeview").length > 0) {
+                        var r = $("#treeview").jstree("get_all_checked");
+                        $("input[name='row[user_rule]']").val(r.join(','));
+                    }
+                    return true;
+                });
+                //渲染权限节点树
+                //销毁已有的节点树
+                $("#treeview").jstree("destroy");
+                Controller.api.rendertree(nodeData);
+                //全选和展开
+                $(document).on("click", "#checkall", function () {
+                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
+                });
+                $(document).on("click", "#expandall", function () {
+                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
+                });
+                $("select[name='row[pid]']").trigger("change");
+            },
+            rendertree: function (content) {
+                $("#treeview")
+                    .on('redraw.jstree', function (e) {
+                        $(".layer-footer").attr("domrefresh", Math.random());
+                    })
+                    .jstree({
+                        "themes": {"stripes": true},
+                        "checkbox": {
+                            "keep_selected_style": false,
+                        },
+                        "types": {
+                            "root": {
+                                "icon": "fa fa-folder-open",
+                            },
+                            "menu": {
+                                "icon": "fa fa-folder-open",
+                            },
+                            "file": {
+                                "icon": "fa fa-file-o",
+                            }
+                        },
+                        "plugins": ["checkbox", "types"],
+                        "core": {
+                            'check_callback': true,
+                            "data": content
+                        }
+                    });
             }
         }
     };