VUE中使用layui-tree

技术没有好坏,只是不同特性的技术应该用在不同的地方。VUE做树形插件也很方便但是不好用。特别是对于我这个做后端开发的来说,我只想快速的解决一个前端问题而不是永久的解决一个前端问题。

layui和VUE特性不同所以整合到一起使用比较麻烦,所以整合的案例比较少。我不想介绍怎么使用layui-tree在官网有更准确的介绍。下面就把我的踩坑过程详细的记录一下。

我需要在VUE框架下打开这个页面并渲染数据进去

VUE添加layui组件js的部分在网上有介绍,我就不啰嗦了。就记录一些网上没有的

页面代码



    


        
            
                
                    
                        
                    
                
                
                    
                        
                    
                
            
            
                
                    
                
                
                    查询
                
            
            
                

                
            
        
        
    

js代码

//5.主数据类别权限 
        
        var dialogForm4 = new Vue({
            el: '#dialogForm4',
            data: {
                dialogFormVisible: false,
                form: {
                    name: '',
                    code: '',
                },
                parm: [],
                roleid: '',
                search: '',
                searchflag:'N',
                treeLoading: false,
                saveLoading: false
            },
            created() {
                
            },
            laytree:null,
            mounted() {
                this.$nextTick(()=>{
                    layui.use("tree",()=>{
                        laytree=layui.tree;
                    })
                })
            },
            updated() {

            },
            methods: {                
                queryTree: function () {//渲染数据到树形结构
                    let that = this;                    
                    let datatree = [];
                    if (that.search == '') {
                        $.ajax({
                            url: Role.Url.typeJurisdiction,
                            type: 'get',
                            async: false,
                            data: {
                                uid: JSON.parse($.cookie('info')).id,
                                rid: that.roleid,
                                supper:'0'
                            },
                            xhrFields: { //带cookie请求
                                withCredentials: true,
                            },
                            dataType: 'json',
                            success: function (res) {
                                if (res.code == 0) {
                                    datatree=res.data;
                                };                            
                            }
                        });
                    } else {
                        $.ajax({
                            url: Role.Url.typeCodeOrName,
                            type: 'post',
                            async: false,
                            data: {
                                cn: that.search,
                                uid: JSON.parse($.cookie('info')).id
                            },
                            xhrFields: { //带cookie请求
                                withCredentials: true,
                            },
                            dataType: 'json',
                            success: function (res) {
                                $('#dialogForm4 .el-dialog').mLoading("hide");
                                if (res.code == 0) {
                                    datatree = res.data;
                                    //dialogForm4.searchflag='Y';
                                    //dialogForm4.rootData=res.data;
                                    //dialogForm4.$refs.rootTree.setCheckedKeys(res.data);        
                                }
                            }
                        });
                    }
                    //layui.use('tree', function () {
                    //    let laytree = layui.tree;                        
                        laytree.render({
                            elem: '#test3',
                            data: datatree,
                            showCheckbox: true, //是否显示复选框
                            onlyIconControl: true,
                            id: 'treeid',
                            oncheck: function (obj) {
                                //alert(JSON.stringify(obj.data.id))
                                //that.parm = tree.getChecked('id4');
                                //alert(1);
                            }
                        });
                    //});    
                },                                
                getCheckup: function () {//保存角色类别权限
                    let that = this;
                    alert(JSON.stringify(laytree.getChecked('treeid')))//到这里后边没写完需要修改
                    let parm = getChildNodes(that.parm, []);
                        alert(parm);
                    // 动画
                    that.saveLoading = true;
                    // 请求接口
                    $.ajax({
                        url: Role.Url.saveTypeJurisdiction,
                        type: 'post',
                        //contentType: 'application/json',
                        async: true,
                        data: {
                            uid: JSON.parse($.cookie('info')).id,
                            roleid: that.roleid,
                            ids: JSON.stringify(parm)
                        },
                        xhrFields: { //带cookie请求
                            withCredentials: true,
                        },
                        dataType: 'json',
                        success: function (res) {
                            that.saveLoading = false;
                            if (res.code == 0) {
                                that.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                                that.dialogFormVisible = false;
                            } else {
                                that.$message({
                                    message: res.msg,
                                    type: 'warning'
                                });
                            }
                        }
                    })
                }
            },
            
        });
    }

之前我把layui-tree写在函数里在另一个函数就调用不了。把layui-tree的注册写到data{}又报错。

我把layui-tree声名成一个VUE内置对象laytree在VUE的mounted里边注册layui组件。在methods里边实例化使用和调用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值