JStree 关联修改

根据业务需求,需要更改JStree源码,以下为记录使用

1.修改选中父级时不自动选中子级,选中子级时直接选中父级并存储父级id

.on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) {
    var obj = data.node,
        m = this._model.data,
        par = this.get_node(obj.parent),
        dom = this.get_node(obj, true),
        i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;

        // apply down
        // if(s.indexOf('down') !== -1) {
            // this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d));
            // for(i = 0, j = obj.children_d.length; i < j; i++) {
            // tmp = m[obj.children_d[i]];
            // tmp.state[ t ? 'selected' : 'checked' ] = true;
            // if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
                //  tmp.original.state.undetermined = false;
                // }
            // }
        // }

        // apply up
        if(s.indexOf('up') !== -1) {
            while(par && par.id !== '#') {
                // c = 0;
                // for(i = 0, j = par.children.length; i < j; i++) {
                    //  c += m[par.children[i]].state[ t ? 'selected' : 'checked' ];
                // }
                // if(c === j) {
                par.state[ t ? 'selected' : 'checked' ] = true;
                if($.inArray(par.id, this._data[ t ? 'core' : 'checkbox' ].selected)==-1){
                    this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id);
                }
                tmp = this.get_node(par, true);
                if(tmp && tmp.length) {
                    tmp.children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked');
                }
                // }
                // else {
                //  break;
                // }
                par = this.get_node(par.parent);
            }
        }

        // apply down (process .children separately?)
        if(s.indexOf('down') !== -1 && dom.length) {
            $(dom.find('.jstree-anchor')[0]).addClass(t ? 'jstree-clicked' : 'jstree-checked');
        }
    }, this))

2.取消选中子级时不取消父级选中状态,不移除选中的父级id

.on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) {
        var obj = data.node,
            dom = this.get_node(obj, true),
            i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;
            if(obj && obj.original && obj.original.state && obj.original.state.undetermined) {
                obj.original.state.undetermined = false;
            }

        // apply down
        if(s.indexOf('down') !== -1) {
            for(i = 0, j = obj.children_d.length; i < j; i++) {
                tmp = this._model.data[obj.children_d[i]];
                tmp.state[ t ? 'selected' : 'checked' ] = false;
                if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
                    tmp.original.state.undetermined = false;
                }
            }
        }

        // apply up
        if(s.indexOf('up') !== -1) {
            // for(i = 0, j = obj.parents.length; i < j; i++) {
                //  tmp = this._model.data[obj.parents[i]];
                //  tmp.state[ t ? 'selected' : 'checked' ] = false;
                //  if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) {
                //      tmp.original.state.undetermined = false;
                //  }
                //  tmp = this.get_node(obj.parents[i], true);
                //  if(tmp && tmp.length) {
                //      tmp.children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
                //  }
            // }
        }
        tmp = [];
        for(i = 0, j = this._data[ t ? 'core' : 'checkbox' ].selected.length; i < j; i++) {
            // apply down + apply up
            if(
                (s.indexOf('down') === -1 || $.inArray(this._data[ t ? 'core' : 'checkbox' ].selected[i], obj.children_d) === -1)
                // &&(s.indexOf('up') === -1 || $.inArray(this._data[ t ? 'core' : 'checkbox' ].selected[i], obj.parents) === -1)
            ) {
                tmp.push(this._data[ t ? 'core' : 'checkbox' ].selected[i]);
            }
        }
        this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(tmp);

        // apply down (process .children separately?)
        if(s.indexOf('down') !== -1 && dom.length) {
            dom.find('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked');
        }
    }, this));

3.jstree初始化时修改父级选中不默认选中子级

.on('model.jstree', $.proxy(function (e, data) {
    var m = this._model.data,
        p = m[data.parent],
        dpc = data.nodes,
        chd = [],
        c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection;

        // if(s.indexOf('down') !== -1) {
        //  // apply down
        //  if(p.state[ t ? 'selected' : 'checked' ]) {
        //      for(i = 0, j = dpc.length; i < j; i++) {
        //          m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true;
        //      }
        //      this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc);
        //  }
        //  else {
        //      for(i = 0, j = dpc.length; i < j; i++) {
        //          if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) {
        //              for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) {
        //                  m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true;
        //              }
        //              this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d);
        //          }
        //      }
        //  }
        // }

        if(s.indexOf('up') !== -1) {
            // apply up
            for(i = 0, j = p.children_d.length; i < j; i++) {
                if(!m[p.children_d[i]].children.length) {
                    chd.push(m[p.children_d[i]].parent);
                }
            }
            chd = $.vakata.array_unique(chd);
            for(k = 0, l = chd.length; k < l; k++) {
            p = m[chd[k]];
            while(p && p.id !== '#') {
                c = 0;
                for(i = 0, j = p.children.length; i < j; i++) {
                    c += m[p.children[i]].state[ t ? 'selected' : 'checked' ];
                }
                if(c === j) {
                    p.state[ t ? 'selected' : 'checked' ] = true;
                    if($.inArray(p.id, this._data[ t ? 'core' : 'checkbox' ].selected)==-1) {
                        this._data[t ? 'core' : 'checkbox'].selected.push(p.id);
                    }
                    tmp = this.get_node(p, true);
                    if(tmp && tmp.length) {
                        // tmp.children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked');
                    }
                }
                else {
                    break;
                }
                p = this.get_node(p.parent);
            }
        }
    }

    this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected);
}, this))
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值