bootstrap-treeview 自定义添加节点方法

        bootstrap-treeview官方没有给出动态添加子节点和子节点集合的方法, 当需要点击父节点再去从后台获取其子节点时, 需要用户自定义动态加载子节点方法. 本文自定义了添加节点集合方法, 用于一次添加多个子节点. 步骤如下.


1. 添加方法入口


        在Tree主函数return {/*在这里添加addNewNodes的入口*/} 处添加代码


            // Add Nodes Method
            addNewNodes: $.proxy(this.addNewNodes, this),

        添加结果如下:


        return {
            // Options (public access)
            options: this.options,

            // Initialize / destroy methods
            init: $.proxy(this.init, this),
            remove: $.proxy(this.remove, this),

            // Add Method
            addNewNodes: $.proxy(this.addNewNodes, this),

            // Get methods
            getNode: $.proxy(this.getNode, this),
            getParent: $.proxy(this.getParent, this),
            getSiblings: $.proxy(this.getSiblings, this),
            ......
        };
    };

2. 编写添加节点方法


	/**
	 * 给节点添加子节点
	 * @param {Object|Number} identifiers - A valid node, node id or array of node identifiers
	 * @param {optional Object} options.node; 
	 */
	Tree.prototype.addNewNodes = function (identifiers, options) {
	    this.forEachIdentifier(identifiers, options, $.proxy(function (node, options) {
	            this.setNewNodes(node, options);
	    }, this));

	    this.setInitialStates({ nodes: this.tree }, 0);
	    this.render();
	};

	/**
	 *  添加子节点
	 */
	Tree.prototype.setNewNodes = function (node, options) {
	    if (node.nodes == null) node.nodes = [];
	    if (options.nodes) {
	        $.each(options.nodes, function (index,option) {
	            node.nodes.push(option);
	        })
	    }
	};

3 使用


        // 当触发展开节点事件时, 发送ajax请求到后台获取子节点列表, 然后使用addNewNodes动态添加到treeview中

        onNodeExpanded: function(event, node) {
        	var childrenIds = listChildrenIds(node);
        	if (childrenIds.length > 0) {
        		return;
        	}
        	var nodeId = node.nodeId;
        	ajaxGet({
        		url: contextPath + "/departments/" + node.code + "/list",
        		success: function(res) {
        			checkableTree_departmentManage.treeview("addNewNodes", [nodeId, { nodes: res.data }]);
        		}
        	})
        },  


展示, 当加载时, 只加载一级部门, 如下图:

15b05636891641e04e2690aed59068cb23276603


点击一级部门, 再去后台请求一级部门的子部门, 如下:

9c9307a66b5f3657ee0a4097ee2deb80241cc051


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值