1. 程式人生 > >JStree 無限節樹結構加子節點分頁

JStree 無限節樹結構加子節點分頁

$('#tree')
    .jstree({
        "core" : {
           "check_callback" : true,
            'data' : {
                "url" : function(data){
                    return '/admin/ajax/list';
                },
                "data" : function (node) {
                    if(node.id !="#"){
                        if(node.li_attr['is_reference']==1 && node.li_attr['viewid']!=undefined) {
                            var id = node.li_attr['viewid'];
                        } else {
                            var id = node.original['id'];
                        }
                        if(node.li_attr['type']==undefined) {
                            node.li_attr['type'] = 0;
                        }
                        if (node.li_attr['page'] != undefined && node.li_attr['page'] > 1) {
                            if (localStorage.getItem(id + "page") == undefined) {
                                localStorage.setItem(id + "page", 1);
                            }
                        }
                        console.log(node);
                        return {"id" : id, 'tableName': node.li_attr['tablename'], 'is_reference':node.li_attr['is_reference'], 'locking_status' : node.li_attr['is_locking'], 'type' : node.li_attr['type'], 'page' : localStorage.getItem(id+"page")};
                    }
                    return {"id" : 0, 'tableName': "root", 'is_reference' : 0, 'locking_status' : 0, 'type':0};//寫自己的載入資料
                }
            }

        },
        plugins : ["types", 'state'],
        "types" : {
            "default" : {  
                "icon" : "/css/default/folder.gif"  
            }, 
            "file" : { "icon" : "/css/default/file.gif" }
        }
    })
    .on('dehover_node.jstree', function (e, data) {
        var id = data.node['id'];
        $("#"+id+"_anchor").find(".handle").remove();
    })
    .on('hover_node.jstree', function (e, data) {
        var html= '';
        var nodeInfo = data.node.li_attr;
        var tableName = nodeInfo['tablename'];
        console.log(data);
        var id = data.node['id'];
        if(tableName!='root') {
            if(nodeInfo['add']==1) {
                html+=' <span class="glyphicon glyphicon-plus-sign" onclick="addNodeData(this);"></span>';
            }
            if(nodeInfo['edit']==1) {
                html+=' <span class="glyphicon glyphicon-edit" onclick="editNodeData(this);"></span>';
            }
        }
        if(tableName=='page' && nodeInfo['page']>1) {

            html+=' <span class="glyphicon glyphicon-backward" onclick="paging(this);"></span> <span class="glyphicon glyphicon-forward" onclick="paging(this);"></span>';
        }

        if(html!='') {
            $("#"+id+"_anchor").append('<span class="handle">'+html+'</span>');
        }
    })
    .on('changed.jstree', function (e, data) {
        if(data.node){
            var currNode = data.node;
            var nodeInfo = currNode.li_attr;
            var selectId = nodeInfo['viewid'];
            if(selectId==undefined){
                selectId = nodeInfo['idvalue'];
            }
            var is_locking = nodeInfo['is_locking'];
            var is_reference = nodeInfo['is_reference'];
            var tableName = nodeInfo['tablename'];
            $.post("/admin/nav/view", { id: selectId, tableName: tableName, is_locking: is_locking, is_reference: is_reference },
                function(result){
                $('#contentRight').html(result);
            });
            selectNode = data.node['id'];
            $.ajaxSetup({
                async: false
            });
            $.post("/admin/nav/referencedcount", { id: selectId, tableName: tableName },
                function(results){
                    if(results['error_code'] == 0){
                        var nodeId = selectNode.split('_')[1];
                        var t = parseInt(nodeId) + parseInt(results['count']);
                        selectNode = "j1_"+t;
                    }
                });
            var exp = new Date();  
            exp.setTime(exp.getTime() + 60 * 5000);//過期時間 5分鐘
            document.cookie="selectNode="+selectNode+";expires="+exp;
            
        }
    })
    .bind("loaded.jstree", function (e, data) {
       // data.instance.open_node(1);
        $('#tree').jstree('open_node', 'j1_1');
    })
    .bind('ready.jstree', function (e, data) {
        $('#tree').css('display','block');
        var selectId=document.cookie.split(";")[0].split("=")[1];
        if(!selectId){
            $('#tree').jstree('select_node', 'j1_1');
        }else{
            $('#tree').jstree('select_node', selectId);
        }

    })
