ztree树状实现全选反选

ztree实现全选反选操作:
全选
反选
查询

     

    /**
     * 全选按钮
     */
    var num=0;
    $(document).on("click","#allCheck",function(){
        num++;
        if(num%2 ==0){
            sessionStorage.removeItem("ids");
            var sel_a=[];
        }else{
            var v = "[[${sb}]]".split(",");//树状列表所有内容
            var split = sessionStorage.getItem("ids");
            var sel_a=[];
            if (v.length>0) {
                for(var item =0;item

    效果图如下:ztree树状实现全选反选_第1张图片

    /**
     * 反选按钮
     */
    $(document).on("click","#inverse",function(e){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"); //ztree对象
        var checked = zTree.getCheckedNodes(true);//得到选中的节点
        var checkeds = zTree.transformToArray(checked); //转变为数组
        var checkNode = zTree.getCheckedNodes(true).length;//选中的节点数量
        var v = "[[${sb}]]".split(",");
        var node = zTree.getNodes();//全部节点
        var nodes = zTree.transformToArray(node);//全部节点数量
    
        if(checkNode< v.length){//已选中的节点数小于总数 - 全选
            zTree.checkAllNodes(true);//给所有的都设置为true
    
            $.each(checked, function(index,node) {//之前选中的节点为false
                zTree.checkNode(node, false, false);
            });
    
        }else{
            zTree.checkAllNodes(false);//否则所有的都设置为false
        }
    
        var all=[];
        if(nodes.length > 0){
            for(var item =0;item

    反选效果:ztree树状实现全选反选_第2张图片

    function checkObj() {
       var sel_a = sessionStorage.getItem("ids")
       var sel_a1 = sessionStorage.getItem("ids")
       var val = "";
       if (sel_a != "") {
          var strings = sel_a.split(",");
          // 获取树对象
          var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
          /** 获取所有树节点 */
          var nodes = treeObj.transformToArray(treeObj.getNodes());
          // 遍历树节点设置树节点为选中
          for (var k = 0, length_3 = nodes.length; k < length_3; k++) {
             for (var item = 0; item < strings.length; item++) {
                var values = strings[item].split("~");
                if (values[0] == nodes[k].id) {
                   nodes[k].checked = true;
                   treeObj.updateNode(nodes[k], true);
                }
             }
    
          }
       } else {
          var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
          treeObj.checkAllNodes(false);
       }
       check_val1 = sel_a1.split(",");
       sessionStorage.setItem("id1",check_val1)
    }

    ztree.js中的写法

    var setting = {
          check: {
             enable: true,
             chkboxType: {"Y":"", "N":""}
          },
          view: {
             dblClickExpand: false
          },
          data: {
             simpleData: {
                enable: true
             }
          },
          callback: {
             onCheck: onCheck
          }
       };
    
       
    
    
    //定义一个变量用于存储选中复选框的值
    var sel_a = []
    function onCheck(e, treeId, treeNode) {
       //定义一个变量用于存储选中复选框的值
       var sel_a = []
       var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
          // nodes = zTree.getCheckedNodes(true)
          nodes = zTree.getCheckedNodes();
       // var split = sessionStorage.getItem("ids");
       if (nodes != null && nodes != "") {
          // var strings = split.split(",");
          if (nodes.length > 0) {
             for (var item = 0; item < nodes.length; item++) {
                var v = nodes[item].id + "~" + nodes[item].name;
                if ($.inArray(v, sel_a) == -1) {
                   sel_a.push(v);
                } else {
                   for (var i = sel_a.length - 1; i >= 0; i--) {
                      if (sel_a[i] == v) {
                         sel_a.splice(i, 1);
                      }
                   }
                }
             }
          } else {
             // sessionStorage.removeItem("ids");
             sel_a = [];
          }
    
       }
       sessionStorage.setItem("ids", sel_a);
    }
    
       $(document).ready(function () {
    
          var keyword = $("#keyword").val();
          var ssjd_id = $("#ssjd_id").val();
          $.ajax({
             url: '/dman/ztree?keyword=' + keyword + '&ssjd=' + ssjd_id,
             type: "POST",
             dataType: 'json',
             success: function (data) {
                var zNodes = new Array();//把后台json格式的数据放到数组中
                for (var i = 0; i < data.length; i++) {
                   zNodes[i] = {
                      id: data[i].id,
                      pId: data[i].pId,
                      name: data[i].name
                   }
                }
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                checkObj();
    
             }
          });
    
       });

    你可能感兴趣的:(ztree树状实现全选反选)