Layui 树 保存展开状态

2022/6/15 23:20:39

本文主要是介绍Layui 树 保存展开状态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//通过cookie保存展开的节点id
    //设置展开id的变量
    var open_id_list = [];
    //判断结构树是否加载完毕
    function isOverLoad(){
        var loadover = $('.layui-icon.layui-tree-head').html();
        if(loadover != undefined){
            retention();
        }else{
            setTimeout(isOverLoad, 1000);
        }
    }
    //初始化
    function retention(){
        openIdList();
        //监听展开按钮
        $('.layui-tree-head').click(function(){
            setOpenIdList(this);
        });
    }
    //清除保存的展开id
    function reset(){
        setCookie("open_id_list",'',30);
    }
    //展开上次展开的内容
    function openIdList(){
        if(checkCookie('open_id_list')){
            open_id_list = JSON.parse(getCookie('open_id_list'));
            for (var i=0;i<open_id_list.length;i++){
                treeGrid.treeNodeOpen('list',treeGrid.getDataMap('list')[parseInt(open_id_list[i])],true);
            }
        }
    }
    //将展开行的id推进展开id变量并保存到cookie
    function setOpenIdList(_self){
        //获取行id
        var id = $(_self).parent().parent().parent().prev().children().children().html();
        if(id != undefined){
            var index = open_id_list.indexOf(id);
            if(index < 0){
                open_id_list.push(id);
            }else{
                open_id_list.splice(index,1);
            }
            setCookie("open_id_list",JSON.stringify(open_id_list),30);
        }
    }
    //获取cookie
    function getCookie(cname){
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
        }
        return "";
    }
    //设置cookie
    function setCookie(cname,cvalue,exdays){
        var d = new Date();
        d.setTime(d.getTime()+(exdays*24*60*60*1000));
        var expires = "expires="+d.toGMTString();
        document.cookie = cname+"="+cvalue+"; "+expires;
    }
    //判断cookie是否存在
    function checkCookie(cname){
        var ProjectBudget=getCookie(cname);
        if (ProjectBudget!=""){
            return true;
        }
        return false;
    }

 



这篇关于Layui 树 保存展开状态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程