public function getList()
{
    $id = Input::get('id');
    $table = Input::get('tableName');
    $is_reference = Input::get('is_reference');
    $locking_status = Input::get('locking_status');
    $page = Input::get('page', 1);
    $type = Input::get('type');
    $f_table = '';
    $data = $list = [];
    //檢視和編輯許可權
    $add = $edit = 0;
    $pageSize = 20;
    //檢視列表資訊
    if (empty($table)) {
        return Response::json(['error_code' => 40000, 'msg' => '缺少必要引數!']);
    }
    switch ($table) {
        case 'template':
            $tableName = "NavPage";
            $f_table = 'page';
            $t_table = 'screen';
            $reYes = Dictionary::getDictIdByKey('db.desktop.page.reference.yes');
            if (checkPermission('admin_nav_screen_add')) {
                $add = 1;
            }
            if (checkPermission('admin_nav_page_edit')) {
                $edit = 1;
            }
            $template_list = $this->userTemplate();
            break;
        case 'page':
            $tableName = "NavScreen";
            $f_table = 'screen';
            $t_table = 'block';
            if (checkPermission('admin_nav_block_add')) {
                $add = 1;
            }
            if (checkPermission('admin_nav_screen_edit')) {
                $edit = 1;
            }
            break;
        case 'screen':
            $tableName = "NavBlock";
            $f_table = 'block';
            if (checkPermission('admin_nav_block_edit')) {
                $edit = 1;
            }
            break;
        case 'root':
            $tableName = "NavTemp";
            //$list = \NavTemp::all();
            $f_table = 'template';
            $version_data = $this->templateVersion();
            $temptype_ppos_id = Dictionary::getDictIdByKey('db.desktop.templatetype.ppos');
            if (checkPermission('admin_nav_page_add')) {
                $add = 1;
            }
            if (checkPermission('admin_nav_temp_edit')) {
                $edit = 1;
            }
            $template_list = $this->userTemplate();
            $list = \NavTemp::whereIn('id', array_keys($template_list))->get();
    }
    if (empty($list)) {
        $builder = $tableName::where($table . '_id', '=', $id);
        $builder = $builder->orderBy('sort', 'asc');
        if ($tableName != 'NavTemp') {
            $builder = $builder->orderBy('fr_epg_status', 'asc');
        }
        if ($table == 'template' && isset($template_list[$id]) && !in_array(0, $template_list[$id])) {
            $builder->whereIn('id', $template_list[$id]);
        }
        if ($table == 'page') {
            $offset = ($page - 1) * $pageSize;
            if ($page > 1) {
                $builder = $builder->offset($offset)->limit($pageSize);
            }
        }
        $list = $builder->get();
    }
    foreach ($list as $k => $v) {
        $data[$k]['id'] = $v['id'];
        $data[$k]['text'] = $v['name'];
        $data[$k]['li_attr'] = ["tablename" => $f_table, 'idvalue' => $v['id']];

        //顯示新增或編輯狀態
        $data[$k]['li_attr']['add'] = $add;
        $data[$k]['li_attr']['edit'] = $edit;

        if ($table == 'root') {
            $data[$k]['children'] = true;
            $data[$k]['li_attr']['is_reference'] = 0;
            $data[$k]['li_attr']['is_locking'] = 0;
            $data[$k]['li_attr']['class'] = $f_table;
            $data[$k]['li_attr']['tmplate_type_version'] = isset($version_data[$v['fr_template_type']]) ? $version_data[$v['fr_template_type']] : $version_data[$temptype_ppos_id];
            $data[$k]['li_attr']['fr_template_type'] = $v['fr_template_type'];
        } else {
            if ($table != 'screen' && !empty($v->$t_table())) {
                $data[$k]['children'] = true;
            } else {
                $data[$k]['children'] = false;
            }
            //只要為鎖定狀態,不可新增或編輯許可權
            if($v['locking_status']==1) {
                $data[$k]['li_attr']['add'] = $data[$k]['li_attr']['edit'] = 0;
            }
            if ($table == 'template') {
                if ($v['is_reference'] != $reYes) {
                    $data[$k]['li_attr']['is_reference'] = 0;
                } else {
                    //如果是引用值,不可新增
                    $data[$k]['li_attr']['add'] = 0;
                    $data[$k]['li_attr']['is_reference'] = 1;
                    $data[$k]['li_attr']['viewid'] = $v['reference_id'];
                }
                $data[$k]['li_attr']['is_locking'] = $v['locking_status'];
                //判斷是否有分頁
                $count = count($v->$t_table);
                $pages = ceil($count / $pageSize);
                $data[$k]['li_attr']['page'] = $pages;
            }
            if ($table == 'page' || $table == 'screen') {
                $data[$k]['li_attr']['is_reference'] = $is_reference;
                $data[$k]['li_attr']['is_locking'] = $locking_status;
                //組合推薦位不加新增
                if ($table == 'page') {
                    if($v['type']!=0) {
                        $data[$k]['li_attr']['add'] = 0;
                    }
                    $data[$k]['li_attr']['type'] = $v['type'];
                }
                if ($table == 'screen') {
                    $data[$k]['icon'] = "/css/default/file.gif";
                    if($type!=0) {
                        $data[$k]['li_attr']['edit'] = 0;
                    }
                }
            }
        }

    }
    if ($table == 'root') {
        return Response::json(['id' => 0, 'text' => "模板列表", 'children' => $data,  "li_attr"=>["tablename"=>"root"] ]);
    } else {
        return Response::json($data);
    }

}