用jstree作許可權 CheckBox 控制選擇

來源:互聯網
上載者:User

參考:http://lipeng88213.javaeye.com/blog/727264

:http://junchao.javaeye.com/blog/698823
前台代碼:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">         
    <title>角色管理</title>  
       <link type="text/css" rel="stylesheet" href="../js/jstree/js0.9/themes/default/style.css"/>
    <script src="../lib/jquery-1.3.2.js"type="text/javascript"></script>  
    <script src="../js/jstree/js0.9/jquery.tree.js"type="text/javascript"></script>  
    <script type="text/javascript" src="../js/jstree/js0.9/jquery.tree.checkbox.js"></script>  
<style type="text/css">  
 
</style>  
<script type="text/javascript">  
 
$(function(){  
    $.ajaxSetup({cache:false});//ajax調用不使用緩衝  
    //樹  
    $("#menuTree").tree({  
        //主題  
        ui : {  
            theme_name : "checkbox"   
        } ,  
        data : {  
          type : "json",  
          async : false,  
          opts : {  
            method: "POST",  
            url : "json.aspx" 
          }  
        },  
        types :{    
            "default" : {    
                draggable : false //設定節點不可拖拽    
            }    
        },  
        plugins : {  
            //外掛程式  
            checkbox : {}  
        }    
      }).bind('click.jstree', function(event) {
            var eventNodeName = event.target.nodeName;
     if (eventNodeName == 'INS') {
      return;
     } else if (eventNodeName == 'A') {
      var $subject = $(event.target).parent();
      if ($subject.find('ul').length > 0)
      {
       //$('#show').html("<font color='red' size='2em'>請選擇葉子節點</font>");
      }
      else
      {
       alert( $subject.text());
       alert($subject.attr("id"));//擷取ID的值。。
      }
     }
});  
 
   //初始化原有菜單許可權  
    $("#menuTree").ajaxStop(function(){  
        /* 
        //得到伺服器傳過來的原有許可權id的字串,格式自訂,我的格式為"0001;0002;xxx;" 
        var checkMenu = $('#checkedMenu').val(); 
        //分割字串成數組 
        var array = checkMenu.split(";"); 
        for(var i=0;i<array.length;i++){ 
        //設定原有許可權菜單處於選中狀態,其中$("#0001")為id為0001的節點。 
            jQuery.tree.plugins.checkbox.check($("#"+array[i]+"")); 
            } 
            */ 
        jQuery.tree.plugins.checkbox.check($("#12"));  
    });  

});  
 
 
 
//取得選中的菜單id  
function getMenuIds(){  
    //取得所有選中的節點,返回節點對象的集合  
    var menu = jQuery.tree.plugins.checkbox.get_checked();  
    //得到節點的id,拼接成字串  
    var ids="";  
    for(i=0;i<menu.size();i++){  
        ids += menu[i].id+";";  
    }  
    //寫回表單  
    $('#menuIds2').val(ids);  
}   
//重設  
function reset1(){  
    /** 
    //得到所有選中的節點集合 
    var menu = jQuery.tree.plugins.checkbox.get_checked(); 
    for(i=0;i<menu.size();i++){ 
    //去掉其選中狀態 
    jQuery.tree.plugins.checkbox.uncheck($("#"+menu[i].id+"")); 
    } 
    */ 
    jQuery.tree.plugins.checkbox.uncheck($("#1"));  
}  
 
</script>  
</head>  
<body>  
    <input type="hidden" name="checkedMenu" id="checkedMenu" value="" />  
       <input type="button" onclick="getMenuIds()" value="得到選中id"/>  
    <input type="button" onclick="reset1()" value="取消選中" />  
    <input type="text" id="menuIds2" />  
      <br /> 
    <input type="hidden" name="menuIds"  id="menuIds" />  
    <div id="menuTree" style="height:500px"></div>     
  
 
      
</body>  
</html> 

後台代碼:json.aspx

public partial class json : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/json";
            //string test = "{/"attr/":{/"id/":/"11/",/"rel/":/"folder/"}, /"children/" : [ { /"data/" : /"彭濤test1/", /"state/" : /"closed/" },{ /"data/" : /"彭濤test2/", /"state/" : /"closed/" } ], /"data/":/"彭濤/",/"state/":/"closed/"}";
            string test = "[{/"data/":/"系統基礎/",/"state/":/"open/",/"attributes/":{/"id/":/"1/"},/"children/" : [ { /"data/" : /"彭濤test1/", /"state/" : /"closed/",/"attributes/":{/"id/":/"11/"} },{ /"data/" : /"彭濤test2/", /"state/" : /"closed/" ,/"attributes/":{/"id/":/"12/"}} ]},{/"data/":/"商務邏輯/",/"state/":/"closed/",/"attributes/":{/"id/":/"2/"}}]";
            //string test = "[  { attributes: {id: /"1/"},data : /"A node/", children : [ { attributes: {id: /"2/"},data : /"Only child/" } ,{ attributes: {id: /"4/"},data : /"Only child/" }], state : /"open/" },{ attributes: {id: /"3/"},data : /"Some other node/" }]"; 
            Response.Write(test);

        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.